Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing & modifying a variable between multiple files node.js

main.js

var count = 1;  // psuedocode // if (words typed begins with @add) require('./add.js');  // if (words typed begins with @remove) require('./remove.js');  // if (words typed begins with @total) require('./total.js');    module.exports.count = count; 

total.js

var count = require('./main.js').count; console.log(count); 

add.js

var count = require('./main.js').count; count += 10; console.log(count); 

remove.js

var count = require('./main.js').count; count -= 10; console.log(count); 

console.log

 1  11  -9 

Background:

I have an application (irc bot), and I want to add a feature that peeps can do @add 1 or @remove 1. I have a main.js that then requires different files depending on the triggers that are said. So add would trigger the add.js file, and that would then require('main.js') and add 10 (10 for simplification, it'll actually parse the number and use that number) to it. The problem I'm having is when someone goes about and does @remove. It require('main.js') and subtracts 10 from 1 resulting in -9. And doing @total would output 1.

I've done a fairly good search for module.exports and I haven't come across an example like the one i listed above. The docs don't include any examples close to what I'm wanting to do; and these questions 1, 2 I understand--but aren't of any usefulness to me--as I understand what's being said there.

Question:

I'd like to have both @add and @remove manipulate the same variable ( count ), and for @total to return the total of count with the @add and @removes taken into account. Am I using module.exports incorrectly; or is there a common way that variables are shared, with one file being able to modify the contents of the module.exports and returning the results to the main.js file?

like image 240
thtsigma Avatar asked Jun 15 '13 04:06

thtsigma


People also ask

What you mean by sharing?

Sharing is the joint use of a resource or space. It is also the process of dividing and distributing. In its narrow sense, it refers to joint or alternating use of inherently finite goods, such as a common pasture or a shared residence.

What is the another word of sharing?

Frequently Asked Questions About share The words partake and participate are common synonyms of share. While all three words mean "to have, get, or use in common with another or others," share usually implies that one as the original holder grants to another the partial use, enjoyment, or possession of a thing.


1 Answers

Your problem is that when you do var count = require('./main.js').count;, you get a copy of that number, not a reference. Changing count does not change the "source".

However, you should have the files export functions. Requiring a file will only run it the first time, but after that it's cached and does not re-run. see docs

Suggestion #1:

// main.js var count = 1; var add = require('./add.js'); count = add(count);  // add.js module.exports = function add(count) {     return count+10; } 

#2:

var count = 1; var add = function() {     count += 10; } add(); 

#3: Personally i would create a counter module (this is a single instance, but you can easily make it a "class"):

// main.js var counter = require('./counter.js'); counter.add(); console.log(counter.count);  // counter.js var Counter = module.exports = {     count: 1,     add: function() {         Counter.count += 10;     },     remove: function() {         Counter.count += 10;     } } 
like image 188
MiniGod Avatar answered Sep 18 '22 00:09

MiniGod