0

I have a function that gets called through a socket.io event. This function is in another moldule. I want to make sure that everytime the function gets called the variables in the module are not changed by previous function calls.

I know how to do this in other languages: create a new instance of an object, call the function but i cant seem to get this to work in javascript.

The code looks like this:

socket.io module where the functions gets called

// -- all events -- //
io.on('connection', function (socket) {
    console.log('user connected');

    socket.on('increase', function (data) {
        var increaser = require('./increase.js');
        increaser.increase();
    });
});

increase module, should print 1 everytime, but prints 1..2..3..4....

/*jslint node: true */
"use strict";

// -- variables -- //
var counter = 0;

module.exports = {
    increase : function () {
        counter += 1;
        console.log(counter);
    }
};

I want to know how to do this, because on my server a function gets called that calls a few asyncronous functions and i want to make sure that all variables stay like they are until the whole function is proccesed complete and dont get changed if another client connects and triggers the same event.

2 Answers 2

1

You can do the same exactly like other languages using an instance of object as below.

Method 1: Simplest function object

/*jslint node: true */
"use strict";


module.exports = function() {
   this.counter = 0;
   this.increase = function () {
        this.counter += 1;
        console.log(this.counter);
   };
};

Method 2: Function with prototype

/*jslint node: true */
"use strict";

function Increaser() {

}

Increaser.prototype = {
   counter: 0,
   increase: function () {
        this.counter += 1;
        console.log(this.counter);
   };
};

module.exports = Increaser;

And at your socket.io,

// -- all events -- //
io.on('connection', function (socket) {
    console.log('user connected');

    socket.on('increase', function (data) {
        var Increaser = require('./increase.js');
        var increaser = new Increaser();
        //  Also you can use as below in one line
        // var increaser = new require('./increase.js')();
        increaser.increase();
    });
});
Sign up to request clarification or add additional context in comments.

6 Comments

and now you can get rid of var counter = 0
Yes right @JoseHermosillaRodrigo. corrected now :-)
@Aruna so i should export every variable the function uses with this.variable in the export function ?
@Vujukas Yes, instead of a variable, you can use with this. Or you can use the same with prototype as I mentioned in method 2 above. Still you should use the property with this inside the increase method.
Thank you that really helped
|
0

The important concept here is that require() will always return the same object, not a new version each time.

So var increaser = require('./increase.js'); inside a callback is an anti-pattern. If you want a new object each time, consider making increase.js return a function which returns your increaser module. Then var counter will not be global.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.