0

I have two functions in my application startup sequence:

// Function A
async function loadPlugins() {
  // loads plugin definitions
}

// Function B
async function createStoreInstance() {
  const initialState = {
    // the initial state depends on a value from getPlugin()
    featureFlag: await getPlugin("somePlugin").isEnabled,
  };

  return configureStore({ initialState });
}

The store’s initial values require getPlugin(...), which only works if loadPlugins() has already been called. So the application always calls them in order:

await loadPlugins();
await createStoreInstance();

Right now, this order dependency (or temporal coupling) is only documented in comments:

// Must call loadPlugins() before createStoreInstance()

When writing test code, if someone tries to use createStoreInstance() directly without calling loadPlugins() first, it will fail. But a new contributor might not realize this dependency, since it’s not obvious just by looking at the createStoreInstance function.

Is there a better way to express or enforce this dependency without just relying on comments? For example, should createStoreInstance somehow require proof that loadPlugins() has been run, or should this be modeled differently?

3
  • 3
    Make loadPlugins return something, make createStoreInstance accept that same something. Probably the shared object can be the plugins. Commented Aug 17 at 10:12
  • you can also test at the start of createStoreInstance : if (!plugins_loaded) { loadPlugins(); } Commented Aug 17 at 10:46
  • I agree with @VLAZ, you need to return something like a plugin manager that also should have that function getPlugin. And you pass that manager to createStoreInstance Commented Aug 17 at 13:28

0

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.