machina 4.0.2 → 6.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +198 -415
- package/dist/index.d.mts +628 -0
- package/dist/index.d.ts +628 -0
- package/dist/index.js +656 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +651 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +28 -149
- package/changelog.md +0 -190
- package/lib/machina.js +0 -744
- package/lib/machina.min.js +0 -2
- package/lib/machina.min.js.map +0 -1
- package/lib/machina_OLD.js +0 -612
package/lib/machina_OLD.js
DELETED
|
@@ -1,612 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* machina - A library for creating powerful and flexible finite state machines. Loosely inspired by Erlang/OTP's gen_fsm behavior.
|
|
3
|
-
* Author: Jim Cowart (http://ifandelse.com)
|
|
4
|
-
* Version: v1.1.2
|
|
5
|
-
* Url: http://machina-js.org/
|
|
6
|
-
* License(s): MIT, GPL
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
( function( root, factory ) {
|
|
11
|
-
/* istanbul ignore if */
|
|
12
|
-
if ( typeof define === "function" && define.amd ) {
|
|
13
|
-
// AMD. Register as an anonymous module.
|
|
14
|
-
define( [ "lodash" ], function( _ ) {
|
|
15
|
-
return factory( _, root );
|
|
16
|
-
} );
|
|
17
|
-
/* istanbul ignore else */
|
|
18
|
-
} else if ( typeof module === "object" && module.exports ) {
|
|
19
|
-
// Node, or CommonJS-Like environments
|
|
20
|
-
module.exports = factory( require( "lodash" ) );
|
|
21
|
-
} else {
|
|
22
|
-
// Browser globals
|
|
23
|
-
root.machina = factory( root._, root );
|
|
24
|
-
}
|
|
25
|
-
}( this, function( _, global, undefined ) {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
var slice = [].slice;
|
|
29
|
-
var NEXT_TRANSITION = "transition";
|
|
30
|
-
var HANDLING = "handling";
|
|
31
|
-
var HANDLED = "handled";
|
|
32
|
-
var NO_HANDLER = "nohandler";
|
|
33
|
-
var TRANSITION = "transition";
|
|
34
|
-
var INVALID_STATE = "invalidstate";
|
|
35
|
-
var DEFERRED = "deferred";
|
|
36
|
-
var NEW_FSM = "newfsm";
|
|
37
|
-
|
|
38
|
-
function getDefaultBehavioralOptions() {
|
|
39
|
-
return {
|
|
40
|
-
initialState: "uninitialized",
|
|
41
|
-
eventListeners: {
|
|
42
|
-
"*": []
|
|
43
|
-
},
|
|
44
|
-
states: {},
|
|
45
|
-
namespace: utils.makeFsmNamespace(),
|
|
46
|
-
useSafeEmit: false,
|
|
47
|
-
hierarchy: {},
|
|
48
|
-
pendingDelegations: {}
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function getDefaultClientMeta() {
|
|
53
|
-
return {
|
|
54
|
-
inputQueue: [],
|
|
55
|
-
targetReplayState: "",
|
|
56
|
-
state: undefined,
|
|
57
|
-
priorState: undefined,
|
|
58
|
-
priorAction: "",
|
|
59
|
-
currentAction: "",
|
|
60
|
-
currentActionArgs: undefined,
|
|
61
|
-
inExitHandler: false
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function getLeaklessArgs( args, startIdx ) {
|
|
66
|
-
var result = [];
|
|
67
|
-
for ( var i = ( startIdx || 0 ); i < args.length; i++ ) {
|
|
68
|
-
result[ i ] = args[ i ];
|
|
69
|
-
}
|
|
70
|
-
return result;
|
|
71
|
-
}
|
|
72
|
-
/*
|
|
73
|
-
handle ->
|
|
74
|
-
child = stateObj._child && stateObj._child.instance;
|
|
75
|
-
|
|
76
|
-
transition ->
|
|
77
|
-
newStateObj._child = getChildFsmInstance( newStateObj._child );
|
|
78
|
-
*/
|
|
79
|
-
function getChildFsmInstance( config ) {
|
|
80
|
-
if ( !config ) {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
var childFsmDefinition = {};
|
|
84
|
-
if ( typeof config === "object" ) {
|
|
85
|
-
// is this a config object with a factory?
|
|
86
|
-
if ( config.factory ) {
|
|
87
|
-
childFsmDefinition = config;
|
|
88
|
-
} else {
|
|
89
|
-
// assuming this is a machina instance
|
|
90
|
-
childFsmDefinition.factory = function() {
|
|
91
|
-
return config;
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
} else if ( typeof config === "function" ) {
|
|
95
|
-
childFsmDefinition.factory = config;
|
|
96
|
-
}
|
|
97
|
-
childFsmDefinition.instance = childFsmDefinition.factory();
|
|
98
|
-
return childFsmDefinition;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function listenToChild( fsm, child ) {
|
|
102
|
-
return child.on( "*", function( eventName, data ) {
|
|
103
|
-
switch ( eventName ) {
|
|
104
|
-
case "nohandler":
|
|
105
|
-
if ( !data.ticket && !data.delegated && data.namespace !== fsm.namespace ) {
|
|
106
|
-
// Ok - we're dealing w/ a child handling input that should bubble up
|
|
107
|
-
data.args[ 1 ].bubbling = true;
|
|
108
|
-
}
|
|
109
|
-
// we do NOT bubble _reset inputs up to the parent
|
|
110
|
-
if ( data.inputType !== "_reset" ) {
|
|
111
|
-
fsm.handle.apply( fsm, data.args );
|
|
112
|
-
}
|
|
113
|
-
break;
|
|
114
|
-
case "handling" :
|
|
115
|
-
var ticket = data.ticket;
|
|
116
|
-
if ( ticket && fsm.pendingDelegations[ ticket ] ) {
|
|
117
|
-
delete fsm.pendingDelegations[ ticket ];
|
|
118
|
-
}
|
|
119
|
-
fsm.emit( eventName, data ); // possibly transform payload?
|
|
120
|
-
break;
|
|
121
|
-
default:
|
|
122
|
-
fsm.emit( eventName, data ); // possibly transform payload?
|
|
123
|
-
break;
|
|
124
|
-
}
|
|
125
|
-
} );
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
// _machKeys are members we want to track across the prototype chain of an extended FSM constructor
|
|
129
|
-
// Since we want to eventually merge the aggregate of those values onto the instance so that FSMs
|
|
130
|
-
// that share the same extended prototype won't share state *on* those prototypes.
|
|
131
|
-
var _machKeys = [ "states", "initialState" ];
|
|
132
|
-
var extend = function( protoProps, staticProps ) {
|
|
133
|
-
var parent = this;
|
|
134
|
-
var fsm; // placeholder for instance constructor
|
|
135
|
-
var machObj = {}; // object used to hold initialState & states from prototype for instance-level merging
|
|
136
|
-
var Ctor = function() {}; // placeholder ctor function used to insert level in prototype chain
|
|
137
|
-
|
|
138
|
-
// The constructor function for the new subclass is either defined by you
|
|
139
|
-
// (the "constructor" property in your `extend` definition), or defaulted
|
|
140
|
-
// by us to simply call the parent's constructor.
|
|
141
|
-
if ( protoProps && protoProps.hasOwnProperty( "constructor" ) ) {
|
|
142
|
-
fsm = protoProps.constructor;
|
|
143
|
-
} else {
|
|
144
|
-
// The default machina constructor (when using inheritance) creates a
|
|
145
|
-
// deep copy of the states/initialState values from the prototype and
|
|
146
|
-
// extends them over the instance so that they'll be instance-level.
|
|
147
|
-
// If an options arg (args[0]) is passed in, a states or intialState
|
|
148
|
-
// value will be preferred over any data pulled up from the prototype.
|
|
149
|
-
fsm = function() {
|
|
150
|
-
var args = slice.call( arguments, 0 );
|
|
151
|
-
args[ 0 ] = args[ 0 ] || {};
|
|
152
|
-
var blendedState;
|
|
153
|
-
var instanceStates = args[ 0 ].states || {};
|
|
154
|
-
blendedState = _.merge( _.cloneDeep( machObj ), { states: instanceStates } );
|
|
155
|
-
blendedState.initialState = args[ 0 ].initialState || this.initialState;
|
|
156
|
-
_.extend( args[ 0 ], blendedState );
|
|
157
|
-
parent.apply( this, args );
|
|
158
|
-
};
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// Inherit class (static) properties from parent.
|
|
162
|
-
_.merge( fsm, parent );
|
|
163
|
-
|
|
164
|
-
// Set the prototype chain to inherit from `parent`, without calling
|
|
165
|
-
// `parent`'s constructor function.
|
|
166
|
-
Ctor.prototype = parent.prototype;
|
|
167
|
-
fsm.prototype = new Ctor();
|
|
168
|
-
|
|
169
|
-
// Add prototype properties (instance properties) to the subclass,
|
|
170
|
-
// if supplied.
|
|
171
|
-
if ( protoProps ) {
|
|
172
|
-
_.extend( fsm.prototype, protoProps );
|
|
173
|
-
_.merge( machObj, _.transform( protoProps, function( accum, val, key ) {
|
|
174
|
-
if ( _machKeys.indexOf( key ) !== -1 ) {
|
|
175
|
-
accum[ key ] = val;
|
|
176
|
-
}
|
|
177
|
-
} ) );
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
// Add static properties to the constructor function, if supplied.
|
|
181
|
-
if ( staticProps ) {
|
|
182
|
-
_.merge( fsm, staticProps );
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// Correctly set child's `prototype.constructor`.
|
|
186
|
-
fsm.prototype.constructor = fsm;
|
|
187
|
-
|
|
188
|
-
// Set a convenience property in case the parent's prototype is needed later.
|
|
189
|
-
fsm.__super__ = parent.prototype;
|
|
190
|
-
return fsm;
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
function createUUID() {
|
|
194
|
-
var s = [];
|
|
195
|
-
var hexDigits = "0123456789abcdef";
|
|
196
|
-
for ( var i = 0; i < 36; i++ ) {
|
|
197
|
-
s[ i ] = hexDigits.substr( Math.floor( Math.random() * 0x10 ), 1 );
|
|
198
|
-
}
|
|
199
|
-
s[ 14 ] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
|
|
200
|
-
|
|
201
|
-
s[ 19 ] = hexDigits.substr( ( s[ 19 ] & 0x3 ) | 0x8, 1 ); // bits 6-7 of the clock_seq_hi_and_reserved to 01
|
|
202
|
-
|
|
203
|
-
s[ 8 ] = s[ 13 ] = s[ 18 ] = s[ 23 ] = "-";
|
|
204
|
-
return s.join( "" );
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
var utils = {
|
|
208
|
-
makeFsmNamespace: ( function() {
|
|
209
|
-
var machinaCount = 0;
|
|
210
|
-
return function() {
|
|
211
|
-
return "fsm." + machinaCount++;
|
|
212
|
-
};
|
|
213
|
-
} )(),
|
|
214
|
-
listenToChild: listenToChild,
|
|
215
|
-
getLeaklessArgs: getLeaklessArgs,
|
|
216
|
-
getDefaultOptions: getDefaultBehavioralOptions,
|
|
217
|
-
getDefaultClientMeta: getDefaultClientMeta,
|
|
218
|
-
createUUID: createUUID
|
|
219
|
-
};
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
var emitter = {
|
|
224
|
-
|
|
225
|
-
emit: function( eventName ) {
|
|
226
|
-
var args = getLeaklessArgs( arguments );
|
|
227
|
-
if ( this.eventListeners[ "*" ] ) {
|
|
228
|
-
_.each( this.eventListeners[ "*" ], function( callback ) {
|
|
229
|
-
if ( !this.useSafeEmit ) {
|
|
230
|
-
callback.apply( this, args );
|
|
231
|
-
} else {
|
|
232
|
-
try {
|
|
233
|
-
callback.apply( this, args );
|
|
234
|
-
} catch ( exception ) {
|
|
235
|
-
/* istanbul ignore else */
|
|
236
|
-
if ( console && typeof console.log !== "undefined" ) {
|
|
237
|
-
console.log( exception.stack );
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}, this );
|
|
242
|
-
}
|
|
243
|
-
if ( this.eventListeners[ eventName ] ) {
|
|
244
|
-
_.each( this.eventListeners[ eventName ], function( callback ) {
|
|
245
|
-
if ( !this.useSafeEmit ) {
|
|
246
|
-
callback.apply( this, args.slice( 1 ) );
|
|
247
|
-
} else {
|
|
248
|
-
try {
|
|
249
|
-
callback.apply( this, args.slice( 1 ) );
|
|
250
|
-
} catch ( exception ) {
|
|
251
|
-
/* istanbul ignore else */
|
|
252
|
-
if ( console && typeof console.log !== "undefined" ) {
|
|
253
|
-
console.log( exception.stack );
|
|
254
|
-
}
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}, this );
|
|
258
|
-
}
|
|
259
|
-
},
|
|
260
|
-
|
|
261
|
-
on: function( eventName, callback ) {
|
|
262
|
-
var self = this;
|
|
263
|
-
self.eventListeners = self.eventListeners || { "*": [] };
|
|
264
|
-
if ( !self.eventListeners[ eventName ] ) {
|
|
265
|
-
self.eventListeners[ eventName ] = [];
|
|
266
|
-
}
|
|
267
|
-
self.eventListeners[ eventName ].push( callback );
|
|
268
|
-
return {
|
|
269
|
-
eventName: eventName,
|
|
270
|
-
callback: callback,
|
|
271
|
-
off: function() {
|
|
272
|
-
self.off( eventName, callback );
|
|
273
|
-
}
|
|
274
|
-
};
|
|
275
|
-
},
|
|
276
|
-
|
|
277
|
-
off: function( eventName, callback ) {
|
|
278
|
-
this.eventListeners = this.eventListeners || { "*": [] };
|
|
279
|
-
if ( !eventName ) {
|
|
280
|
-
this.eventListeners = {};
|
|
281
|
-
} else {
|
|
282
|
-
if ( callback ) {
|
|
283
|
-
this.eventListeners[ eventName ] = _.without( this.eventListeners[ eventName ], callback );
|
|
284
|
-
} else {
|
|
285
|
-
this.eventListeners[ eventName ] = [];
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
};
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
var MACHINA_PROP = "__machina__";
|
|
293
|
-
|
|
294
|
-
function BehavioralFsm( options ) {
|
|
295
|
-
_.extend( this, options );
|
|
296
|
-
_.defaults( this, getDefaultBehavioralOptions() );
|
|
297
|
-
this.initialize.apply( this, arguments );
|
|
298
|
-
machina.emit( NEW_FSM, this );
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
_.extend( BehavioralFsm.prototype, {
|
|
302
|
-
initialize: function() {},
|
|
303
|
-
|
|
304
|
-
initClient: function initClient( client ) {
|
|
305
|
-
var initialState = this.initialState;
|
|
306
|
-
if ( !initialState ) {
|
|
307
|
-
throw new Error( "You must specify an initial state for this FSM" );
|
|
308
|
-
}
|
|
309
|
-
if ( !this.states[ initialState ] ) {
|
|
310
|
-
throw new Error( "The initial state specified does not exist in the states object." );
|
|
311
|
-
}
|
|
312
|
-
this.transition( client, initialState );
|
|
313
|
-
},
|
|
314
|
-
|
|
315
|
-
ensureClientMeta: function ensureClientMeta( client ) {
|
|
316
|
-
if ( typeof client !== "object" ) {
|
|
317
|
-
throw new Error( "An FSM client must be an object." );
|
|
318
|
-
}
|
|
319
|
-
client[ MACHINA_PROP ] = client[ MACHINA_PROP ] || {};
|
|
320
|
-
if ( !client[ MACHINA_PROP ][ this.namespace ] ) {
|
|
321
|
-
client[ MACHINA_PROP ][ this.namespace ] = _.cloneDeep( utils.getDefaultClientMeta() );
|
|
322
|
-
this.initClient( client );
|
|
323
|
-
}
|
|
324
|
-
return client[ MACHINA_PROP ][ this.namespace ];
|
|
325
|
-
},
|
|
326
|
-
|
|
327
|
-
buildEventPayload: function( client, data ) {
|
|
328
|
-
if ( _.isPlainObject( data ) ) {
|
|
329
|
-
return _.extend( data, { client: client, namespace: this.namespace } );
|
|
330
|
-
} else {
|
|
331
|
-
return { client: client, data: data || null, namespace: this.namespace };
|
|
332
|
-
}
|
|
333
|
-
},
|
|
334
|
-
|
|
335
|
-
getHandlerArgs: function( args, isCatchAll ) {
|
|
336
|
-
// index 0 is the client, index 1 is inputType
|
|
337
|
-
// if we're in a catch-all handler, input type needs to be included in the args
|
|
338
|
-
// inputType might be an object, so we need to just get the inputType string if so
|
|
339
|
-
var _args = args.slice( 0 );
|
|
340
|
-
var input = _args[ 1 ];
|
|
341
|
-
if ( typeof input === "object" ) {
|
|
342
|
-
_args.splice( 1, 1, input.inputType );
|
|
343
|
-
}
|
|
344
|
-
return isCatchAll ?
|
|
345
|
-
_args :
|
|
346
|
-
[ _args[ 0 ] ].concat( _args.slice( 2 ) );
|
|
347
|
-
},
|
|
348
|
-
|
|
349
|
-
handle: function( client, input ) {
|
|
350
|
-
var inputDef = input;
|
|
351
|
-
if ( typeof input === "undefined" ) {
|
|
352
|
-
throw new Error( "The input argument passed to the FSM's handle method is undefined. Did you forget to pass the input name?" );
|
|
353
|
-
}
|
|
354
|
-
if ( typeof input === "string" ) {
|
|
355
|
-
inputDef = { inputType: input, delegated: false, ticket: undefined };
|
|
356
|
-
}
|
|
357
|
-
var clientMeta = this.ensureClientMeta( client );
|
|
358
|
-
var args = getLeaklessArgs( arguments );
|
|
359
|
-
if ( typeof input !== "object" ) {
|
|
360
|
-
args.splice( 1, 1, inputDef );
|
|
361
|
-
}
|
|
362
|
-
clientMeta.currentActionArgs = args.slice( 1 );
|
|
363
|
-
var currentState = clientMeta.state;
|
|
364
|
-
var stateObj = this.states[ currentState ];
|
|
365
|
-
var handlerName;
|
|
366
|
-
var handler;
|
|
367
|
-
var isCatchAll = false;
|
|
368
|
-
var child;
|
|
369
|
-
var result;
|
|
370
|
-
var action;
|
|
371
|
-
if ( !clientMeta.inExitHandler ) {
|
|
372
|
-
child = stateObj._child && stateObj._child.instance;
|
|
373
|
-
if ( child && !this.pendingDelegations[ inputDef.ticket ] && !inputDef.bubbling ) {
|
|
374
|
-
inputDef.ticket = ( inputDef.ticket || utils.createUUID() );
|
|
375
|
-
inputDef.delegated = true;
|
|
376
|
-
this.pendingDelegations[ inputDef.ticket ] = { delegatedTo: child.namespace };
|
|
377
|
-
// WARNING - returning a value from `handle` on child FSMs is not really supported.
|
|
378
|
-
// If you need to return values from child FSM input handlers, use events instead.
|
|
379
|
-
result = child.handle.apply( child, args );
|
|
380
|
-
} else {
|
|
381
|
-
if ( inputDef.ticket && this.pendingDelegations[ inputDef.ticket ] ) {
|
|
382
|
-
delete this.pendingDelegations[ inputDef.ticket ];
|
|
383
|
-
}
|
|
384
|
-
handlerName = stateObj[ inputDef.inputType ] ? inputDef.inputType : "*";
|
|
385
|
-
isCatchAll = ( handlerName === "*" );
|
|
386
|
-
handler = ( stateObj[ handlerName ] || this[ handlerName ] ) || this[ "*" ];
|
|
387
|
-
action = clientMeta.state + "." + handlerName;
|
|
388
|
-
clientMeta.currentAction = action;
|
|
389
|
-
var eventPayload = this.buildEventPayload(
|
|
390
|
-
client,
|
|
391
|
-
{ inputType: inputDef.inputType, delegated: inputDef.delegated, ticket: inputDef.ticket }
|
|
392
|
-
);
|
|
393
|
-
if ( !handler ) {
|
|
394
|
-
this.emit( NO_HANDLER, _.extend( { args: args }, eventPayload ) );
|
|
395
|
-
} else {
|
|
396
|
-
this.emit( HANDLING, eventPayload );
|
|
397
|
-
if ( typeof handler === "function" ) {
|
|
398
|
-
result = handler.apply( this, this.getHandlerArgs( args, isCatchAll ) );
|
|
399
|
-
} else {
|
|
400
|
-
result = handler;
|
|
401
|
-
this.transition( client, handler );
|
|
402
|
-
}
|
|
403
|
-
this.emit( HANDLED, eventPayload );
|
|
404
|
-
}
|
|
405
|
-
clientMeta.priorAction = clientMeta.currentAction;
|
|
406
|
-
clientMeta.currentAction = "";
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
return result;
|
|
410
|
-
},
|
|
411
|
-
|
|
412
|
-
transition: function( client, newState ) {
|
|
413
|
-
var clientMeta = this.ensureClientMeta( client );
|
|
414
|
-
var curState = clientMeta.state;
|
|
415
|
-
var curStateObj = this.states[ curState ];
|
|
416
|
-
var newStateObj = this.states[ newState ];
|
|
417
|
-
var child;
|
|
418
|
-
if ( !clientMeta.inExitHandler && newState !== curState ) {
|
|
419
|
-
if ( newStateObj ) {
|
|
420
|
-
if ( newStateObj._child ) {
|
|
421
|
-
newStateObj._child = getChildFsmInstance( newStateObj._child );
|
|
422
|
-
child = newStateObj._child && newStateObj._child.instance;
|
|
423
|
-
}
|
|
424
|
-
if ( curStateObj && curStateObj._onExit ) {
|
|
425
|
-
clientMeta.inExitHandler = true;
|
|
426
|
-
curStateObj._onExit.call( this, client );
|
|
427
|
-
clientMeta.inExitHandler = false;
|
|
428
|
-
}
|
|
429
|
-
if ( curStateObj && curStateObj._child && curStateObj._child.instance && this.hierarchy[ curStateObj._child.instance.namespace ] ) {
|
|
430
|
-
this.hierarchy[ curStateObj._child.instance.namespace ].off();
|
|
431
|
-
}
|
|
432
|
-
clientMeta.targetReplayState = newState;
|
|
433
|
-
clientMeta.priorState = curState;
|
|
434
|
-
clientMeta.state = newState;
|
|
435
|
-
if ( child ) {
|
|
436
|
-
this.hierarchy[ child.namespace ] = utils.listenToChild( this, child );
|
|
437
|
-
}
|
|
438
|
-
var eventPayload = this.buildEventPayload( client, {
|
|
439
|
-
fromState: clientMeta.priorState,
|
|
440
|
-
action: clientMeta.currentAction,
|
|
441
|
-
toState: newState
|
|
442
|
-
} );
|
|
443
|
-
this.emit( TRANSITION, eventPayload );
|
|
444
|
-
if ( newStateObj._onEnter ) {
|
|
445
|
-
newStateObj._onEnter.call( this, client );
|
|
446
|
-
}
|
|
447
|
-
if ( child ) {
|
|
448
|
-
child.handle( client, "_reset" );
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
if ( clientMeta.targetReplayState === newState ) {
|
|
452
|
-
this.processQueue( client, NEXT_TRANSITION );
|
|
453
|
-
}
|
|
454
|
-
return;
|
|
455
|
-
}
|
|
456
|
-
this.emit( INVALID_STATE, this.buildEventPayload( client, {
|
|
457
|
-
state: clientMeta.state,
|
|
458
|
-
attemptedState: newState
|
|
459
|
-
} ) );
|
|
460
|
-
}
|
|
461
|
-
},
|
|
462
|
-
|
|
463
|
-
deferUntilTransition: function( client, stateName ) {
|
|
464
|
-
var clientMeta = this.ensureClientMeta( client );
|
|
465
|
-
if ( clientMeta.currentActionArgs ) {
|
|
466
|
-
var queued = {
|
|
467
|
-
type: NEXT_TRANSITION,
|
|
468
|
-
untilState: stateName,
|
|
469
|
-
args: clientMeta.currentActionArgs
|
|
470
|
-
};
|
|
471
|
-
clientMeta.inputQueue.push( queued );
|
|
472
|
-
var eventPayload = this.buildEventPayload( client, {
|
|
473
|
-
state: clientMeta.state,
|
|
474
|
-
queuedArgs: queued
|
|
475
|
-
} );
|
|
476
|
-
this.emit( DEFERRED, eventPayload );
|
|
477
|
-
}
|
|
478
|
-
},
|
|
479
|
-
|
|
480
|
-
deferAndTransition: function( client, stateName ) {
|
|
481
|
-
this.deferUntilTransition( client, stateName );
|
|
482
|
-
this.transition( client, stateName );
|
|
483
|
-
},
|
|
484
|
-
|
|
485
|
-
processQueue: function( client ) {
|
|
486
|
-
var clientMeta = this.ensureClientMeta( client );
|
|
487
|
-
var filterFn = function( item ) {
|
|
488
|
-
return ( ( !item.untilState ) || ( item.untilState === clientMeta.state ) );
|
|
489
|
-
};
|
|
490
|
-
var toProcess = _.filter( clientMeta.inputQueue, filterFn );
|
|
491
|
-
clientMeta.inputQueue = _.difference( clientMeta.inputQueue, toProcess );
|
|
492
|
-
_.each( toProcess, function( item ) {
|
|
493
|
-
this.handle.apply( this, [ client ].concat( item.args ) );
|
|
494
|
-
}, this );
|
|
495
|
-
},
|
|
496
|
-
|
|
497
|
-
clearQueue: function( client, name ) {
|
|
498
|
-
var clientMeta = this.ensureClientMeta( client );
|
|
499
|
-
if ( !name ) {
|
|
500
|
-
clientMeta.inputQueue = [];
|
|
501
|
-
} else {
|
|
502
|
-
var filter = function( evnt ) {
|
|
503
|
-
return ( name ? evnt.untilState !== name : true );
|
|
504
|
-
};
|
|
505
|
-
clientMeta.inputQueue = _.filter( clientMeta.inputQueue, filter );
|
|
506
|
-
}
|
|
507
|
-
},
|
|
508
|
-
|
|
509
|
-
compositeState: function( client ) {
|
|
510
|
-
var clientMeta = this.ensureClientMeta( client );
|
|
511
|
-
var state = clientMeta.state;
|
|
512
|
-
var child = this.states[state]._child && this.states[state]._child.instance;
|
|
513
|
-
if ( child ) {
|
|
514
|
-
state += "." + child.compositeState( client );
|
|
515
|
-
}
|
|
516
|
-
return state;
|
|
517
|
-
}
|
|
518
|
-
}, emitter );
|
|
519
|
-
|
|
520
|
-
BehavioralFsm.extend = extend;
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
var Fsm = {
|
|
525
|
-
constructor: function() {
|
|
526
|
-
BehavioralFsm.apply( this, arguments );
|
|
527
|
-
this.ensureClientMeta();
|
|
528
|
-
},
|
|
529
|
-
initClient: function initClient() {
|
|
530
|
-
var initialState = this.initialState;
|
|
531
|
-
if ( !initialState ) {
|
|
532
|
-
throw new Error( "You must specify an initial state for this FSM" );
|
|
533
|
-
}
|
|
534
|
-
if ( !this.states[ initialState ] ) {
|
|
535
|
-
throw new Error( "The initial state specified does not exist in the states object." );
|
|
536
|
-
}
|
|
537
|
-
this.transition( initialState );
|
|
538
|
-
},
|
|
539
|
-
ensureClientMeta: function ensureClientMeta() {
|
|
540
|
-
if ( !this._stamped ) {
|
|
541
|
-
this._stamped = true;
|
|
542
|
-
_.defaults( this, _.cloneDeep( getDefaultClientMeta() ) );
|
|
543
|
-
this.initClient();
|
|
544
|
-
}
|
|
545
|
-
return this;
|
|
546
|
-
},
|
|
547
|
-
|
|
548
|
-
ensureClientArg: function( args ) {
|
|
549
|
-
var _args = args;
|
|
550
|
-
// we need to test the args and verify that if a client arg has
|
|
551
|
-
// been passed, it must be this FSM instance (this isn't a behavioral FSM)
|
|
552
|
-
if ( typeof _args[ 0 ] === "object" && !( "inputType" in _args[ 0 ] ) && _args[ 0 ] !== this ) {
|
|
553
|
-
_args.splice( 0, 1, this );
|
|
554
|
-
} else if ( typeof _args[ 0 ] !== "object" || ( typeof _args[ 0 ] === "object" && ( "inputType" in _args[ 0 ] ) ) ) {
|
|
555
|
-
_args.unshift( this );
|
|
556
|
-
}
|
|
557
|
-
return _args;
|
|
558
|
-
},
|
|
559
|
-
|
|
560
|
-
getHandlerArgs: function( args, isCatchAll ) {
|
|
561
|
-
// index 0 is the client, index 1 is inputType
|
|
562
|
-
// if we're in a catch-all handler, input type needs to be included in the args
|
|
563
|
-
// inputType might be an object, so we need to just get the inputType string if so
|
|
564
|
-
var _args = args;
|
|
565
|
-
var input = _args[ 1 ];
|
|
566
|
-
if ( typeof inputType === "object" ) {
|
|
567
|
-
_args.splice( 1, 1, input.inputType );
|
|
568
|
-
}
|
|
569
|
-
return isCatchAll ?
|
|
570
|
-
_args.slice( 1 ) :
|
|
571
|
-
_args.slice( 2 );
|
|
572
|
-
},
|
|
573
|
-
// "classic" machina FSM do not emit the client property on events (which would be the FSM itself)
|
|
574
|
-
buildEventPayload: function() {
|
|
575
|
-
var args = this.ensureClientArg( utils.getLeaklessArgs( arguments ) );
|
|
576
|
-
var data = args[ 1 ];
|
|
577
|
-
if ( _.isPlainObject( data ) ) {
|
|
578
|
-
return _.extend( data, { namespace: this.namespace } );
|
|
579
|
-
} else {
|
|
580
|
-
return { data: data || null, namespace: this.namespace };
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
};
|
|
584
|
-
|
|
585
|
-
_.each( [
|
|
586
|
-
"handle",
|
|
587
|
-
"transition",
|
|
588
|
-
"deferUntilTransition",
|
|
589
|
-
"processQueue",
|
|
590
|
-
"clearQueue"
|
|
591
|
-
], function( methodWithClientInjected ) {
|
|
592
|
-
Fsm[methodWithClientInjected] = function() {
|
|
593
|
-
var args = this.ensureClientArg( utils.getLeaklessArgs( arguments ) );
|
|
594
|
-
return BehavioralFsm.prototype[methodWithClientInjected].apply( this, args );
|
|
595
|
-
};
|
|
596
|
-
} );
|
|
597
|
-
|
|
598
|
-
Fsm = BehavioralFsm.extend( Fsm );
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
var machina = _.merge( emitter, {
|
|
603
|
-
Fsm: Fsm,
|
|
604
|
-
BehavioralFsm: BehavioralFsm,
|
|
605
|
-
utils: utils,
|
|
606
|
-
eventListeners: {
|
|
607
|
-
newFsm: []
|
|
608
|
-
}
|
|
609
|
-
} );
|
|
610
|
-
|
|
611
|
-
return machina;
|
|
612
|
-
} ) );
|