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?
loadPluginsreturn something, makecreateStoreInstanceaccept that same something. Probably the shared object can be the plugins.createStoreInstance:if (!plugins_loaded) { loadPlugins(); }getPlugin. And you pass that manager tocreateStoreInstance