Thursday, June 11, 2015

#50over20

What is this hash tag? Well I got tired of seeing all these #30under30, or #40under40 clubs that people are a part of. There are plenty of awesome people who are not necessarily part of these communities where they can be nominated and win (assuming it's not just a popularity contest). So here's my idea. I'm going to find 50 people I have met in person that I think are awesome. My ONLY requirement is that I have met them. That is all. I think you guys need to have some recognition. If you're tagged in my #50over20 twitter feeds, (or if you're not on twitter, well that's ok too, we'll mention you somehow). Please send me a short bio and I'll try to talk you up too. (If you don't have my email, it's my twitter @gmail.com unless it's my onaclovtech twitter account, in which case it's tyson @ well you know my domain name.

For those of you reading this thinking. Man I wish I were part of a #50over20 club... YOU CAN BE, just start your own. Tweet things with the hash tag, and just rock it. Recognize people around you that you think are deserving, or well have simply met you that you would like to call out!

Maybe I'll make it on someone's list, but if not, at least I know that there are 50 people I know who deserve to be on someone's list!

I'll be following up with a post of the "winners" :)

Thanks and have fun!




Friday, June 5, 2015

Working Around Nodejs Module Common Singleton Design Pattern

I ran into a problem a few days ago. I built this really nifty queue module. It removes elements once they're old as defined by the key "endTime" (which come to think about it, maybe that could be a future enhancement to specify how to "expire", but I digress). Here is the problem, I needed a second queue. Well I can't just require my queue module in because it's cached and well it's not a "new" instance.

So for example.

var queue = require('queue.js');
var queue2 = require('queue.js');

queue.add({endTime : 1234});
queue2.add({enddTime : 2345});

// Assume queue.element returns the head of the queue
console.log(queue.element());

Output
>> {endTime : 2345}

// Assume queue.entire returns the entire queue
console.log(queue2.entire());

Output
>> {endTime : 1234}, {endTime : 2345}

As you can see you don't get a unique queue.

I found some examples around the interwebs and gave it a try. I have a problem with the common approach shown below.

var queue = function(){
   var self = this;
   self.local_queue = [];
   var funcs = {
                      add : function (obj){
                      ...... stuff ......
                      },
                     entire : function(){
                     ..... stuff..........
                     }
                     ...
       }
}
module.exports = function(){
    return new queue();
}

I don't like implementing my functions IN the surrounding function (the add: function example).

I prefer the following:

var add = function(obj){
... stuff ...
}

...

...


var queue = function(){
   var self = this;
   self.local_queue = [];
   var funcs = {
                      add : add,
                     entire : entire,
                     ...
       }
}
module.exports = function(){
    return new queue();
}

The problem is that my functions were not able to reference self. 

So I finally worked around this like this.

var add = function(self, obj){
... stuff ...
}

...

...


var queue = function(){
   var self = this;
   self.local_queue = [];
   var funcs = {
                      add : function(obj){return add(self, obj)},
                       ...
       }
}
module.exports = function(){
    return new queue();
}


If you notice now we have a self reference being passed to the function, all is now well in queue land.

Now when I require in it looks something like this.

var queue = require('./queue')
var queue1 = new queue();
var queue2 = new queue();

Now any adds and removes all affect only their own queues.

If you have a better cleaner way to implement this I'd be happy to see it.