0

i have a question about the node js execution of function. Code example

const dynamic = require('dynamic'),
      obj1 = dynamic.obj1,
      ovj2 = dynamic.obj2;

const myFunction1 = () => {
      let x = obj1  
      };

const myFunction2 = () => {
      let x = obj2  
      };

module.exports.myFunction1 = myFunction1;
module.exports.myFunction2 = myFunction2;

The question is how to design this code to able get updated value from dynamic variable every time when i calling myFunctions. Because require works only once, in the beginning of module. And the dynamic would be static, how to make a solution for it?

4
  • why do you have 3 different assignments to pass two variables down? Commented Jul 18, 2017 at 8:38
  • my code would be much bigger, and i am trying to find the solution to declare all my variables in the beginning, and make it update able if dynamic was changed. Most of the variables will execute the method to make the return data. And also i have proxy, dynamic in this case, which going to be updated if there is any changes Commented Jul 18, 2017 at 8:42
  • If your code should not be isomorphic, i.e. works in node.js and browser too, just use ECMAScript Proxy object. It satisfies all your requirements, including reading actual value of variable and dynamic function control flow. In simplier case, getters/setters would be enough. Commented Jul 18, 2017 at 8:43
  • @EdwardGizbreht Import/export subsystem, including dynamic import from ES8, is not correct way of solution, because only one object, may be complex, is exported, and it is immutable. But if it's Proxy or Getter/setter base object, all content can be variadic, and 'feel' like it re-imported any time. Commented Jul 18, 2017 at 8:45

2 Answers 2

1

This should work as you expect:

const dynamic = require('dynamic');

const myFunction1 = () => {
  const x = dynamic.obj1;
};

const myFunction2 = () => {
  const x = dynamic.obj2;
};

module.exports.myFunction1 = myFunction1;
module.exports.myFunction2 = myFunction2;
Sign up to request clarification or add additional context in comments.

Comments

1

If you are the owner of the dynamic module you can change it to export function from this module not an object, thanks to this caching mechanism of the require method wont be a problem. Then you can just call function from this module in every place in module, and values can be retrived updated

//  dynamic.js
module.exports = () => {

  return {
    obj1,
    obj2
  };
};


//and in another module
const dynamic = require('dynamic'),
  // then in code you can do this
  obj1 = dynamic().obj1,
  ovj2 = dynamic().obj2;

8 Comments

But the obj1 and obj2 would be with static data. the reason why i am asking is that i need to rewirte the objects if the dynamic was changed ...
@EdwardGizbreht hmm so what is the dynamic module is it javascript file or json file?
it is Proxy Tables .
@EdwardGizbreht in what circumstances the dynamic module can change?
@EdwardGizbreht Just use ES-Proxy or Object.create -like getters. It will fully solve your case. Not matter which is underlying logic, it's universal solution. For example, it's used in socket or IPC systems, in which imported objects are dynamic
|

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.