ifandelse / machina.js

js ex machina - finite state machines in JavaScript
http://machina-js.org/
Other
1.93k stars 147 forks source link

Suggestions for maintaining a large number of FSM instances and transition them via a REST API #119

Closed niravshah closed 8 years ago

niravshah commented 8 years ago

My application requires the creation of a large number of FSM and exposing them via a Express API. I am using the Behavioral FSM instance and hence my state machines are stored on individual domain objects. Does the FSM instance maintain a record of all the objects it has initiated?

What would be the suggested way to manage the large number of domain objects initiated with a state machine and get hold of instances to transition them via the Express API?

edwinallenz commented 8 years ago

@niravshah do you came with a pattern to handle this?

ifandelse commented 8 years ago

@niravshah @edwinallenz This slipped past me, sorry for missing it. Machina doesn't explicitly track all the FSMs created, however, there is a newFsm event you can listen for, so you could do something along these lines if you wanted to keep an object lookup of the FSM instances:

var lookup = {};

machina.on( "newFsm", function( fsm ) {
    lookup[ fsm.namespace ] = fsm;
} );

Just bear in mind that not pruning the lookup when you remove instances will most certainly result in memory leaks.

edwinallenz commented 8 years ago

thanks @ifandelse I'll give it a try