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

[nodejs] Re: how to create a singleton

2013-11-26 Thread dhtml
On Thursday, November 21, 2013 7:13:48 AM UTC-8, Gregg Caines wrote: So how do you achieve the same effect in javascript? In the browser, you have globals. The global object has nothing to do with web browsers. If you want just one instance of a thing, create it, and set it to a

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

[nodejs] Re: how to create a singleton

2013-11-22 Thread Reza Razavipour
thanks for the response. I will have to 'digest' it but I get the idea you are laying out. My original intent is for the consumer of this object is to use the client synchronously on demand, as opposed to async. On Monday, November 18, 2013 3:38:09 PM UTC-8, Reza Razavipour wrote: A newbie

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

[nodejs] Re: how to create a singleton

2013-11-21 Thread Gregg Caines
The singleton pattern is actually unnecessary in most languages outside of java, including javascript. You should be wary of any javascript book that tries to teach you singletons at all. Many of those gang of four patterns simply don't translate outside of java and c++ (eg if you want to

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

[nodejs] Re: how to create a singleton

2013-11-20 Thread dhtml
On Monday, November 18, 2013 3:38:09 PM UTC-8, Reza Razavipour wrote: A newbie question... I have an app that connects and reuses the same connection to a remote database and a connection to a remote soap server. I want to implement a singleton pattern for each of these. I am used to

[nodejs] Re: how to create a singleton

2013-11-18 Thread Reza Razavipour
Wonderful book, thank you. So reading the book, if I put the code for the mySingleton class into a file on its own, call it single.js. In my main.js, I add that with a require statement, such as var singleton = require('./.single); when I say, singleton.getInstance(), I get a compile error

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.

[nodejs] Re: how to create a singleton

2013-11-18 Thread Reza Razavipour
Awesome that works wonderfully. Thanks. One more issue that you have an idea on, is that in getInstance I have to make an async call. It creates a soap connection and that is only supported in async mode. Any thoughts on how to structure that code? On Monday, November 18, 2013 3:38:09 PM