Skip to content

Commit

Permalink
debt - platform#setImmediate should be a micro task on all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Sep 2, 2019
1 parent c842995 commit 4293733
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
25 changes: 13 additions & 12 deletions src/vs/base/common/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,21 @@ export const translationsConfigFile = _translationsConfigFile;
const _globals = (typeof self === 'object' ? self : typeof global === 'object' ? global : {} as any);
export const globals: any = _globals;

let _setImmediate: ((callback: (...args: any[]) => void) => number) | null = null;
export function setImmediate(callback: (...args: any[]) => void): number {
if (_setImmediate === null) {
if (globals.setImmediate) {
_setImmediate = globals.setImmediate.bind(globals);
} else if (typeof process !== 'undefined' && typeof process.nextTick === 'function') {
_setImmediate = process.nextTick.bind(process);
} else {
_setImmediate = globals.setTimeout.bind(globals);
}
}
return _setImmediate!(callback);
interface ISetImmediate {
(callback: (...args: any[]) => void): void;
}

export const setImmediate: ISetImmediate = (function defineSetImmediate() {
if (globals.setImmediate) {
return globals.setImmediate.bind(globals);
}
if (typeof process !== 'undefined' && typeof process.nextTick === 'function') {
return process.nextTick.bind(process);
}
const _promise = Promise.resolve();
return (callback: (...args: any[]) => void) => _promise.then(callback);
})();

export const enum OperatingSystem {
Windows = 1,
Macintosh = 2,
Expand Down
4 changes: 2 additions & 2 deletions src/vs/base/common/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ interface IProcess {
env: IProcessEnvironment;

cwd(): string;
nextTick(callback: (...args: any[]) => void): number;
nextTick(callback: (...args: any[]) => void): void;
}

declare const process: IProcess;
const safeProcess: IProcess = (typeof process === 'undefined') ? {
cwd(): string { return '/'; },
env: Object.create(null),
get platform(): string { return isWindows ? 'win32' : isMacintosh ? 'darwin' : 'linux'; },
nextTick(callback: (...args: any[]) => void): number { return setImmediate(callback); }
nextTick(callback: (...args: any[]) => void): void { return setImmediate(callback); }
} : process;

export const cwd = safeProcess.cwd;
Expand Down

0 comments on commit 4293733

Please sign in to comment.