Skip to content

Commit c3aebef

Browse files
committed
remove usages of TPromise.timeout
fixes microsoft#57866
1 parent 5f8ec7b commit c3aebef

11 files changed

Lines changed: 56 additions & 58 deletions

File tree

src/vs/base/common/async.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export function toThenable<T>(arg: T | Thenable<T>): Thenable<T> {
2626

2727
export interface CancelablePromise<T> extends Promise<T> {
2828
cancel(): void;
29+
cancelableThen<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => U | Thenable<U>): CancelablePromise<U>;
30+
cancelableThen<U>(onFulfilled?: (value: T) => U | Thenable<U>, onRejected?: (error: any) => void): CancelablePromise<U>;
2931
}
3032

3133
export function createCancelablePromise<T>(callback: (token: CancellationToken) => Thenable<T>): CancelablePromise<T> {
@@ -55,6 +57,13 @@ export function createCancelablePromise<T>(callback: (token: CancellationToken)
5557
catch<TResult = never>(reject?: ((reason: any) => TResult | Thenable<TResult>) | undefined | null): Promise<T | TResult> {
5658
return this.then(undefined, reject);
5759
}
60+
cancelableThen<TResult1 = T, TResult2 = never>(resolve?: ((value: T) => TResult1 | Thenable<TResult1>) | undefined | null, reject?: ((reason: any) => TResult2 | Thenable<TResult2>) | undefined | null): CancelablePromise<TResult1 | TResult2> {
61+
return createCancelablePromise<TResult1 | TResult2>(token => {
62+
const listener = token.onCancellationRequested(_ => this.cancel());
63+
always(promise, () => listener.dispose());
64+
return this.then(resolve, reject);
65+
});
66+
}
5867
};
5968
}
6069

src/vs/base/parts/ipc/node/ipc.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import { TPromise } from 'vs/base/common/winjs.base';
99
import { IDisposable, toDisposable, Disposable } from 'vs/base/common/lifecycle';
1010
import { Event, Emitter, once, filterEvent, toNativePromise, Relay } from 'vs/base/common/event';
11-
import { always, CancelablePromise, createCancelablePromise } from 'vs/base/common/async';
11+
import { always, CancelablePromise, createCancelablePromise, timeout } from 'vs/base/common/async';
1212
import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cancellation';
1313
import * as errors from 'vs/base/common/errors';
1414

@@ -611,7 +611,7 @@ export function getNextTickChannel<T extends IChannel>(channel: T): T {
611611
return channel.call(command, arg, cancellationToken);
612612
}
613613

614-
return TPromise.timeout(0)
614+
return TPromise.wrap(timeout(0))
615615
.then(() => didTick = true)
616616
.then(() => channel.call(command, arg, cancellationToken));
617617
},
@@ -622,7 +622,7 @@ export function getNextTickChannel<T extends IChannel>(channel: T): T {
622622

623623
const relay = new Relay();
624624

625-
TPromise.timeout(0)
625+
timeout(0)
626626
.then(() => didTick = true)
627627
.then(() => relay.input = channel.listen(event, arg));
628628

src/vs/base/parts/ipc/test/node/testService.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import { TPromise } from 'vs/base/common/winjs.base';
88
import { IChannel } from 'vs/base/parts/ipc/node/ipc';
99
import { Event, Emitter } from 'vs/base/common/event';
10+
import { timeout } from 'vs/base/common/async';
1011

1112
export interface IMarcoPoloEvent {
1213
answer: string;
@@ -34,7 +35,7 @@ export class TestService implements ITestService {
3435
}
3536

3637
cancelMe(): TPromise<boolean> {
37-
return TPromise.timeout(100).then(() => true);
38+
return TPromise.wrap(timeout(100)).then(() => true);
3839
}
3940
}
4041

src/vs/base/parts/tree/browser/treeView.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { KeyCode } from 'vs/base/common/keyCodes';
2525
import { Event, Emitter } from 'vs/base/common/event';
2626
import { DataTransfers } from 'vs/base/browser/dnd';
2727
import { DefaultTreestyler } from './treeDefaults';
28-
import { Delayer, CancelablePromise, createCancelablePromise, wireCancellationToken } from 'vs/base/common/async';
28+
import { Delayer, CancelablePromise, createCancelablePromise, wireCancellationToken, timeout } from 'vs/base/common/async';
2929

3030
export interface IRow {
3131
element: HTMLElement;
@@ -1543,7 +1543,7 @@ export class TreeView extends HeightMap {
15431543
}
15441544

15451545
if (reaction.autoExpand) {
1546-
const promise = WinJS.TPromise.timeout(500)
1546+
const promise = WinJS.TPromise.wrap(timeout(500))
15471547
.then(() => this.context.tree.expand(this.currentDropElement))
15481548
.then(() => this.shouldInvalidateDropReaction = true);
15491549

src/vs/base/parts/tree/test/browser/treeModel.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as WinJS from 'vs/base/common/winjs.base';
1212
import * as model from 'vs/base/parts/tree/browser/treeModel';
1313
import * as TreeDefaults from 'vs/base/parts/tree/browser/treeDefaults';
1414
import { Event, Emitter } from 'vs/base/common/event';
15+
import { timeout } from 'vs/base/common/async';
1516

1617
export class FakeRenderer {
1718

@@ -1322,7 +1323,7 @@ suite('TreeModel - Dynamic data model', () => {
13221323
dataModel.addChild('father', 'brother');
13231324
dataModel.addChild('mother', 'sister');
13241325

1325-
dataModel.promiseFactory = () => { return WinJS.TPromise.timeout(0); };
1326+
dataModel.promiseFactory = () => { return WinJS.TPromise.wrap(timeout(0)); };
13261327

13271328
var getTimes = 0;
13281329
var gotTimes = 0;
@@ -1583,7 +1584,7 @@ suite('TreeModel - Dynamic data model', () => {
15831584
return model.setInput('root').then(() => {
15841585

15851586
// delay expansions and refreshes
1586-
dataModel.promiseFactory = () => { return WinJS.TPromise.timeout(0); };
1587+
dataModel.promiseFactory = () => { return WinJS.TPromise.wrap(timeout(0)); };
15871588

15881589
var promises: WinJS.Promise[] = [];
15891590

@@ -1636,9 +1637,9 @@ suite('TreeModel - bugs', () => {
16361637
let listeners = <any>[];
16371638

16381639
// helpers
1639-
var getGetRootChildren = (children: string[], timeout = 0) => () => WinJS.TPromise.timeout(timeout).then(() => children);
1640+
var getGetRootChildren = (children: string[], millis = 0) => () => WinJS.TPromise.wrap(timeout(millis)).then(() => children);
16401641
var getRootChildren = getGetRootChildren(['homer', 'bart', 'lisa', 'marge', 'maggie'], 0);
1641-
var getGetBartChildren = (timeout = 0) => () => WinJS.TPromise.timeout(timeout).then(() => ['milhouse', 'nelson']);
1642+
var getGetBartChildren = (millis = 0) => () => WinJS.TPromise.wrap(timeout(millis)).then(() => ['milhouse', 'nelson']);
16421643
var getBartChildren = getGetBartChildren(0);
16431644

16441645
// item expanding should not exist!

src/vs/base/test/common/async.test.ts

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,7 @@ suite('Async', () => {
158158

159159
test('Throttler', function () {
160160
let count = 0;
161-
let factory = () => {
162-
return TPromise.timeout(0).then(() => {
163-
return ++count;
164-
});
165-
};
161+
let factory = () => TPromise.wrap(async.timeout(0)).then(() => ++count);
166162

167163
let throttler = new async.Throttler();
168164

@@ -185,11 +181,7 @@ suite('Async', () => {
185181

186182
test('Throttler - cancel should not cancel other promises', function () {
187183
let count = 0;
188-
let factory = () => {
189-
return TPromise.timeout(0).then(() => {
190-
return ++count;
191-
});
192-
};
184+
let factory = () => TPromise.wrap(async.timeout(0)).then(() => ++count);
193185

194186
let throttler = new async.Throttler();
195187
let p1: TPromise;
@@ -208,11 +200,7 @@ suite('Async', () => {
208200

209201
test('Throttler - cancel the first queued promise should not cancel other promises', function () {
210202
let count = 0;
211-
let factory = () => {
212-
return TPromise.timeout(0).then(() => {
213-
return ++count;
214-
});
215-
};
203+
let factory = () => TPromise.wrap(async.timeout(0)).then(() => ++count);
216204

217205
let throttler = new async.Throttler();
218206
let p2: TPromise;
@@ -231,11 +219,7 @@ suite('Async', () => {
231219

232220
test('Throttler - cancel in the middle should not cancel other promises', function () {
233221
let count = 0;
234-
let factory = () => {
235-
return TPromise.timeout(0).then(() => {
236-
return ++count;
237-
});
238-
};
222+
let factory = () => TPromise.wrap(async.timeout(0)).then(() => ++count);
239223

240224
let throttler = new async.Throttler();
241225
let p3: TPromise;
@@ -254,7 +238,7 @@ suite('Async', () => {
254238

255239
test('Throttler - last factory should be the one getting called', function () {
256240
let factoryFactory = (n: number) => () => {
257-
return TPromise.timeout(0).then(() => n);
241+
return TPromise.wrap(async.timeout(0)).then(() => n);
258242
};
259243

260244
let throttler = new async.Throttler();
@@ -465,9 +449,7 @@ suite('Async', () => {
465449
});
466450

467451
test('Limiter - async', function () {
468-
let factoryFactory = (n: number) => () => {
469-
return TPromise.timeout(0).then(() => n);
470-
};
452+
let factoryFactory = (n: number) => () => TPromise.wrap(async.timeout(0)).then(() => n);
471453

472454
let limiter = new async.Limiter(1);
473455
let promises: TPromise[] = [];
@@ -492,7 +474,7 @@ suite('Async', () => {
492474
let factoryFactory = (n: number) => () => {
493475
activePromises++;
494476
assert(activePromises < 6);
495-
return TPromise.timeout(0).then(() => { activePromises--; return n; });
477+
return TPromise.wrap(async.timeout(0)).then(() => { activePromises--; return n; });
496478
};
497479

498480
let limiter = new async.Limiter(5);
@@ -513,7 +495,7 @@ suite('Async', () => {
513495
let f1 = () => TPromise.as(true).then(() => syncPromise = true);
514496

515497
let asyncPromise = false;
516-
let f2 = () => TPromise.timeout(10).then(() => asyncPromise = true);
498+
let f2 = () => TPromise.wrap(async.timeout(10)).then(() => asyncPromise = true);
517499

518500
assert.equal(queue.size, 0);
519501

@@ -535,10 +517,10 @@ suite('Async', () => {
535517
let res: number[] = [];
536518

537519
let f1 = () => TPromise.as(true).then(() => res.push(1));
538-
let f2 = () => TPromise.timeout(10).then(() => res.push(2));
520+
let f2 = () => TPromise.wrap(async.timeout(10)).then(() => res.push(2));
539521
let f3 = () => TPromise.as(true).then(() => res.push(3));
540-
let f4 = () => TPromise.timeout(20).then(() => res.push(4));
541-
let f5 = () => TPromise.timeout(0).then(() => res.push(5));
522+
let f4 = () => TPromise.wrap(async.timeout(20)).then(() => res.push(4));
523+
let f5 = () => TPromise.wrap(async.timeout(0)).then(() => res.push(5));
542524

543525
queue.queue(f1);
544526
queue.queue(f2);
@@ -560,10 +542,10 @@ suite('Async', () => {
560542
let error = false;
561543

562544
let f1 = () => TPromise.as(true).then(() => res.push(1));
563-
let f2 = () => TPromise.timeout(10).then(() => res.push(2));
545+
let f2 = () => TPromise.wrap(async.timeout(10)).then(() => res.push(2));
564546
let f3 = () => TPromise.as(true).then(() => TPromise.wrapError(new Error('error')));
565-
let f4 = () => TPromise.timeout(20).then(() => res.push(4));
566-
let f5 = () => TPromise.timeout(0).then(() => res.push(5));
547+
let f4 = () => TPromise.wrap(async.timeout(20)).then(() => res.push(4));
548+
let f5 = () => TPromise.wrap(async.timeout(0)).then(() => res.push(5));
567549

568550
queue.queue(f1);
569551
queue.queue(f2);
@@ -584,10 +566,10 @@ suite('Async', () => {
584566
let res: number[] = [];
585567

586568
let f1 = () => TPromise.as(true).then(() => res.push(1));
587-
let f2 = () => TPromise.timeout(10).then(() => res.push(2));
569+
let f2 = () => TPromise.wrap(async.timeout(10)).then(() => res.push(2));
588570
let f3 = () => TPromise.as(true).then(() => res.push(3));
589-
let f4 = () => TPromise.timeout(20).then(() => res.push(4));
590-
let f5 = () => TPromise.timeout(0).then(() => res.push(5));
571+
let f4 = () => TPromise.wrap(async.timeout(20)).then(() => res.push(4));
572+
let f5 = () => TPromise.wrap(async.timeout(0)).then(() => res.push(5));
591573

592574
return queue.queue(f1).then(() => {
593575
return queue.queue(f2).then(() => {
@@ -616,9 +598,9 @@ suite('Async', () => {
616598

617599
let res: number[] = [];
618600

619-
let f1 = () => TPromise.timeout(10).then(() => res.push(2));
620-
let f2 = () => TPromise.timeout(20).then(() => res.push(4));
621-
let f3 = () => TPromise.timeout(0).then(() => res.push(5));
601+
let f1 = () => TPromise.wrap(async.timeout(10)).then(() => res.push(2));
602+
let f2 = () => TPromise.wrap(async.timeout(20)).then(() => res.push(4));
603+
let f3 = () => TPromise.wrap(async.timeout(0)).then(() => res.push(5));
622604

623605
const q1 = queue.queue(f1);
624606
const q2 = queue.queue(f2);

src/vs/base/test/common/cache.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import * as assert from 'assert';
99
import Cache from 'vs/base/common/cache';
1010
import { TPromise } from 'vs/base/common/winjs.base';
11-
import { createCancelablePromise, wireCancellationToken } from 'vs/base/common/async';
11+
import { createCancelablePromise, timeout } from 'vs/base/common/async';
1212

1313
suite('Cache', () => {
1414

@@ -37,7 +37,7 @@ suite('Cache', () => {
3737

3838
const cache = new Cache(() => {
3939
counter1++;
40-
return createCancelablePromise(token => wireCancellationToken(token, TPromise.timeout(1).then(() => counter2++)));
40+
return timeout(2).cancelableThen(() => counter2++);
4141
});
4242

4343
assert.equal(counter1, 0);

src/vs/base/test/common/event.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ suite('Event', function () {
196196
// If the source event is fired once, the debounced (on the leading edge) event should be fired only once
197197
emitter.fire();
198198

199-
return TPromise.timeout(1).then(() => {
199+
return timeout(1).then(() => {
200200
assert.equal(calls, 1);
201201
});
202202
});
@@ -214,7 +214,7 @@ suite('Event', function () {
214214
emitter.fire();
215215
emitter.fire();
216216
emitter.fire();
217-
return TPromise.timeout(1).then(() => {
217+
return timeout(1).then(() => {
218218
assert.equal(calls, 2);
219219
});
220220
});
@@ -422,15 +422,15 @@ suite('Event utils', () => {
422422

423423
assert.equal(count, 0);
424424

425-
return TPromise.timeout(10).then(() => {
425+
return timeout(10).then(() => {
426426
assert.equal(count, 1);
427427
});
428428
});
429429

430430
test('should emit when done - setTimeout', async () => {
431431
let count = 0;
432432

433-
const promise = TPromise.timeout(5);
433+
const promise = timeout(5);
434434
const event = fromPromise(promise);
435435
event(() => count++);
436436

@@ -500,7 +500,7 @@ suite('Event utils', () => {
500500
const listener = bufferedEvent(num => result.push(num));
501501
assert.deepEqual(result, []);
502502

503-
return TPromise.timeout(10).then(() => {
503+
return timeout(10).then(() => {
504504
emitter.fire(4);
505505
assert.deepEqual(result, [1, 2, 3, 4]);
506506

src/vs/platform/driver/electron-browser/driver.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { getTopLeftOffset, getClientArea } from 'vs/base/browser/dom';
1414
import * as electron from 'electron';
1515
import { IWindowService } from 'vs/platform/windows/common/windows';
1616
import { Terminal } from 'vscode-xterm';
17+
import { timeout } from 'vs/base/common/async';
1718

1819
function serializeElement(element: Element, recursive: boolean): IElement {
1920
const attributes = Object.create(null);
@@ -89,9 +90,9 @@ class WindowDriver implements IWindowDriver {
8990
const webContents: electron.WebContents = (electron as any).remote.getCurrentWebContents();
9091
webContents.sendInputEvent({ type: 'mouseDown', x, y, button: 'left', clickCount } as any);
9192

92-
return TPromise.timeout(10).then(() => {
93+
return TPromise.wrap(timeout(10)).then(() => {
9394
webContents.sendInputEvent({ type: 'mouseUp', x, y, button: 'left', clickCount } as any);
94-
return TPromise.timeout(100);
95+
return TPromise.wrap(timeout(100));
9596
});
9697
});
9798
}

src/vs/platform/driver/electron-main/driver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { Emitter, toPromise } from 'vs/base/common/event';
1919
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
2020
import { ScanCodeBinding } from 'vs/base/common/scanCode';
2121
import { KeybindingParser } from 'vs/base/common/keybindingParser';
22+
import { timeout } from 'vs/base/common/async';
2223

2324
class WindowRouter implements IClientRouter {
2425

@@ -136,7 +137,7 @@ export class Driver implements IDriver, IWindowDriverRegistry {
136137

137138
webContents.sendInputEvent({ type: 'keyUp', keyCode, modifiers } as any);
138139

139-
return TPromise.timeout(100);
140+
return TPromise.wrap(timeout(100));
140141
}
141142

142143
click(windowId: number, selector: string, xoffset?: number, yoffset?: number): TPromise<void> {

0 commit comments

Comments
 (0)