0

Hi any help or links to similar questions is greatly appreciated as I have done a bit of research but am not sure on the most correct path here.

I dont really know how to explain exactly what i want so i drew this picture that might help explain a bit:

enter image description here

Currently i have all written in 1 file and would like to make it more modular so others can code there own 'strategy's' using system objects and so on.

The main goal here is that i can change all the system code in one place because i have many strategies and if i want to update the system code to add more results etc.. i need to change it in many files.

A good comparison i can see to this is the npm module testcafe. Once downloaded how you use it in your code is similar:

import testcafe

test(testName, => { all the code you want to run is here and you have many 
objects and functions provided from testcafe you can use})

My guess would be to achieve this maybe i need global classes, maybe a class called strategy and inside of it is all the objects, functions, for loop, log results, etc..

Things i worry about:

  1. how to integrate the user defined globals into the strategy
  2. how to make it so that when the user types in there IDE they dont get red errors for the globals they defined and the functions and objects that exist outside in the system code.

This is all pretty high level, im not looking for exact code to type to solve it all (of course would be nice :) ) but more looking for pointers, ideas, and paths i should research. Any help is highly appreciated!

3
  • "make it more modular so others can code there own 'strategy's' using system objects" - do you mean within the same application? Or should they take the "system code" file, as a library, and copy it into their own applications? Commented Jan 15, 2022 at 11:09
  • Which file is supposed to be the entry point of the system? Commented Jan 15, 2022 at 11:10
  • The goal is to have system files that are written once and are global. The user can write there own code using files imported from system code and also there code can be exported to be used in system file. The entry point is the user coded file. In the end the user should import something which will give him objects and functions. They should also be able to write code that will be exported and ran inside a for loop somewhere else. to run it would just be "node userFile.js" Commented Jan 15, 2022 at 11:22

1 Answer 1

1

Don't export something from the user file that the system file would import. Make use of dependency inversion and only have the user code depend on the system code. Since the user code is the entry point of the application anyway, that's easy.

In the system code file, export a function that takes the strategy as a parameter and runs the main loop. No need to "import" anything.

// system.js
import … from 'environment';

/* declarations */
export function utility() { … }

/* the loop */
export function run(strategy) {
  let … // more declarations
  for (…) {
    …
    step = strategy(newValues)
    …
  }
  console.log(results);
}
// userFile.js
import { utility, run } from 'system';

let … // globals
run((…) => {
  … // use globals, loop arguments and imported utilities
});
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks so much Bergi!! This looks complete but i have 3 questions on it: - why (…) inside run (in the userFile.js)? Is this if i wanted to pass params? and if so are they params for system.js? - what if i declare an object in system.js then use it in strategy and change a keys value, wouldnt it get erased to the default every time strategy is run in the for loop? - I dont quite understand this line "step = strategy(newValues)" what is "newValues"? I assume they are related to the params inside run. Again you went above and beyone by typing all this and really appreciate all the help!
Yes, the parameters, newValues arguments and step return value are for passing data back and forth between the run loop and the strategy implementation. If you could edit your question to include the contents of the 1 file that you currently have (or even better 2 files of similar applications that make it clear which parts of the code are duplicated and which parts are strategy-specific), I could show a more concrete example code.
And yes, you could also use a global object that is created in system.js and mutated by both parties, but keeping state locally in the run function (in local variables, passing them to the strategy implementation explicitly) allows you to potentially call run multiple times independently. And if they need to be set up differently between the applications, just pass extra arguments into run besides the strategy!
Seems perfect thanks so much!!

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.