statemachine-adapter.js
9.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* ADOBE CONFIDENTIAL
*
* Copyright 2016 Adobe Systems Incorporated
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe Systems Incorporated and its suppliers,
* if any. The intellectual and technical concepts contained
* herein are proprietary to Adobe Systems Incorporated and its
* suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe Systems Incorporated.
*/
/* globals Promise */
define('screens/player/store/statemachine-adapter', [
'underscore',
'screens/player/shared/serviceadmin',
'screens/player/store/store'
], function(_, ServiceAdmin, Store) {
'use strict';
/**
* @memberof StateMachineAdapter
*/
var EVENTS = Object.freeze({
CHANGE: 'statemachine-change',
CLEAR: 'statemachine-clear'
});
/**
* @enum {StateMachineAdapter.LISTENER}
* @memberof StateMachineAdapter
* @readonly
*/
var LISTENER = Object.freeze({
/**
* entered the state
* @memberof StateMachineAdapter.LISTENER
*/
ENTER: '@enter',
/**
* exited the state
* @memberof StateMachineAdapter.LISTENER
*/
EXIT: '@exit'
});
/**
* Creates a new object based on a given dot-notated path
* @param {string} path in dot-notation
* @param {*} value the value to be set on the lowest level
* @private
* @returns {Object} a nested object with the set value
*/
function createNestedObject(path, value) {
return path.split('.').reverse().reduce(function(tree, prop) {
var obj = {};
obj[prop] = tree;
return obj;
}, value);
}
/**
* Statemachine factory to support a valid transition flow
* for states in the store
*
* @param {string} path full path in the state of the store
* @param {string} initialState the initial state which will be set during activation
* @param {Object} config state flow description and listeners
* @constructor
*/
var StateMachineAdapter = function(path, initialState, config) {
this._config = config;
var pathParts = path.split('.');
this._namespace = pathParts[0];
pathParts.shift();
this._path = pathParts.join('.');
this._initialState = initialState;
this._throttledTransitions = {};
if (!this._path.match(/^[\w.]+$/)) {
throw new Error('Invalid path');
}
function fetchFromObject(obj, prop) {
return prop.split('.').reduce(function(object, property) {
return object ? object[property] : null;
}, obj);
}
this._getStateValue = function(obj) {
try {
return fetchFromObject(obj, this._path);
}
catch (ex) {
return null;
}
};
this._isInitialized = false;
};
/**
* Listener options for state transitions
* @type {Object}
*/
StateMachineAdapter.LISTENER = LISTENER;
/**
* Dispatching events for the store
* @type {Object}
*/
StateMachineAdapter.EVENTS = EVENTS;
/**
* Initializes the configured state machine
* @returns {Promise} a promise that the state machine was initialized
*/
StateMachineAdapter.prototype.initialize = function() {
var self = this;
if (this._isInitialized) {
return Promise.reject('StateMachineAdapter already activated');
}
this._isInitialized = true;
return new Promise(function(resolve, reject) {
ServiceAdmin.onServiceHighestRankedStart(Store.serviceName, function(store) {
// add reducer
store.addReducer(self.reducer.bind(self), self._namespace);
// subscribe to changes
self._stateChangeListener = store.subscribe(self._onStateChange.bind(self), self._namespace);
// propagate initial state
self.transition();
resolve();
});
});
};
/**
* Tears down the state machine
* @returns {Promise} a promise that the state machine was destroyed
*/
StateMachineAdapter.prototype.destroy = function() {
var self = this;
return new Promise(function(resolve, reject) {
ServiceAdmin.onServiceHighestRankedStart(Store.serviceName, function(store) {
store.dispatch({
type: EVENTS.CLEAR,
payload: {
namespace: self._namespace,
path: self._path
}
});
store.removeReducer(self.reducer);
if (self._stateChangeListener) {
store.unsubscribe(self._stateChangeListener);
self._stateChangeListener = null;
}
self._throttledTransitions = {};
self._isInitialized = false;
resolve();
});
});
};
/**
* Looks up the possible transition states for the current state
* @param {string} currentState of the machine
* @returns {Object} the possible options for further transitions
*/
StateMachineAdapter.prototype.validNextStates = function(currentState) {
var ret = _.assign({}, this._config.states[currentState]);
delete ret[LISTENER.ENTER];
delete ret[LISTENER.EXIT];
return ret;
};
StateMachineAdapter.prototype._throttleTransition = function(nextState, interval) {
if (!_.isFunction(this._throttledTransitions[nextState])) {
this._throttledTransitions[nextState] = _.throttle(function() {
this._throttledTransitions[nextState] = null;
this.transition(nextState);
}.bind(this), interval, {
trailing: true,
leading: false
});
}
this._throttledTransitions[nextState]();
};
/**
* Requests a transition to a state
* @param {string} nextState the desired state
* @param {Number?} interval specifies the value by what we throttle
*/
StateMachineAdapter.prototype.transition = function(nextState, interval) {
if (_.isNumber(interval)) {
this._throttleTransition(nextState, interval);
return;
}
ServiceAdmin.getService(Store.serviceName).dispatch({
type: EVENTS.CHANGE,
payload: {
transitionTo: nextState,
namespace: this._namespace,
path: this._path
}
});
};
/**
* Reducer to handle transition changes
* @param {Object} state of the store
* @param {string} action the dispatched action
* @returns {object} state
*/
StateMachineAdapter.prototype.reducer = function(state, action) {
var data = action.payload;
if (action.type === EVENTS.CHANGE &&
data.namespace === this._namespace &&
data.path === this._path) {
var nextVal;
if (action.payload.transitionTo) {
var curVal = this._getStateValue(state);
if (this._config.states[curVal]) {
nextVal = this._config.states[curVal][action.payload.transitionTo];
nextVal = _.isString(nextVal) ? nextVal : nextVal && action.payload.transitionTo;
}
// @todo this ignores the state machine flow
// Due to refactoring this will remain until all modules
// properly describe the flow
if (!nextVal) {
nextVal = this._config.states[action.payload.transitionTo] && action.payload.transitionTo;
}
} else { // if no state given then use the initial state
nextVal = this._initialState;
}
if (nextVal) { // TODO: check that this is a valid state to transition to according to the config
var newValues = createNestedObject(this._path, nextVal);
return _.assign({}, state, newValues);
}
}
else if (action.type === EVENTS.CLEAR &&
data.namespace === this._namespace &&
data.path === this._path) {
return null;
}
return state || {};
};
/**
* Handler for state changes which triggers the state transition callbacks
* @param {Object} state of the store
* @param {Object} previousState of the store
* @private
*/
StateMachineAdapter.prototype._onStateChange = function(state, previousState) {
var prevVal = this._getStateValue(previousState);
var curVal = this._getStateValue(state);
if (this._config.states[prevVal]) {
var exitListener = this._config.states[prevVal][LISTENER.EXIT];
if (_.isFunction(exitListener)) {
exitListener(curVal);
}
}
if (this._config.states[prevVal] && _.isFunction(this._config.states[prevVal][curVal])) {
var listener = this._config.states[prevVal][curVal];
if (_.isFunction(listener)) {
listener(prevVal);
}
}
else if (this._config.states[curVal]) {
var enterListener = this._config.states[curVal][LISTENER.ENTER];
if (_.isFunction(enterListener)) {
enterListener(prevVal);
}
}
};
return StateMachineAdapter;
});