flams / synchronous-fsm

A synchronous FSM that triggers events when entering or leaving a state.
Other
1 stars 0 forks source link

callbacks scope #2

Open Sandreu opened 10 years ago

Sandreu commented 10 years ago

Hello again,

I got your scope logic with the third transition.add() parameter, but to put the fsm object context in scope, You have to add transition manually with add() after object declaration :

var fsm = new FSM(); fsm.add(['foo', function () { ... }, fsm, 'bar']); fsm.add(['bar', function () { ... }, fsm, 'foo']);

This is just a life hacking but perhaps the fsm object as default scope is nice... To keep the array init possible...?

podefr commented 10 years ago

Hey,

I'm not sure to understand. If specified, the scope (third parameter), will determine the value of thiswhen the callback (second parameter) is executed. You'd like this scope to be the state machine itself by default?

Sandreu commented 10 years ago

Yes that what I mean :) Even if my code is a shortcut because I skiped the transition object

Sandreu commented 10 years ago

It would be more like :

var fsm = new FSM(); var t; t = fsm.add('foo'): t.add('e1', function () { this.advance('bar'); }, fsm); t.add('e2', function () { ... }, fsm, 'bar'); t = fsm.add('bar'); t.add('e2', function () { ... }, fsm, 'foo');

Not cool ;)

Sandreu commented 10 years ago

With the enhancement become

var fsm = new FSM({ 'foo' :[ ['e1', function () { this.advance('bar'); }] ['e2', function () { ... }, 'bar' ] ], 'bar' : [ ['e2', function () { .... }, 'foo'] ]});

podefr commented 10 years ago

Yep, I'll do it an publish a new version