Skip to content

Commit 4293733

Browse files
committed
debt - platform#setImmediate should be a micro task on all platforms
1 parent c842995 commit 4293733

2 files changed

Lines changed: 15 additions & 14 deletions

File tree

src/vs/base/common/platform.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,20 +156,21 @@ export const translationsConfigFile = _translationsConfigFile;
156156
const _globals = (typeof self === 'object' ? self : typeof global === 'object' ? global : {} as any);
157157
export const globals: any = _globals;
158158

159-
let _setImmediate: ((callback: (...args: any[]) => void) => number) | null = null;
160-
export function setImmediate(callback: (...args: any[]) => void): number {
161-
if (_setImmediate === null) {
162-
if (globals.setImmediate) {
163-
_setImmediate = globals.setImmediate.bind(globals);
164-
} else if (typeof process !== 'undefined' && typeof process.nextTick === 'function') {
165-
_setImmediate = process.nextTick.bind(process);
166-
} else {
167-
_setImmediate = globals.setTimeout.bind(globals);
168-
}
169-
}
170-
return _setImmediate!(callback);
159+
interface ISetImmediate {
160+
(callback: (...args: any[]) => void): void;
171161
}
172162

163+
export const setImmediate: ISetImmediate = (function defineSetImmediate() {
164+
if (globals.setImmediate) {
165+
return globals.setImmediate.bind(globals);
166+
}
167+
if (typeof process !== 'undefined' && typeof process.nextTick === 'function') {
168+
return process.nextTick.bind(process);
169+
}
170+
const _promise = Promise.resolve();
171+
return (callback: (...args: any[]) => void) => _promise.then(callback);
172+
})();
173+
173174
export const enum OperatingSystem {
174175
Windows = 1,
175176
Macintosh = 2,

src/vs/base/common/process.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ interface IProcess {
1010
env: IProcessEnvironment;
1111

1212
cwd(): string;
13-
nextTick(callback: (...args: any[]) => void): number;
13+
nextTick(callback: (...args: any[]) => void): void;
1414
}
1515

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

2424
export const cwd = safeProcess.cwd;

0 commit comments

Comments
 (0)