Skip to content
This repository was archived by the owner on Feb 16, 2019. It is now read-only.

Commit 28ed83a

Browse files
committed
Improved asynchronous call by setImmediate and MessageChannel.
1 parent 7f4b42d commit 28ed83a

File tree

1 file changed

+52
-8
lines changed

1 file changed

+52
-8
lines changed

src/Core.js

+52-8
Original file line numberDiff line numberDiff line change
@@ -1187,16 +1187,60 @@ update(PotInternal, {
11871187
* @ignore
11881188
* @based JSDeferred.next
11891189
*/
1190-
flush : function(callback) {
1191-
var handler = this.byEvent || this.byTick || this.byTimer;
1192-
handler(callback);
1190+
flush : function() {
1191+
(this.byTick || this.byImmediate ||
1192+
this.byMessage || this.byEvent ||
1193+
this.byTimer)(callback);
11931194
},
1195+
/**
1196+
* @private
1197+
* @ignore
1198+
*/
1199+
byMessage : function() {
1200+
var channel, queue;
1201+
if (typeof MessageChannel !== 'function') {
1202+
return false;
1203+
}
1204+
try {
1205+
channel = new MessageChannel();
1206+
if (!channel.port1 || !channel.port2) {
1207+
throw false;
1208+
}
1209+
queue = [];
1210+
/**@ignore*/
1211+
channel.port1.onmessage = function() {
1212+
queue.shift()();
1213+
};
1214+
} catch (e) {
1215+
return false;
1216+
}
1217+
return function(callback) {
1218+
queue.push(callback);
1219+
channel.port2.postMessage('');
1220+
};
1221+
}(),
1222+
/**
1223+
* @private
1224+
* @ignore
1225+
*/
1226+
byImmediate : function() {
1227+
if (typeof setImmediate !== 'function') {
1228+
return false;
1229+
}
1230+
return function(callback) {
1231+
try {
1232+
setImmediate(callback);
1233+
} catch (e) {
1234+
(this.byImmediate = this.byTimer)(callback);
1235+
}
1236+
};
1237+
}(),
11941238
/**
11951239
* @private
11961240
* @ignore
11971241
* @based JSDeferred.next
11981242
*/
1199-
byEvent : (function() {
1243+
byEvent : function() {
12001244
var IMAGE;
12011245
if (PotSystem.isNonBrowser || PotSystem.isNodeJS ||
12021246
typeof window !== 'object' || typeof document !== 'object' ||
@@ -1234,15 +1278,15 @@ update(PotInternal, {
12341278
try {
12351279
img.src = IMAGE;
12361280
} catch (e) {
1237-
this.byEvent = this.byTimer;
1281+
(this.byEvent = this.byTimer)(callback);
12381282
}
12391283
};
1240-
}()),
1284+
}(),
12411285
/**
12421286
* @private
12431287
* @ignore
12441288
*/
1245-
byTick : (function() {
1289+
byTick : function() {
12461290
if (!PotSystem.isNodeJS || typeof process !== 'object' ||
12471291
typeof process.nextTick !== 'function') {
12481292
return false;
@@ -1251,7 +1295,7 @@ update(PotInternal, {
12511295
return function(callback) {
12521296
process.nextTick(callback);
12531297
};
1254-
}()),
1298+
}(),
12551299
/**
12561300
* @private
12571301
* @ignore

0 commit comments

Comments
 (0)