diff --git a/src/mol-task/execution/synchronous.ts b/src/mol-task/execution/synchronous.ts index 0ccaf8e66ab6d6e087877332358d21467d166072..3d0f1f2c0de45c7dfd49caab26e87251a548588c 100644 --- a/src/mol-task/execution/synchronous.ts +++ b/src/mol-task/execution/synchronous.ts @@ -10,7 +10,6 @@ import RuntimeContext from './runtime-context' const voidPromise = Promise.resolve(void 0); class SynchronousRuntimeContext implements RuntimeContext { - id: number = 0; requiresUpdate: boolean = false; update(progress: Partial<RuntimeContext.ProgressUpdate>): Promise<void> { return voidPromise; } runChild<T>(progress: Partial<RuntimeContext.ProgressUpdate>, task: Task<T>): Promise<T> { return ExecuteSynchronous(task); } diff --git a/src/mol-task/time.ts b/src/mol-task/time.ts index 9f0d233adf92d20d0ab3cc5a27c03f26a1b42f73..1b29d548da47e8cb42eb00cc6799878e7fe3de7c 100644 --- a/src/mol-task/time.ts +++ b/src/mol-task/time.ts @@ -16,6 +16,8 @@ const now: () => number = (function () { const t = process.hrtime(); return t[0] * 1000 + t[1] / 1000000; }; + } else if (Date.now) { + return () => Date.now(); } else { return () => +new Date(); } diff --git a/src/mol-task/execution/immediate.ts b/src/mol-task/util/immediate.ts similarity index 81% rename from src/mol-task/execution/immediate.ts rename to src/mol-task/util/immediate.ts index e118ec97cbf221e6b21d7126687f39bcc78753e7..411f82db36496a051f1f212e4e0b9a8ae7f72bc7 100644 --- a/src/mol-task/execution/immediate.ts +++ b/src/mol-task/util/immediate.ts @@ -10,14 +10,21 @@ * MIT license. */ +declare var WorkerGlobalScope: any; 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 Task = { callback: Callback, args: any[] } const tasksByHandle: { [handle: number]: Task } = { }; const doc = typeof document !== 'undefined' ? document : void 0; - let currentlyRunningATask = false; let nextHandle = 1; // Spec says greater than zero let registerImmediate: ((handle: number) => void); @@ -60,24 +67,9 @@ function createImmediateActions() { } 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]; - if (task) { - currentlyRunningATask = true; - try { - run(task); - } finally { - clearImmediate(handle); - currentlyRunningATask = false; - } - } - } + const task = tasksByHandle[handle]; + clearImmediate(handle); + run(task); } function installNextTickImplementation() { @@ -87,9 +79,6 @@ function createImmediateActions() { } 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) { let postMessageIsAsynchronous = true; const oldOnMessage = global.onmessage; @@ -108,7 +97,6 @@ function createImmediateActions() { // * http://www.whatwg.org/specs/web-apps/current-work/multipage/comms.html#crossDocumentMessages const messagePrefix = 'setImmediate$' + Math.random() + '$'; - const global = typeof window !== 'undefined' ? window as any : void 0; const onGlobalMessage = function(event: any) { if (event.source === global && typeof event.data === 'string' && @@ -189,7 +177,10 @@ function createImmediateActions() { const immediateActions = (function () { if (typeof setImmediate !== '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 } } return createImmediateActions();