diff --git a/lib/state.js b/lib/state.js index d5ce5b7..19781f2 100644 --- a/lib/state.js +++ b/lib/state.js @@ -399,9 +399,15 @@ this.__transitions__ = []; this.trace = false; + // attach to __instances so external tools can access this instance + if (opts.track) { State.instances.push(this); } + if (f) { f.call(this); } } + // Public: exposes state instances that have been given `track: true` in `opt`. + State.instances = []; + // Public: Convenience method for creating a new statechart. Simply creates a // root state and invokes the given function in the context of that state. // diff --git a/spec/statechart_spec.js b/spec/statechart_spec.js index 36346a3..41e4277 100644 --- a/spec/statechart_spec.js +++ b/spec/statechart_spec.js @@ -62,6 +62,11 @@ describe('State constructor function', function() { s = new State('x', {H: true}, f); expect(context).toBe(s); }); + + it('should push the state onto `instances` via setting `track` to `true`', function() { + var s = new State('a', { track: true }); + expect(State.instances).toEqual([s]) + }); }); describe('State#addSubstate', function() {