Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Commit 5555090

Browse files
Updating defaultscheduler
1 parent 089dca5 commit 5555090

25 files changed

+644
-256
lines changed

dist/rx.all.compat.js

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@
14461446
return currentScheduler;
14471447
}());
14481448

1449-
var scheduleMethod, clearMethod = noop;
1449+
var scheduleMethod;
14501450

14511451
var localTimer = (function () {
14521452
var localSetTimeout, localClearTimeout = noop;
@@ -1472,7 +1472,30 @@
14721472

14731473
(function () {
14741474

1475-
var taskId = 0, tasks = new Array(1000);
1475+
var nextHandle = 1, tasksByHandle = {}, currentlyRunning = false;
1476+
1477+
function clearMethod(handle) {
1478+
delete tasksByHandle[handle];
1479+
}
1480+
1481+
function runTask(handle) {
1482+
if (currentlyRunning) {
1483+
root.setTimeout(function () { runTask(handle) }, 0);
1484+
} else {
1485+
var task = tasksByHandle[handle];
1486+
if (task) {
1487+
currentlyRunning = true;
1488+
try {
1489+
task();
1490+
} catch (e) {
1491+
throw e;
1492+
} finally {
1493+
clearMethod(handle);
1494+
currentlyRunning = false;
1495+
}
1496+
}
1497+
}
1498+
}
14761499

14771500
var reNative = RegExp('^' +
14781501
String(toString)
@@ -1481,9 +1504,7 @@
14811504
);
14821505

14831506
var setImmediate = typeof (setImmediate = freeGlobal && moduleExports && freeGlobal.setImmediate) == 'function' &&
1484-
!reNative.test(setImmediate) && setImmediate,
1485-
clearImmediate = typeof (clearImmediate = freeGlobal && moduleExports && freeGlobal.clearImmediate) == 'function' &&
1486-
!reNative.test(clearImmediate) && clearImmediate;
1507+
!reNative.test(setImmediate) && setImmediate;
14871508

14881509
function postMessageSupported () {
14891510
// Ensure not in a worker
@@ -1498,20 +1519,33 @@
14981519
}
14991520

15001521
// Use in order, setImmediate, nextTick, postMessage, MessageChannel, script readystatechanged, setTimeout
1501-
if (typeof setImmediate === 'function') {
1502-
scheduleMethod = setImmediate;
1503-
clearMethod = clearImmediate;
1522+
if (isFunction(setImmediate)) {
1523+
scheduleMethod = function (action) {
1524+
var id = nextHandle++;
1525+
tasksByHandle[id] = action;
1526+
setImmediate(function () {
1527+
runTask(id);
1528+
});
1529+
1530+
return id;
1531+
};
15041532
} else if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
1505-
scheduleMethod = process.nextTick;
1533+
scheduleMethod = function (action) {
1534+
var id = nextHandle++;
1535+
tasksByHandle[id] = action;
1536+
process.nextTick(function () {
1537+
runTask(id);
1538+
});
1539+
1540+
return id;
1541+
};
15061542
} else if (postMessageSupported()) {
15071543
var MSG_PREFIX = 'ms.rx.schedule' + Math.random();
15081544

1509-
var onGlobalPostMessage = function (event) {
1545+
function onGlobalPostMessage(event) {
15101546
// Only if we're a match to avoid any other global events
15111547
if (typeof event.data === 'string' && event.data.substring(0, MSG_PREFIX.length) === MSG_PREFIX) {
1512-
var handleId = event.data.substring(MSG_PREFIX.length), action = tasks[handleId];
1513-
action();
1514-
tasks[handleId] = undefined;
1548+
runTask(event.data.substring(MSG_PREFIX.length));
15151549
}
15161550
}
15171551

@@ -1522,40 +1556,49 @@
15221556
}
15231557

15241558
scheduleMethod = function (action) {
1525-
var currentId = taskId++;
1526-
tasks[currentId] = action;
1559+
var id = nextHandle++;
1560+
tasksByHandle[id] = action;
15271561
root.postMessage(MSG_PREFIX + currentId, '*');
1562+
return id;
15281563
};
15291564
} else if (!!root.MessageChannel) {
15301565
var channel = new root.MessageChannel();
15311566

1532-
channel.port1.onmessage = function (event) {
1533-
var id = event.data, action = tasks[id];
1534-
action();
1535-
tasks[id] = undefined;
1536-
};
1567+
channel.port1.onmessage = function (event) { runTask(event.data); };
15371568

15381569
scheduleMethod = function (action) {
1539-
var id = taskId++;
1540-
tasks[id] = action;
1570+
var id = nextHandle++;
1571+
tasksByHandle[id] = action;
15411572
channel.port2.postMessage(id);
1573+
return id;
15421574
};
15431575
} else if ('document' in root && 'onreadystatechange' in root.document.createElement('script')) {
15441576

15451577
scheduleMethod = function (action) {
15461578
var scriptElement = root.document.createElement('script');
1579+
var id = nextHandle++;
1580+
tasksByHandle[id] = action;
1581+
15471582
scriptElement.onreadystatechange = function () {
1548-
action();
1583+
runTask(id);
15491584
scriptElement.onreadystatechange = null;
15501585
scriptElement.parentNode.removeChild(scriptElement);
15511586
scriptElement = null;
15521587
};
15531588
root.document.documentElement.appendChild(scriptElement);
1589+
return id;
15541590
};
15551591

15561592
} else {
1557-
scheduleMethod = function (action) { return localSetTimeout(action, 0); };
1558-
clearMethod = localClearTimeout;
1593+
scheduleMethod = function (action) {
1594+
var id = nextHandle++;
1595+
tasksByHandle[id] = action;
1596+
localSetTimeout(function () {
1597+
runTask(id);
1598+
}, 0);
1599+
1600+
return id;
1601+
};
15591602
}
15601603
}());
15611604

dist/rx.all.compat.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rx.all.compat.min.js

Lines changed: 4 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rx.all.js

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@
12551255
return currentScheduler;
12561256
}());
12571257

1258-
var scheduleMethod, clearMethod = noop;
1258+
var scheduleMethod;
12591259

12601260
var localTimer = (function () {
12611261
var localSetTimeout, localClearTimeout = noop;
@@ -1281,7 +1281,30 @@
12811281

12821282
(function () {
12831283

1284-
var taskId = 0, tasks = new Array(1000);
1284+
var nextHandle = 1, tasksByHandle = {}, currentlyRunning = false;
1285+
1286+
function clearMethod(handle) {
1287+
delete tasksByHandle[handle];
1288+
}
1289+
1290+
function runTask(handle) {
1291+
if (currentlyRunning) {
1292+
root.setTimeout(function () { runTask(handle) }, 0);
1293+
} else {
1294+
var task = tasksByHandle[handle];
1295+
if (task) {
1296+
currentlyRunning = true;
1297+
try {
1298+
task();
1299+
} catch (e) {
1300+
throw e;
1301+
} finally {
1302+
clearMethod(handle);
1303+
currentlyRunning = false;
1304+
}
1305+
}
1306+
}
1307+
}
12851308

12861309
var reNative = RegExp('^' +
12871310
String(toString)
@@ -1290,9 +1313,7 @@
12901313
);
12911314

12921315
var setImmediate = typeof (setImmediate = freeGlobal && moduleExports && freeGlobal.setImmediate) == 'function' &&
1293-
!reNative.test(setImmediate) && setImmediate,
1294-
clearImmediate = typeof (clearImmediate = freeGlobal && moduleExports && freeGlobal.clearImmediate) == 'function' &&
1295-
!reNative.test(clearImmediate) && clearImmediate;
1316+
!reNative.test(setImmediate) && setImmediate;
12961317

12971318
function postMessageSupported () {
12981319
// Ensure not in a worker
@@ -1307,20 +1328,33 @@
13071328
}
13081329

13091330
// Use in order, setImmediate, nextTick, postMessage, MessageChannel, script readystatechanged, setTimeout
1310-
if (typeof setImmediate === 'function') {
1311-
scheduleMethod = setImmediate;
1312-
clearMethod = clearImmediate;
1331+
if (isFunction(setImmediate)) {
1332+
scheduleMethod = function (action) {
1333+
var id = nextHandle++;
1334+
tasksByHandle[id] = action;
1335+
setImmediate(function () {
1336+
runTask(id);
1337+
});
1338+
1339+
return id;
1340+
};
13131341
} else if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
1314-
scheduleMethod = process.nextTick;
1342+
scheduleMethod = function (action) {
1343+
var id = nextHandle++;
1344+
tasksByHandle[id] = action;
1345+
process.nextTick(function () {
1346+
runTask(id);
1347+
});
1348+
1349+
return id;
1350+
};
13151351
} else if (postMessageSupported()) {
13161352
var MSG_PREFIX = 'ms.rx.schedule' + Math.random();
13171353

1318-
var onGlobalPostMessage = function (event) {
1354+
function onGlobalPostMessage(event) {
13191355
// Only if we're a match to avoid any other global events
13201356
if (typeof event.data === 'string' && event.data.substring(0, MSG_PREFIX.length) === MSG_PREFIX) {
1321-
var handleId = event.data.substring(MSG_PREFIX.length), action = tasks[handleId];
1322-
action();
1323-
tasks[handleId] = undefined;
1357+
runTask(event.data.substring(MSG_PREFIX.length));
13241358
}
13251359
}
13261360

@@ -1331,40 +1365,49 @@
13311365
}
13321366

13331367
scheduleMethod = function (action) {
1334-
var currentId = taskId++;
1335-
tasks[currentId] = action;
1368+
var id = nextHandle++;
1369+
tasksByHandle[id] = action;
13361370
root.postMessage(MSG_PREFIX + currentId, '*');
1371+
return id;
13371372
};
13381373
} else if (!!root.MessageChannel) {
13391374
var channel = new root.MessageChannel();
13401375

1341-
channel.port1.onmessage = function (event) {
1342-
var id = event.data, action = tasks[id];
1343-
action();
1344-
tasks[id] = undefined;
1345-
};
1376+
channel.port1.onmessage = function (event) { runTask(event.data); };
13461377

13471378
scheduleMethod = function (action) {
1348-
var id = taskId++;
1349-
tasks[id] = action;
1379+
var id = nextHandle++;
1380+
tasksByHandle[id] = action;
13501381
channel.port2.postMessage(id);
1382+
return id;
13511383
};
13521384
} else if ('document' in root && 'onreadystatechange' in root.document.createElement('script')) {
13531385

13541386
scheduleMethod = function (action) {
13551387
var scriptElement = root.document.createElement('script');
1388+
var id = nextHandle++;
1389+
tasksByHandle[id] = action;
1390+
13561391
scriptElement.onreadystatechange = function () {
1357-
action();
1392+
runTask(id);
13581393
scriptElement.onreadystatechange = null;
13591394
scriptElement.parentNode.removeChild(scriptElement);
13601395
scriptElement = null;
13611396
};
13621397
root.document.documentElement.appendChild(scriptElement);
1398+
return id;
13631399
};
13641400

13651401
} else {
1366-
scheduleMethod = function (action) { return localSetTimeout(action, 0); };
1367-
clearMethod = localClearTimeout;
1402+
scheduleMethod = function (action) {
1403+
var id = nextHandle++;
1404+
tasksByHandle[id] = action;
1405+
localSetTimeout(function () {
1406+
runTask(id);
1407+
}, 0);
1408+
1409+
return id;
1410+
};
13681411
}
13691412
}());
13701413

dist/rx.all.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/rx.all.min.js

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)