Skip to content
Snippets Groups Projects
Commit 64ba197d authored by David Sehnal's avatar David Sehnal
Browse files

working on mol-task

parent abcb1f54
No related branches found
No related tags found
No related merge requests found
...@@ -10,7 +10,6 @@ import RuntimeContext from './runtime-context' ...@@ -10,7 +10,6 @@ import RuntimeContext from './runtime-context'
const voidPromise = Promise.resolve(void 0); const voidPromise = Promise.resolve(void 0);
class SynchronousRuntimeContext implements RuntimeContext { class SynchronousRuntimeContext implements RuntimeContext {
id: number = 0;
requiresUpdate: boolean = false; requiresUpdate: boolean = false;
update(progress: Partial<RuntimeContext.ProgressUpdate>): Promise<void> { return voidPromise; } update(progress: Partial<RuntimeContext.ProgressUpdate>): Promise<void> { return voidPromise; }
runChild<T>(progress: Partial<RuntimeContext.ProgressUpdate>, task: Task<T>): Promise<T> { return ExecuteSynchronous(task); } runChild<T>(progress: Partial<RuntimeContext.ProgressUpdate>, task: Task<T>): Promise<T> { return ExecuteSynchronous(task); }
......
...@@ -16,6 +16,8 @@ const now: () => number = (function () { ...@@ -16,6 +16,8 @@ const now: () => number = (function () {
const t = process.hrtime(); const t = process.hrtime();
return t[0] * 1000 + t[1] / 1000000; return t[0] * 1000 + t[1] / 1000000;
}; };
} else if (Date.now) {
return () => Date.now();
} else { } else {
return () => +new Date(); return () => +new Date();
} }
......
...@@ -10,14 +10,21 @@ ...@@ -10,14 +10,21 @@
* MIT license. * MIT license.
*/ */
declare var WorkerGlobalScope: any;
function createImmediateActions() { function createImmediateActions() {
const global: any = (function () {
const _window = typeof window !== 'undefined' && window;
const _self = typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope && self;
const _global = typeof global !== 'undefined' && global;
return _window || _global || _self;
})();
type Callback = (...args: any[]) => void; type Callback = (...args: any[]) => void;
type Task = { callback: Callback, args: any[] } type Task = { callback: Callback, args: any[] }
const tasksByHandle: { [handle: number]: Task } = { }; const tasksByHandle: { [handle: number]: Task } = { };
const doc = typeof document !== 'undefined' ? document : void 0; const doc = typeof document !== 'undefined' ? document : void 0;
let currentlyRunningATask = false;
let nextHandle = 1; // Spec says greater than zero let nextHandle = 1; // Spec says greater than zero
let registerImmediate: ((handle: number) => void); let registerImmediate: ((handle: number) => void);
...@@ -60,24 +67,9 @@ function createImmediateActions() { ...@@ -60,24 +67,9 @@ function createImmediateActions() {
} }
function runIfPresent(handle: number) { function runIfPresent(handle: number) {
// From the spec: 'Wait until any invocations of this algorithm started before this one have completed.'
// So if we're currently running a task, we'll need to delay this invocation.
if (currentlyRunningATask) {
// Delay by doing a setTimeout. setImmediate was tried instead, but in Firefox 7 it generated a
// 'too much recursion' error.
setTimeout(runIfPresent, 0, handle);
} else {
const task = tasksByHandle[handle]; const task = tasksByHandle[handle];
if (task) {
currentlyRunningATask = true;
try {
run(task);
} finally {
clearImmediate(handle); clearImmediate(handle);
currentlyRunningATask = false; run(task);
}
}
}
} }
function installNextTickImplementation() { function installNextTickImplementation() {
...@@ -87,9 +79,6 @@ function createImmediateActions() { ...@@ -87,9 +79,6 @@ function createImmediateActions() {
} }
function canUsePostMessage() { function canUsePostMessage() {
// The test against `importScripts` prevents this implementation from being installed inside a web worker,
// where `global.postMessage` means something completely different and can't be used for this purpose.
const global = typeof window !== 'undefined' ? window as any : void 0;
if (global && global.postMessage && !global.importScripts) { if (global && global.postMessage && !global.importScripts) {
let postMessageIsAsynchronous = true; let postMessageIsAsynchronous = true;
const oldOnMessage = global.onmessage; const oldOnMessage = global.onmessage;
...@@ -108,7 +97,6 @@ function createImmediateActions() { ...@@ -108,7 +97,6 @@ function createImmediateActions() {
// * http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html#crossDocumentMessages // * http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html#crossDocumentMessages
const messagePrefix = 'setImmediate$' + Math.random() + '$'; const messagePrefix = 'setImmediate$' + Math.random() + '$';
const global = typeof window !== 'undefined' ? window as any : void 0;
const onGlobalMessage = function(event: any) { const onGlobalMessage = function(event: any) {
if (event.source === global && if (event.source === global &&
typeof event.data === 'string' && typeof event.data === 'string' &&
...@@ -189,7 +177,10 @@ function createImmediateActions() { ...@@ -189,7 +177,10 @@ function createImmediateActions() {
const immediateActions = (function () { const immediateActions = (function () {
if (typeof setImmediate !== 'undefined') { if (typeof setImmediate !== 'undefined') {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
return { setImmediate: (handler: any, ...args: any[]) => window.setImmediate(handler, ...args as any), clearImmediate: (handle: any) => window.clearImmediate(handle) }; return {
setImmediate: (handler: any, ...args: any[]) => window.setImmediate(handler, ...args as any),
clearImmediate: (handle: any) => window.clearImmediate(handle)
};
} else return { setImmediate, clearImmediate } } else return { setImmediate, clearImmediate }
} }
return createImmediateActions(); return createImmediateActions();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment