This repository has been archived by the owner on Dec 19, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
fluxcontroller.js
1270 lines (1089 loc) · 45.6 KB
/
fluxcontroller.js
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2014 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
define(function (require, exports, module) {
"use strict";
var Fluxxor = require("fluxxor"),
Promise = require("bluebird"),
EventEmitter = require("eventEmitter"),
_ = require("lodash");
var ps = require("adapter").ps,
util = require("adapter").util;
var locks = require("./locks"),
events = require("./events"),
AsyncDependencyQueue = require("./util/async-dependency-queue"),
synchronization = require("./util/synchronization"),
performance = require("./util/performance"),
objUtil = require("./util/object"),
log = require("./util/log");
// Using contexts, we load action files automatically
var storeContext = require.context("./stores", true, /^\.\/.*\.js$/),
// Do not compile the "debug" module in production mode.
actionContext = require.context("./actions", true, __PG_DEBUG__ ? /^\.\/.*\.js$/ : /^\.\/((?!debug).)*\.js$/),
actionIndex = actionContext.keys().reduce(function (actionMap, actionKey) {
// "tool/superselect/type.js => tool.superselect.type"
var actionId = actionKey.substring(2, actionKey.indexOf(".js")).replace(/\//gi, ".");
actionMap[actionId] = actionContext(actionKey);
return actionMap;
}, {});
/**
* The number of logical CPU cores, used to determine the maximum number of
* concurrently executing actions.
*
* @const
* @type {number}
*/
var CORES = window.navigator.hardwareConcurrency || 8;
/**
* Suffix used to name throttled actions.
*
* @const
* @type {string}
*/
var THROTTLED_ACTION_SUFFIX = "Throttled";
/**
* Maximum delay after which reset retry will continue
* before failing definitively.
*
* @const
* @type {number}
*/
var MAX_RETRY_WINDOW = 6400;
/**
* Highlight actions in the console that take longer than the specific time (ms) to complete.
*
* @const
* @type {number}
*/
var SLOW_ACTION = 100;
/**
* Priority order comparator for action modules.
*
* @private
* @param {string} moduleName1
* @param {string} moduleName2
* @return {number}
*/
var _actionModuleComparator = function (moduleName1, moduleName2) {
var module1 = actionIndex[moduleName1],
module2 = actionIndex[moduleName2],
priority1 = module1._priority || 0,
priority2 = module2._priority || 0;
// sort modules in descending priority order
return priority2 - priority1;
};
/**
* Manages the lifecycle of a Fluxxor instance.
*
* @constructor
*/
var FluxController = function (testStores) {
EventEmitter.call(this);
this._actionQueue = new AsyncDependencyQueue(CORES);
this._initActionNames();
this._initActionLocks();
this._synchronizedActions = new Map();
this._idleTasks = new Set();
var actions = this._synchronizeAllModules(actionIndex),
stores = this._initializeAllStores(storeContext),
allStores = _.merge(stores, testStores || {});
this._flux = new Fluxxor.Flux(allStores, actions);
this._resetHelper = synchronization.throttle(this._resetWithDelay, this);
this._actionReceivers = this._createActionReceivers(this._flux);
};
util.inherits(FluxController, EventEmitter);
/**
* The main Fluxxor instance.
* @private
* @type {?Fluxxor.Flux}
*/
FluxController.prototype._flux = null;
/**
* Whether the flux instance is running
* @private
* @type {boolean}
*/
FluxController.prototype._running = false;
/**
* Used to synchronize flux action execution
*
* @private
* @type {ActionQueue}
*/
FluxController.prototype._actionQueue = null;
/**
* Map from unsynchronized action functions to pathnames.
*
* @private
* @type {Map.<function, string>}
*/
FluxController.prototype._actionNames = null;
/**
* Map from pathnames to unsynchronized action functions.
*
* @private
* @type {Map.<function, string>}
*/
FluxController.prototype._actionsByName = null;
/**
* Map from unsynchronized action functions to their transitive read lock set.
*
* @private
* @type {Map.<function, Set.<string>>}
*/
FluxController.prototype._transitiveReads = null;
/**
* Map from unsynchronized action functions to their transitive write lock set.
*
* @private
* @type {Map.<function, Set.<string>>}
*/
FluxController.prototype._transitiveWrites = null;
/**
* Per-action cache of action receivers
*
* @private
* @type {Map.<Action, ActionReceiver>}
*/
FluxController.prototype._actionReceivers = null;
/**
* Map from unsynchronized to synchronized actions.
*
* @private
* @type {Map.<Action, ActionReceiver>}
*/
FluxController.prototype._synchronizedActions = null;
/**
* Indicates whether or not the UI is currently locked.
*
* @private
* @type {boolean}
*/
FluxController.prototype._uiLocked = false;
/**
* Indicates whether or not failures in actions should
* be silenced
*
* @private
* @type {boolean}
*/
FluxController.prototype._allowFailure = false;
/**
* The set of pending idle task promises.
*
* @private
* @type {Set.<Promise>}
*/
FluxController.prototype._idleTasks = null;
Object.defineProperties(FluxController.prototype, {
"flux": {
enumerable: true,
get: function () {
return this._flux;
}
},
"active": {
enumerable: true,
get: function () {
return this._running && !this._resetPending;
}
}
});
/**
* Initialize maps to and from unsynchronized action functions and action pathnames.
*
* @private
*/
FluxController.prototype._initActionNames = function () {
this._actionsByName = new Map();
this._actionNames = new Map();
Object.keys(actionIndex).forEach(function (actionModuleName) {
var actionModule = actionIndex[actionModuleName];
Object.keys(actionModule)
.filter(function (actionName) {
return actionName[0] !== "_";
})
.forEach(function (actionName) {
var action = actionModule[actionName],
actionPath = actionModuleName + "." + actionName;
this._actionsByName.set(actionPath, action);
this._actionNames.set(action, actionPath);
}, this);
}, this);
};
/**
* The complete set of valid properties for action objects. An error is thrown
* if an action contains a property not in this set.
*
* @type {Array.<number>}
*/
const ACTION_OBJECT_PROPERTIES = new Set([
"reads",
"writes",
"modal",
"transfers",
"allowFailure",
"post",
"lockUI",
"hideOverlays"
]);
/**
* Calculate the (transitive) set of locks required to execute each action
* based on its immediate lock requirements and its declared action transfers.
*
* @private
*/
FluxController.prototype._initActionLocks = function () {
var actionDependencies = new Map();
var _resolveDependencies = function (action, stack) {
stack = stack || new Set();
// Check the stack to prevent circular dependency between actions (e.g. A -> B -> C -> A).
if (stack.has(action)) {
return new Set();
}
stack.add(action);
// Map from an action to the set of all unsynchonrized actions to
// which it may transfer, including via sub-action transfers.
var dependencies = actionDependencies.get(action),
actionObject = action.action;
if (!dependencies) {
dependencies = new Set([action]);
if (actionObject) {
var actionName = this._actionNames.get(action);
// Assert validity of all action properties
Object.keys(actionObject).forEach(function (key) {
if (!ACTION_OBJECT_PROPERTIES.has(key)) {
throw new Error("Unexpected property " + key + " of action " + actionName);
}
});
if (actionObject.transfers) {
actionObject.transfers.forEach(function (dependency, index) {
// Translate action pathnames to unsynchronized action functions
if (typeof dependency === "string") {
dependency = this._actionsByName.get(dependency);
}
// Validate transfer declarations
if (!dependency) {
throw new Error("Transfer declaration " + index + " of " + actionName + " is invalid.");
}
// Resolve child dependencies before proceeding
_resolveDependencies(dependency, stack).forEach(dependencies.add, dependencies);
}, this);
}
actionDependencies.set(action, dependencies);
var reads = actionObject.reads || locks.ALL_LOCKS,
writes = actionObject.writes || locks.ALL_LOCKS;
// Assert uniqueness of read locks
var uniqueReads = _.uniq(reads);
if (reads.length !== uniqueReads.length) {
throw new Error("Redundant read lock specified for " + actionName);
} else {
reads = uniqueReads;
}
// Assert uniqueness of read locks
var uniqueWrites = _.uniq(writes);
if (writes.length !== uniqueWrites.length) {
throw new Error("Redundant write lock specified for " + actionName);
} else {
writes = uniqueWrites;
}
// Calculate transitive lock sets based on the action's dependencies
dependencies.forEach(function (dependency) {
if (dependency.action) {
reads = reads.concat(dependency.action.reads || locks.ALL_LOCKS);
writes = writes.concat(dependency.action.writes || locks.ALL_LOCKS);
} else {
reads = reads.concat(locks.ALL_LOCKS);
writes = writes.concat(locks.ALL_LOCKS);
}
});
this._transitiveReads.set(action, _.uniq(reads));
this._transitiveWrites.set(action, _.uniq(writes));
}
}
stack.delete(action);
return dependencies;
}.bind(this);
this._transitiveReads = new Map();
this._transitiveWrites = new Map();
this._actionsByName.forEach(function (action, actionName) {
var actionObject = action.action;
// Validate action read locks
if (actionObject) {
if (actionObject.reads) {
actionObject.reads.forEach(function (lock, index) {
if (typeof lock !== "string") {
throw new Error("Read lock declaration " + index + " of " + actionName + " is invalid.");
}
});
}
// Validate action write locks
if (actionObject.writes) {
actionObject.writes.forEach(function (lock, index) {
if (typeof lock !== "string") {
throw new Error("Write lock declaration " + index + " of " + actionName + " is invalid.");
}
});
}
}
_resolveDependencies(action);
});
};
/**
* Create a map from all unsynchronized action functions to their action receivers.
*
* @private
* @param {Fluxxor.Flux} flux
* @return {Map.<function, ActionReceiver>}
*/
FluxController.prototype._createActionReceivers = function (flux) {
var dispatchBinder = flux.dispatchBinder,
actionReceivers = new Map();
Object.keys(actionIndex).forEach(function (actionModuleName) {
var actionModule = actionIndex[actionModuleName];
Object.keys(actionModule)
.filter(function (actionName) {
return actionName[0] !== "_";
})
.forEach(function (actionName) {
var action = actionModule[actionName];
if (typeof action !== "function") {
return;
}
var name = actionModuleName + "." + actionName,
receiver = this._makeActionReceiver(dispatchBinder, action, name);
actionReceivers.set(action, receiver);
}, this);
}, this);
return actionReceivers;
};
/**
* Call each action receiver's _reset method, which clears its transfer
* queue. This is called when an action transfer fails.
*
* @private
*/
FluxController.prototype._resetActionReceivers = function () {
this._actionReceivers.forEach(function (receiver) {
// Reset the receiver, clearing it's transfer queue
receiver._reset();
}, this);
};
/**
* Construct a receiver for the given action that augments the standard
* Fluxxor "dispatch binder" with additional action-specific helper methods.
*
* @private
* @param {object} proto Fluxxor dispatch binder
* @param {Action} action Action definition
* @param {string} actionName The fully qualified action name (i.e., "module.action")
* @return {ActionReceiver}
*/
FluxController.prototype._makeActionReceiver = function (proto, action, actionName) {
var actionObject = action.action;
if (actionObject && !actionObject.writes) {
log.warn("Action " + actionName + " does not specify any write locks. " +
"All locks will be required for execution.");
}
var actionQueue = this._actionQueue,
transferQueue = new AsyncDependencyQueue(CORES),
currentTransfers = new Set(actionObject.transfers || []),
self = this,
resolvedPromise;
var receiver = Object.create(proto, {
/**
* Provides direct controller access to actions
* @type {FluxController}
*/
controller: {
value: self
},
/**
* Reset the action receiver. Clears all jobs from the transfer queue.
*
* @private
*/
_reset: {
value: function () {
transferQueue.removeAll();
}
},
/**
* Safely transfer control from this action to another action, confirming
* that that action doesn't require additional locks, and preserving the
* receiver of that action.
*
* @param {string|Action} nextAction
* @return {Promise} The result of executing the next action
*/
transfer: {
value: function (nextAction) {
var nextActionName;
if (typeof nextAction === "string") {
nextActionName = nextAction;
nextAction = self._actionsByName.get(nextActionName);
} else {
nextActionName = self._actionNames.get(nextAction);
}
if (!nextAction || (typeof nextAction !== "function")) {
throw new Error("Transfer passed an undefined action");
}
if (!currentTransfers.has(nextAction) && !currentTransfers.has(nextActionName)) {
var message = "Invalid transfer from " + actionName + " to " + nextActionName +
". Add " + nextActionName + " to the list of transfers declared for " +
actionName + ".";
throw new Error(message);
}
var params = Array.prototype.slice.call(arguments, 1),
nextReceiver = self._actionReceivers.get(nextAction),
reads = self._transitiveReads.get(nextAction),
writes = self._transitiveWrites.get(nextAction),
logTransfers = __PG_DEBUG__ &&
this.flux.store("preferences").getState().get("logActionTransfers"),
enqueued;
if (logTransfers) {
enqueued = Date.now();
log.debug("Enqueuing transfer from %s to %s; %d/%d",
actionName, nextActionName,
transferQueue.active(), transferQueue.pending());
}
return transferQueue.push(function () {
var start;
if (logTransfers) {
start = Date.now();
log.debug("Executing transfer from %s to %s after waiting %dms; %d/%d",
actionName, nextActionName,
start - enqueued,
transferQueue.active(), transferQueue.pending());
log.timeStamp("Executing transfer from " + actionName + " to " + nextActionName);
}
return self._applyAction(nextAction, nextReceiver, params, actionName)
.tap(function () {
if (logTransfers) {
var finished = Date.now(),
elapsed = finished - start,
total = finished - enqueued,
color = elapsed > SLOW_ACTION ? "color:red" : "";
log.debug("Finished transfer from %s to %s in %c%dms %cwith RTT %dms; %d/%d",
actionName, nextActionName,
color, elapsed, "color:blue",
total, transferQueue.active(), transferQueue.pending());
log.timeStamp("Finished transfer from " + actionName + " to " + nextActionName);
}
})
.catch(function (err) {
var message = "Transfer from " + actionName + " to " + nextActionName + " failed:",
errMessage = err instanceof Error ? (err.stack || err.message) : err;
log.error(message, errMessage);
// Failed transfers trigger a controller reset (unless parent had allowFailure set)
this._resetController(err);
throw err;
});
}.bind(self), reads, writes);
}
},
/**
* Dispatch an event using the Flux dispatcher on the next tick of the event loop.
*
* @param {string} event
* @param {object=} payload
* @return {Promise} Resolves immediately
*/
dispatchAsync: {
value: function (event, payload) {
return resolvedPromise.then(function () {
this.dispatch(event, payload);
});
}
},
/**
* Enqueue an action for execution. Calling this.enqueue(foo.bar, baz, quux)
* is equivalent to calling this.flux.actions.foo.bar(baz, quux).
*
* @param {string|Action} nextAction
* @return {Promise}
*/
enqueue: {
value: function (nextAction) {
var nextActionName;
if (typeof nextAction === "string") {
nextActionName = nextAction;
nextAction = self._actionsByName.get(nextActionName);
} else {
nextActionName = self._actionNames.get(nextAction);
}
if (!nextAction || (typeof nextAction !== "function")) {
throw new Error("Exec passed an undefined action");
}
var params = Array.prototype.slice.call(arguments, 1),
synchronizedAction = self._synchronizedActions.get(nextAction);
return synchronizedAction.apply(null, params);
}
},
/**
* When the action queue and the JavaScript engine are idle, enqueue an action for execution.
*
* @param {string|Action} nextAction
* @return {Promise}
*/
whenIdle: {
value: function () {
var params = Array.prototype.slice.call(arguments, 0);
var idleTaskPromise = new Promise(function (resolve, reject, onCancel) {
var timer,
idle;
// When the queue becomes active, clear the timers and wait for it
// to become idle again.
var handleActive = function () {
if (timer) {
window.clearTimeout(timer);
timer = null;
}
if (idle) {
window.cancelIdleCallback(idle);
idle = null;
}
// Wait for the queue to become idle again
actionQueue.once("idle", handleIdle);
};
// When the queue become idle, wait for one second to ensure that it
// stays idle, and then wait for the JavaScript engine to become idle
// before finally enqueing the action and removing the queue-
// activation listener.
var handleIdle = function () {
// Wait for the queue to quiesce for one second.
timer = window.setTimeout(function () {
// Wait for the JavaScript engine to become idle.
idle = window.requestIdleCallback(function () {
// Clean up the queue-activation handler
actionQueue.off("active", handleActive);
timer = null;
idle = null;
// Enqueue the task
this.enqueue.apply(this, params)
.then(resolve, reject);
}.bind(this));
}.bind(this), 1000);
// Start over if the queue becomes active.
actionQueue.once("active", handleActive);
}.bind(this);
// Directly handle either the idle or active case depending on the
// current queue state.
if (actionQueue.isIdle()) {
handleIdle();
} else {
handleActive();
}
// If the controller is reset before the task has been executed, the
// promise will be canceled, and we should clean up any dangling timers
// or event handlers.
onCancel(function () {
window.clearTimeout(timer);
window.cancelIdleCallback(idle);
actionQueue.off("idle", handleIdle);
actionQueue.off("active", handleActive);
});
}.bind(this));
// Add to the current set of idle tasks so that it can be canceled if the
// controller is reset.
self._idleTasks.add(idleTaskPromise);
// Otherwise, remove it from the set of idle tasks once resolved.
idleTaskPromise.finally(function () {
self._idleTasks.delete(idleTaskPromise);
});
return idleTaskPromise;
}
}
});
resolvedPromise = Promise.bind(receiver);
return receiver;
};
/**
* Get an action receiver for the given action, creating it if necessary.
*
* @param {{flux: Flux, dispatch: function}} proto Fluxxor "dispatch binder",
* which is used as the prototype for the action receiver.
* @param {Action} action
* @return {ActionReceiver}
*/
FluxController.prototype._getActionReceiver = function (proto, action, actionName) {
var receiver = this._actionReceivers.get(action);
if (!receiver) {
receiver = this._makeActionReceiver(proto, action, actionName);
this._actionReceivers.set(action, receiver);
}
return receiver;
};
/**
* Lock the UI.
*
* @private
*/
FluxController.prototype._lockUI = function () {
if (!this._uiLocked) {
this._uiLocked = true;
this.emit("lock");
}
};
/**
* Sets the allow failure flag
* While the flag is on, all errors will be quietly logged
* but not cause a reset
*
* @private
* @param {boolean} flag
*/
FluxController.prototype._setAllowFailure = function (flag) {
this._allowFailure = flag;
};
/**
* Unock the UI.
*
* @private
*/
FluxController.prototype._unlockUI = function () {
if (this._uiLocked) {
this._uiLocked = false;
this.emit("unlock");
}
};
/**
* Apply the given action, bound to the given action receiver, to
* the given actual parameters. Verifies any postconditions defined as part
* of the action.
*
* @param {Action} action
* @param {ActionReceiver} actionReceiver
* @param {Array.<*>} params
* @return {Promise}
*/
FluxController.prototype._applyAction = function (action, actionReceiver, params, parentActionName) {
var flux = this.flux,
actionObject = action.action,
actionName = this._actionNames.get(action),
logActions = __PG_DEBUG__ && this.flux.store("preferences").getState().get("logActions", true);
if (!actionObject) {
throw new Error("Action " + actionName + " is not a valid action");
}
var lockUI = actionObject.lockUI,
hideOverlays = actionObject.hideOverlays,
allowFailure = actionObject.allowFailure,
post = actionObject.post,
modal = actionObject.modal || false,
actionTitle;
if (parentActionName) {
actionTitle = "sub-action " + actionName + " of action " + parentActionName;
} else {
actionTitle = "action " + actionName;
}
if (logActions) {
log.timeStamp("Executing " + actionTitle);
}
if (hideOverlays) {
actionReceiver.dispatch(events.panel.START_CANVAS_UPDATE);
}
var uiWasLocked = this._uiLocked;
if (lockUI && !uiWasLocked) {
this._lockUI();
}
var failuresWereAllowed = this._allowFailure;
if (allowFailure && !failuresWereAllowed) {
this._setAllowFailure(allowFailure);
}
var modalPromise;
if (!modal && flux.store("tool").getModalToolState()) {
log.warn("Killing modal state for " + actionTitle);
modalPromise = ps.endModalToolState(true)
.catch(function () {
// If the modal state has already ended, quietly continue
});
} else {
modalPromise = Promise.resolve();
}
return modalPromise
.bind(this)
.then(function () {
var actionPromise = action.apply(actionReceiver, params);
if (!(actionPromise && typeof actionPromise.then === "function")) {
var valueError = new Error("Action " + actionName + " did not return a promise");
valueError.returnValue = actionPromise;
throw valueError;
}
return actionPromise;
})
.catch(function (err) {
if (!this._allowFailure) {
throw err;
}
})
.tap(function () {
if (hideOverlays) {
actionReceiver.dispatch(events.panel.END_CANVAS_UPDATE);
}
if (lockUI && !uiWasLocked) {
this._unlockUI();
}
if (allowFailure && !failuresWereAllowed) {
this._setAllowFailure(false);
}
if (logActions) {
log.timeStamp("Finished " + actionTitle);
}
if (__PG_DEBUG__ && post && post.length > 0 &&
flux.store("preferences").get("postConditionsEnabled")) {
var postStart = Date.now(),
postTitle = post.length + " postcondition" + (post.length > 1 ? "s" : "");
log.debug("Verifying " + postTitle + " for " + actionTitle);
var postPromises = post.map(function (conjunct, index) {
if (typeof conjunct === "string") {
conjunct = this._actionsByName.get(conjunct);
}
return conjunct.apply(this)
.catch(function (err) {
var errMessage = err && err.message || "no error message";
log.error("Verification of postcondition %s failed for %s - %s",
index, actionTitle, errMessage);
});
}, this);
return Promise.all(postPromises)
.then(function () {
var postElapsed = Date.now() - postStart;
log.debug("Verified " + postTitle + " for " + actionTitle +
" in " + postElapsed + "ms");
});
}
});
};
/**
* Given a promise-returning method, returns a synchronized function that
* enqueues an application of that method.
*
* @private
* @param {string} namespace
* @param {object} module
* @param {string} name The name of the function in the module
* @return {function(): Promise}
*/
FluxController.prototype._synchronize = function (namespace, module, name) {
var self = this,
actionQueue = this._actionQueue,
action = module[name],
actionName = namespace + "." + name,
reads = this._transitiveReads.get(action),
writes = this._transitiveWrites.get(action);
return function () {
var args = Array.prototype.slice.call(arguments, 0),
logActions = __PG_DEBUG__ && self.flux.store("preferences").getState().get("logActions", true);
// The receiver of the action, augmented to include a transfer
// function that allows it to safely transfer control to another action
var actionReceiver = self._actionReceivers.get(action);
if (logActions) {
var enqueued = Date.now();
log.debug("Enqueuing action %s; %d/%d",
actionName, actionQueue.active(), actionQueue.pending());
}
var jobPromise = actionQueue.push(function () {
if (logActions) {
var start = Date.now();
log.debug("Executing action %s after waiting %dms; %d/%d",
actionName, start - enqueued, actionQueue.active(), actionQueue.pending());
}
return this._applyAction(action, actionReceiver, args)
.bind(this)
.tap(function () {
var finished = Date.now();
if (logActions) {
var elapsed = finished - start,
total = finished - enqueued,
color = elapsed > SLOW_ACTION ? "color:red" : "";
log.debug("Finished action %s in %c%dms %cwith RTT %dms; %d/%d", actionName, color, elapsed,
"color:blue", total, actionQueue.active(), actionQueue.pending());
}
if (__PG_DEBUG__) {
performance.recordAction(namespace, name, enqueued, start, finished);
}
})
.catch(function (err) {
var message = err instanceof Error ? (err.stack || err.message) : err;
log.error("Action " + actionName + " failed:", message);
// Reset all action modules on failure
this._resetController(err);
throw err;
});
}.bind(self), reads, writes);
return jobPromise;
};
};
/**
* Given a module name, gets the module in the action tree
*
* @param {string} moduleName dot separated path to the module
*
* @return {object} Synchronized module
*/
FluxController.prototype.getModule = function (moduleName) {
return objUtil.getPath(this._flux.actions, moduleName);
};
/**
* Given a module, returns a copy in which the methods have been synchronized.
*
* @private
* @param {string} namespace
* @param {object} module
* @return {object} The synchronized module
*/
FluxController.prototype._synchronizeModule = function (namespace, module) {
return Object.keys(module).reduce(function (exports, name) {
// Ignore underscore-prefixed exports
if (name[0] === "_") {
exports[name] = module[name];
return exports;