Closed kkirby closed 6 years ago
having to run that check 200 times on a single event is a bit intense and unnecessary logic
IMO, that's not what the current API is intending. Normally, you would attach only one listener for all FSMs. Not one listener per FSM. In case you need to treat the different FSMs differently, you would then need to have an if/else or switch statement in the listener. But that seems like a code smell because if the FSMs are of the same type (i.e. same BehavioralFSM) then it should not be necessary to treat them differently. Or the listener could simply call a method on the client and each client could implement that method as appropriate. That would be more object-oriented. For example:
var myBehavioralFsm = new machine.BehavioralFsm({
// ...
});
myBehavioralFsm.on('transition', function(event) {
event.client.onTransition();
});
@kkirby Hey - just wanted to drop you a note to say I plan to follow up with a reply, just been a crazy week! Once I get a second to actually sit down and think I'll touch base! :-)
Aaaand 2 years just flew by in an instant. I wish I could pretend I'm a time traveler, but alas, #146 explains that's not the case. 😺
For our future fellow devs that find themselves with similar questions, my current recommendation is along the lines of what @j-ulrich suggested: attach one listener to the behavioral FSM and then you can control the responsibilities from there. I'm not opposed to revisiting this issue in the future - but some real world use cases would be super helpful!
Hello,
I had a question about how
BehavioralFsm
emits events. I was reading the code and saw that all events from any client are emitted on theBehavioralFsm
instance (which makes sense). But let's say I have 200 instances of a class that utilize aBehavioralFsm
, and I want to listen totransition
events. Whenever any statemachine transitions, all listeners will be called even if they don't care so much because they aren't concerned with theFsm
that transitioned. I know you could do an if check on theclient
event argument property to see if it is equal tothis
, but having to run that check 200 times on a single event is a bit intense and unnecessary logic. Is there anyway around this? Perhaps a method signature likeon(client,event)
? I'm pretty sure it doesn't exist, but wouldmachina.js
give open arms to a pull request that implemented this functionality? If so, are there any gotchas I should know about before implementing such functionality?I would was thinking of implementing it using a
WeakMap
and when anemitEvent
was called, the client would be looked up in theWeakMap
to determine the listeners.