Re: [nodejs] Re: how to create a singleton

2013-11-27 Thread Gregg Caines
Sure that's the common explanation, but don't fall for it: globality has everything to do with it. If the singleton didn't care about creating global state, it could just have `this.created = true;` for its *only* internal state and have thrown exceptions from the constructor for any subsequent

Re: [nodejs] Re: how to create a singleton

2013-11-22 Thread Gregg Caines
I'm not 100% sure of your requirements, but this is what I assume you're after: - file : soapClient.js var memoizedClient; var soap = require('soap'); var create = function (cb){ if (memoizedClient){ return cb(null, memoizedClient); } var

Re: [nodejs] Re: how to create a singleton

2013-11-21 Thread Kamil Leszczuk
For example, if I have a module for emailing with a send() method on it, I don't have it export a constructor; I have it export an object. That object might maintain some state or it might not. When the module is subsequently require()'d, it will have any state that it has accumulated since.

Re: [nodejs] Re: how to create a singleton

2013-11-21 Thread Reza Razavipour
this I get the jist of the conversation but my Javascript skills, less than 2 months, does not allow me to be able to code this up. Also, as a starter in the new language the last thing I want to do is to miss out on the language correct way of things and force lets say the Java way of doing

Re: [nodejs] Re: how to create a singleton

2013-11-21 Thread Rick Waldron
I recommend being wary of any broad generalizations about programming patterns :P Rick On Thu, Nov 21, 2013 at 7:13 AM, Gregg Caines cai...@gmail.com wrote: The singleton pattern is actually unnecessary in most languages outside of java, including javascript. You should be wary of any

Re: [nodejs] Re: how to create a singleton

2013-11-21 Thread Reza Razavipour
Ok so this is the way i have it constructed... SoapClient is my module and I want it to be used as a singleton, i.e. if it is initialized, it just returns the connection, or SoapClient.instance... var SoapClient = function() { var soap = require('soap'); this.init = function (){ var url =

Re: [nodejs] Re: how to create a singleton

2013-11-21 Thread Kamil Leszczuk
Aah, nevermind then, I misunerstood ;) 21 lis 2013 16:36 Gregg Caines gr...@caines.ca napisał(a): Yeah... that's what I'm saying :) G On Thu, Nov 21, 2013 at 7:33 AM, Kamil Leszczuk kamit...@gmail.comwrote: For example, if I have a module for emailing with a send() method on it, I don't

Re: [nodejs] Re: how to create a singleton

2013-11-21 Thread Gregg Caines
Yeah... that's what I'm saying :) G On Thu, Nov 21, 2013 at 7:33 AM, Kamil Leszczuk kamit...@gmail.com wrote: For example, if I have a module for emailing with a send() method on it, I don't have it export a constructor; I have it export an object. That object might maintain some state or

Re: [nodejs] Re: how to create a singleton

2013-11-18 Thread Jose G. Quenum
yeah that's right. You just need to add the line module.exports = mySingleton and in the caller file require as follows var singleton = require('./single').mySingleton Hope that helps José On Nov 19, 2013, at 2:40 AM, Reza Razavipour reza.razavip...@gmail.com wrote: Wonderful book, thank you.