Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ve.Scheduler.js

raw symbols SCIP
1 /*!
2 * VisualEditor Scheduler class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * @class
9 *
10 * @constructor
11 */
12 ve.Scheduler = function VeScheduler() {
13 // TODO: If we decide to start tracking setTimeout calls within actions, we'll
14 // need to keep state here.
15 };
16
17 /* Inheritance */
18
19 OO.initClass( ve.Scheduler );
20
21 /* Static Properties */
22
23 ve.Scheduler.static.maxDelay = 1000;
24
25 /* Methods */
26
27 /**
28 * Perform an action and await a callback when its side-effects are complete
29 *
30 * The ultimate definition of "side-effects are complete" is "when the chain of async
31 * actions / setTimeout calls spawned by the action finish". This is intended to be a way
32 * to wrap non-promise code in promises and have it mostly work.
33 *
34 * As currently implemented, we use completionTest as our sole signal. This is not
35 * guaranteed to remain true. Don't write code that assumes completionTest will be
36 * called, or which tests for a completely unrelated condition.
37 *
38 * The signature of this function is designed to let you leave signals about your intent.
39 * You pass the action with side-effects in, and explain the conditions that must be met
40 * for further actions to be taken.
41 *
42 * @param {Function} immediateAction Action to take whose status we want to track
43 * @param {Function} completionTest Tests whether action is complete; ideally very cheap;
44 * there's no guarantee that we will ever call this, if we can sense completion in
45 * some other way
46 * @param {number} [delayHint] Optional hint about how long to wait between tests
47 * @return {jQuery.Promise} Promise that resolves when the completionTest returns true.
48 * Note that this _could_ already be resolved when it's returned, so there's no
49 * guarantee that your `done` call on it will be delayed.
50 */
51 ve.Scheduler.prototype.schedule = function ( immediateAction, completionTest, delayHint ) {
52 const deferred = ve.createDeferred(),
53 startTime = this.now(),
54 testThenAct = function () {
55 let complete;
56 try {
57 complete = completionTest();
58 } catch ( e ) {
59 deferred.reject( e );
60 return;
61 }
62 if ( complete ) {
63 deferred.resolve();
64 return;
65 }
66 if ( this.now() - startTime > this.constructor.static.maxDelay ) {
67 deferred.reject();
68 return;
69 }
70 this.postpone( testThenAct, delayHint );
71 }.bind( this );
72
73 // In the future, we may want to expand this to track whether other async calls
74 // were made within the action.
75 immediateAction();
76
77 // Spin up the test cycle
78 testThenAct();
79
80 return deferred.promise();
81 };
82
83 /**
84 * Make a postponed call.
85 *
86 * This is a separate function because that makes it easier to replace when testing
87 *
88 * @param {Function} callback The function to call
89 * @param {number} delay Delay before running callback
90 * @return {number} Unique postponed timeout id
91 */
92 ve.Scheduler.prototype.postpone = function ( callback, delay ) {
93 return setTimeout( callback, delay );
94 };
95
96 /**
97 * Obtain the current timestamp
98 *
99 * This is a separate function because that makes it easier to replace when testing
100 *
101 * @return {number} Current timestamp in milliseconds
102 */
103 ve.Scheduler.prototype.now = function () {
104 return Date.now();
105 };
106
107 /* Initialization */
108
109 ve.scheduler = new ve.Scheduler();