Skip to content

Commit

Permalink
wait a little when between edits the ratio of symbols and lines chang…
Browse files Browse the repository at this point in the history
…es by more than half, microsoft#49975
  • Loading branch information
jrieken committed May 17, 2018
1 parent 19bf185 commit 6152160
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 55 deletions.
18 changes: 18 additions & 0 deletions src/vs/base/common/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ export function wireCancellationToken<T>(token: CancellationToken, promise: TPro
return always(promise, () => subscription.dispose());
}

export function asDisposablePromise<T>(input: Thenable<T>, cancelValue?: T, bucket?: IDisposable[]): { promise: Thenable<T> } & IDisposable {
let dispose: () => void;
let promise = new TPromise((resolve, reject) => {
dispose = function () {
resolve(cancelValue);
};
input.then(resolve, reject);
});
let res = {
promise,
dispose
};
if (Array.isArray(bucket)) {
bucket.push(res);
}
return res;
}

export interface ITask<T> {
(): T;
}
Expand Down
81 changes: 46 additions & 35 deletions src/vs/base/test/common/async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,22 @@
'use strict';

import * as assert from 'assert';
import { Promise, TPromise } from 'vs/base/common/winjs.base';
import { TPromise } from 'vs/base/common/winjs.base';
import * as Async from 'vs/base/common/async';
import URI from 'vs/base/common/uri';

suite('Async', () => {

test('asDisposablePromise', async function () {
let value = await Async.asDisposablePromise(TPromise.as(1)).promise;
assert.equal(value, 1);

let disposablePromise = Async.asDisposablePromise(TPromise.timeout(1000).then(_ => 1), 2);
disposablePromise.dispose();
value = await disposablePromise.promise;
assert.equal(value, 2);
});

test('Throttler - non async', function () {
let count = 0;
let factory = () => {
Expand All @@ -18,7 +29,7 @@ suite('Async', () => {

let throttler = new Async.Throttler();

return Promise.join([
return TPromise.join([
throttler.queue(factory).then((result) => { assert.equal(result, 1); }),
throttler.queue(factory).then((result) => { assert.equal(result, 2); }),
throttler.queue(factory).then((result) => { assert.equal(result, 3); }),
Expand All @@ -37,14 +48,14 @@ suite('Async', () => {

let throttler = new Async.Throttler();

return Promise.join([
return TPromise.join([
throttler.queue(factory).then((result) => { assert.equal(result, 1); }),
throttler.queue(factory).then((result) => { assert.equal(result, 2); }),
throttler.queue(factory).then((result) => { assert.equal(result, 2); }),
throttler.queue(factory).then((result) => { assert.equal(result, 2); }),
throttler.queue(factory).then((result) => { assert.equal(result, 2); })
]).then(() => {
Promise.join([
TPromise.join([
throttler.queue(factory).then((result) => { assert.equal(result, 3); }),
throttler.queue(factory).then((result) => { assert.equal(result, 4); }),
throttler.queue(factory).then((result) => { assert.equal(result, 4); }),
Expand All @@ -63,9 +74,9 @@ suite('Async', () => {
};

let throttler = new Async.Throttler();
let p1: Promise;
let p1: TPromise;

const p = Promise.join([
const p = TPromise.join([
p1 = throttler.queue(factory).then((result) => { assert(false, 'should not be here, 1'); }, () => { assert(true, 'yes, it was cancelled'); }),
throttler.queue(factory).then((result) => { assert.equal(result, 1); }, () => { assert(false, 'should not be here, 2'); }),
throttler.queue(factory).then((result) => { assert.equal(result, 1); }, () => { assert(false, 'should not be here, 3'); }),
Expand All @@ -86,9 +97,9 @@ suite('Async', () => {
};

let throttler = new Async.Throttler();
let p2: Promise;
let p2: TPromise;

const p = Promise.join([
const p = TPromise.join([
throttler.queue(factory).then((result) => { assert.equal(result, 1); }, () => { assert(false, 'should not be here, 1'); }),
p2 = throttler.queue(factory).then((result) => { assert(false, 'should not be here, 2'); }, () => { assert(true, 'yes, it was cancelled'); }),
throttler.queue(factory).then((result) => { assert.equal(result, 2); }, () => { assert(false, 'should not be here, 3'); }),
Expand All @@ -109,9 +120,9 @@ suite('Async', () => {
};

let throttler = new Async.Throttler();
let p3: Promise;
let p3: TPromise;

const p = Promise.join([
const p = TPromise.join([
throttler.queue(factory).then((result) => { assert.equal(result, 1); }, () => { assert(false, 'should not be here, 1'); }),
throttler.queue(factory).then((result) => { assert.equal(result, 2); }, () => { assert(false, 'should not be here, 2'); }),
p3 = throttler.queue(factory).then((result) => { assert(false, 'should not be here, 3'); }, () => { assert(true, 'yes, it was cancelled'); }),
Expand All @@ -130,13 +141,13 @@ suite('Async', () => {

let throttler = new Async.Throttler();

let promises: Promise[] = [];
let promises: TPromise[] = [];

promises.push(throttler.queue(factoryFactory(1)).then((n) => { assert.equal(n, 1); }));
promises.push(throttler.queue(factoryFactory(2)).then((n) => { assert.equal(n, 3); }));
promises.push(throttler.queue(factoryFactory(3)).then((n) => { assert.equal(n, 3); }));

return Promise.join(promises);
return TPromise.join(promises);
});

test('Throttler - progress should work', function () {
Expand All @@ -149,14 +160,14 @@ suite('Async', () => {
});

let throttler = new Async.Throttler();
let promises: Promise[] = [];
let promises: TPromise[] = [];
let progresses: any[][] = [[], [], []];

promises.push(throttler.queue(factory).then(null, null, (p) => progresses[0].push(p)));
promises.push(throttler.queue(factory).then(null, null, (p) => progresses[1].push(p)));
promises.push(throttler.queue(factory).then(null, null, (p) => progresses[2].push(p)));

return Promise.join(promises).then(() => {
return TPromise.join(promises).then(() => {
assert.deepEqual(progresses[0], [0]);
assert.deepEqual(progresses[1], [0]);
assert.deepEqual(progresses[2], [0]);
Expand All @@ -170,7 +181,7 @@ suite('Async', () => {
};

let delayer = new Async.Delayer(0);
let promises: Promise[] = [];
let promises: TPromise[] = [];

assert(!delayer.isTriggered());

Expand All @@ -183,7 +194,7 @@ suite('Async', () => {
promises.push(delayer.trigger(factory).then((result) => { assert.equal(result, 1); assert(!delayer.isTriggered()); }));
assert(delayer.isTriggered());

return Promise.join(promises).then(() => {
return TPromise.join(promises).then(() => {
assert(!delayer.isTriggered());
});
});
Expand Down Expand Up @@ -218,7 +229,7 @@ suite('Async', () => {
};

let delayer = new Async.Delayer(0);
let promises: Promise[] = [];
let promises: TPromise[] = [];

assert(!delayer.isTriggered());

Expand All @@ -233,7 +244,7 @@ suite('Async', () => {

delayer.cancel();

return Promise.join(promises).then(() => {
return TPromise.join(promises).then(() => {
assert(!delayer.isTriggered());
});
});
Expand All @@ -245,7 +256,7 @@ suite('Async', () => {
};

let delayer = new Async.Delayer(0);
let promises: Promise[] = [];
let promises: TPromise[] = [];

assert(!delayer.isTriggered());

Expand All @@ -261,7 +272,7 @@ suite('Async', () => {

delayer.cancel();

const p = Promise.join(promises).then(() => {
const p = TPromise.join(promises).then(() => {
promises = [];

assert(!delayer.isTriggered());
Expand All @@ -272,7 +283,7 @@ suite('Async', () => {
promises.push(delayer.trigger(factory).then(() => { assert.equal(result, 1); assert(!delayer.isTriggered()); }));
assert(delayer.isTriggered());

const p = Promise.join(promises).then(() => {
const p = TPromise.join(promises).then(() => {
assert(!delayer.isTriggered());
});

Expand All @@ -297,15 +308,15 @@ suite('Async', () => {
};

let delayer = new Async.Delayer(0);
let promises: Promise[] = [];
let promises: TPromise[] = [];

assert(!delayer.isTriggered());

promises.push(delayer.trigger(factoryFactory(1)).then((n) => { assert.equal(n, 3); }));
promises.push(delayer.trigger(factoryFactory(2)).then((n) => { assert.equal(n, 3); }));
promises.push(delayer.trigger(factoryFactory(3)).then((n) => { assert.equal(n, 3); }));

const p = Promise.join(promises).then(() => {
const p = TPromise.join(promises).then(() => {
assert(!delayer.isTriggered());
});

Expand All @@ -324,14 +335,14 @@ suite('Async', () => {
});

let delayer = new Async.Delayer(0);
let promises: Promise[] = [];
let promises: TPromise[] = [];
let progresses: any[][] = [[], [], []];

promises.push(delayer.trigger(factory).then(null, null, (p) => progresses[0].push(p)));
promises.push(delayer.trigger(factory).then(null, null, (p) => progresses[1].push(p)));
promises.push(delayer.trigger(factory).then(null, null, (p) => progresses[2].push(p)));

return Promise.join(promises).then(() => {
return TPromise.join(promises).then(() => {
assert.deepEqual(progresses[0], [0]);
assert.deepEqual(progresses[1], [0]);
assert.deepEqual(progresses[2], [0]);
Expand All @@ -348,14 +359,14 @@ suite('Async', () => {
});

let delayer = new Async.ThrottledDelayer(0);
let promises: Promise[] = [];
let promises: TPromise[] = [];
let progresses: any[][] = [[], [], []];

promises.push(delayer.trigger(factory).then(null, null, (p) => progresses[0].push(p)));
promises.push(delayer.trigger(factory).then(null, null, (p) => progresses[1].push(p)));
promises.push(delayer.trigger(factory).then(null, null, (p) => progresses[2].push(p)));

return Promise.join(promises).then(() => {
return TPromise.join(promises).then(() => {
assert.deepEqual(progresses[0], [0]);
assert.deepEqual(progresses[1], [0]);
assert.deepEqual(progresses[2], [0]);
Expand Down Expand Up @@ -390,18 +401,18 @@ suite('Async', () => {

let limiter = new Async.Limiter(1);

let promises: Promise[] = [];
let promises: TPromise[] = [];
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(n => promises.push(limiter.queue(factoryFactory(n))));

return Promise.join(promises).then((res) => {
return TPromise.join(promises).then((res) => {
assert.equal(10, res.length);

limiter = new Async.Limiter(100);

promises = [];
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(n => promises.push(limiter.queue(factoryFactory(n))));

return Promise.join(promises).then((res) => {
return TPromise.join(promises).then((res) => {
assert.equal(10, res.length);
});
});
Expand All @@ -413,18 +424,18 @@ suite('Async', () => {
};

let limiter = new Async.Limiter(1);
let promises: Promise[] = [];
let promises: TPromise[] = [];
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(n => promises.push(limiter.queue(factoryFactory(n))));

return Promise.join(promises).then((res) => {
return TPromise.join(promises).then((res) => {
assert.equal(10, res.length);

limiter = new Async.Limiter(100);

promises = [];
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(n => promises.push(limiter.queue(factoryFactory(n))));

return Promise.join(promises).then((res) => {
return TPromise.join(promises).then((res) => {
assert.equal(10, res.length);
});
});
Expand All @@ -440,10 +451,10 @@ suite('Async', () => {

let limiter = new Async.Limiter(5);

let promises: Promise[] = [];
let promises: TPromise[] = [];
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].forEach(n => promises.push(limiter.queue(factoryFactory(n))));

return Promise.join(promises).then((res) => {
return TPromise.join(promises).then((res) => {
assert.equal(10, res.length);
assert.deepEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], res);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ export abstract class TreeElement {
}
return undefined;
}

static size(element: TreeElement): number {
let res = 1;
for (const key in element.children) {
res += TreeElement.size(element.children[key]);
}
return res;
}
}

export class OutlineElement extends TreeElement {
Expand Down
Loading

0 comments on commit 6152160

Please sign in to comment.