I'm writing a tool that takes some data manually input by the user every 10 minutes, and runs a set of calculations on each input. Plugin A provides one calculation, Plugin B provides another, and so on. Mostly these plugins are independent of one another, i.e. order doesn't matter, because each plugin's calculation returns an integer that gets summed with the other plugins' integers.
But suppose now, I do have a Plugin C that depends on, say, whether Plugin A's return was non-zero. Data-wise, let's say I know how to make Plugin A's states available to Plugin C. (If it were C++, I'd make Plugin A a friend of Plugin C, for example. However, I'm writing this in Javascript, so I may take a looser approach.) My question is more about the pattern for ordering / dependence. How do I ensure that Plugin A's calculation runs before Plugin C's?
Of course, the simplest approach is to simplly "install" the plugins in the order they need to run, i.e. insert the plugins in the right order into an array so the loop looping through said array doesn't need to think.
But this may become fragile as I add more and more plugins (upwards of 20, maybe 30, depending on the scenario). I'd like something more robust.
The best idea I have right now is:
- On "installing" a plugin, I supply an array of plugins it depends on.
- Each plugin will have a static member, say,
_complete, that indicates whether it was run, and gets reset on every new iteration (user input). - As I loop through each plugin, I check each plugin's dependency's
_completestates; and if one isn't complete, then I don't run the calculation yet; the loop will be awhile-loop that comes back to retry this plugin after attempting all the others. I'll also have a maximum-retries guard to prevent infinite loops.
How can this be improved?
Aucun commentaire:
Enregistrer un commentaire