This repository has been archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
asyncscheduler.js
183 lines (149 loc) · 5.23 KB
/
asyncscheduler.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
'use strict';
var Scheduler = require('./scheduler');
var disposables = require('rx.disposables');
var BinaryDisposable = disposables.BinaryDisposable;
var Disposable = disposables.Disposable;
var SingleAssignmentDisposable = disposables.SingleAssignmentDisposable;
var isFunction = require('./internal/isfunction');
var tryCatchUtils = require('./internal/trycatchutils');
var tryCatch = tryCatchUtils.tryCatch, thrower = tryCatchUtils.thrower;
var inherits = require('inherits');
var scheduleMethod, clearMethod;
(function () {
var nextHandle = 1, tasksByHandle = {}, currentlyRunning = false;
clearMethod = function (handle) {
delete tasksByHandle[handle];
};
function runTask(handle) {
if (currentlyRunning) {
global.setTimeout(function () { runTask(handle); }, 0);
} else {
var task = tasksByHandle[handle];
if (task) {
currentlyRunning = true;
var result = tryCatch(task)();
clearMethod(handle);
currentlyRunning = false;
if (result === global.RxSchedulers.errorObj) { thrower(result.e); }
}
}
}
var setImmediate = global.setImmediate;
function postMessageSupported () {
// Ensure not in a worker
if (!global.postMessage || global.importScripts) { return false; }
var isAsync = false, oldHandler = global.onmessage;
// Test for async
global.onmessage = function () { isAsync = true; };
global.postMessage('', '*');
global.onmessage = oldHandler;
return isAsync;
}
// Use in order, setImmediate, nextTick, postMessage, MessageChannel, script readystatechanged, setTimeout
if (isFunction(setImmediate)) {
scheduleMethod = function (action) {
var id = nextHandle++;
tasksByHandle[id] = action;
setImmediate(function () { runTask(id); });
return id;
};
} else if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
scheduleMethod = function (action) {
var id = nextHandle++;
tasksByHandle[id] = action;
process.nextTick(function () { runTask(id); });
return id;
};
} else if (postMessageSupported()) {
var MSG_PREFIX = 'ms.rx.schedule' + Math.random();
var onGlobalPostMessage = function (event) {
// Only if we're a match to avoid any other global events
if (typeof event.data === 'string' && event.data.substring(0, MSG_PREFIX.length) === MSG_PREFIX) {
runTask(event.data.substring(MSG_PREFIX.length));
}
};
global.addEventListener('message', onGlobalPostMessage, false);
scheduleMethod = function (action) {
var id = nextHandle++;
tasksByHandle[id] = action;
global.postMessage(MSG_PREFIX + id, '*');
return id;
};
} else if (!!global.MessageChannel) {
var channel = new global.MessageChannel();
channel.port1.onmessage = function (e) { runTask(e.data); };
scheduleMethod = function (action) {
var id = nextHandle++;
tasksByHandle[id] = action;
channel.port2.postMessage(id);
return id;
};
} else if ('document' in global && 'onreadystatechange' in global.document.createElement('script')) {
scheduleMethod = function (action) {
var scriptElement = global.document.createElement('script');
var id = nextHandle++;
tasksByHandle[id] = action;
scriptElement.onreadystatechange = function () {
runTask(id);
scriptElement.onreadystatechange = null;
scriptElement.parentNode.removeChild(scriptElement);
scriptElement = null;
};
global.document.documentElement.appendChild(scriptElement);
return id;
};
} else {
scheduleMethod = function (action) {
var id = nextHandle++;
tasksByHandle[id] = action;
global.setTimeout(function () {
runTask(id);
}, 0);
return id;
};
}
}());
/**
* Gets a scheduler that schedules work via a timed callback based upon platform.
*/
function AsyncScheduler() {
Scheduler.call(this);
}
inherits(AsyncScheduler, Scheduler);
function scheduleAction(disposable, action, scheduler, state) {
return function schedule() {
disposable.setDisposable(Disposable._fixup(action(scheduler, state)));
};
}
function ClearDisposable(id) {
this._id = id;
this.isDisposed = false;
}
ClearDisposable.prototype.dispose = function () {
if (!this.isDisposed) {
this.isDisposed = true;
clearMethod(this._id);
}
};
function LocalClearDisposable(id) {
this._id = id;
this.isDisposed = false;
}
LocalClearDisposable.prototype.dispose = function () {
if (!this.isDisposed) {
this.isDisposed = true;
global.clearTimeout(this._id);
}
};
AsyncScheduler.prototype.schedule = function (state, action) {
var disposable = new SingleAssignmentDisposable(),
id = scheduleMethod(scheduleAction(disposable, action, this, state));
return new BinaryDisposable(disposable, new ClearDisposable(id));
};
AsyncScheduler.prototype._scheduleFuture = function (state, dueTime, action) {
if (dueTime === 0) { return this.schedule(state, action); }
var disposable = new SingleAssignmentDisposable(),
id = global.setTimeout(scheduleAction(disposable, action, this, state), dueTime);
return new BinaryDisposable(disposable, new LocalClearDisposable(id));
};
module.exports = AsyncScheduler;