Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Iterator and AsyncIterator with Symbol.dispose. #59264

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/lib/esnext.disposable.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/// <reference lib="es2015.symbol" />
/// <reference lib="es2015.iterable" />
/// <reference lib="es2018.asynciterable" />

interface SymbolConstructor {
/**
Expand Down Expand Up @@ -165,3 +167,10 @@ interface AsyncDisposableStackConstructor {
readonly prototype: AsyncDisposableStack;
}
declare var AsyncDisposableStack: AsyncDisposableStackConstructor;

interface Iterator<T, TReturn = any, TNext = undefined> {
[Symbol.dispose](): void;
}
interface AsyncIterator<T, TReturn = any, TNext = undefined> {
[Symbol.dispose](): Promise<void>;
}
2 changes: 2 additions & 0 deletions src/testRunner/unittests/evaluation/forAwaitOf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe("unittests:: evaluation:: forAwaitOfEvaluation", () => {
let i = 0;
const iterator: IterableIterator<any> = {
[Symbol.iterator]() { return this; },
[Symbol.dispose]() { },
next() {
switch (i++) {
case 0: return { value: 1, done: false };
Expand Down Expand Up @@ -37,6 +38,7 @@ describe("unittests:: evaluation:: forAwaitOfEvaluation", () => {
let i = 0;
const iterator: IterableIterator<any> = {
[Symbol.iterator]() { return this; },
[Symbol.dispose]() { },
next() {
switch (i++) {
case 0: return { value: 1, done: false };
Expand Down
6 changes: 6 additions & 0 deletions tests/baselines/reference/customAsyncIterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class ConstantIterator<T> implements AsyncIterator<T, undefined, T | undefined>
}
return { value: this.constant, done: false };
}
async [Symbol.dispose]() {
return;
}
}

//// [customAsyncIterator.js]
Expand All @@ -25,4 +28,7 @@ class ConstantIterator {
}
return { value: this.constant, done: false };
}
async [Symbol.dispose]() {
return;
}
}
10 changes: 9 additions & 1 deletion tests/baselines/reference/customAsyncIterator.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class ConstantIterator<T> implements AsyncIterator<T, undefined, T | undefined> {
>ConstantIterator : Symbol(ConstantIterator, Decl(customAsyncIterator.ts, 0, 0))
>T : Symbol(T, Decl(customAsyncIterator.ts, 1, 23))
>AsyncIterator : Symbol(AsyncIterator, Decl(lib.es2018.asynciterable.d.ts, --, --))
>AsyncIterator : Symbol(AsyncIterator, Decl(lib.es2018.asynciterable.d.ts, --, --), Decl(lib.esnext.disposable.d.ts, --, --))
>T : Symbol(T, Decl(customAsyncIterator.ts, 1, 23))
>T : Symbol(T, Decl(customAsyncIterator.ts, 1, 23))

Expand Down Expand Up @@ -34,4 +34,12 @@ class ConstantIterator<T> implements AsyncIterator<T, undefined, T | undefined>
>constant : Symbol(ConstantIterator.constant, Decl(customAsyncIterator.ts, 2, 16))
>done : Symbol(done, Decl(customAsyncIterator.ts, 8, 38))
}
async [Symbol.dispose]() {
>[Symbol.dispose] : Symbol(ConstantIterator[Symbol.dispose], Decl(customAsyncIterator.ts, 9, 5))
>Symbol.dispose : Symbol(SymbolConstructor.dispose, Decl(lib.esnext.disposable.d.ts, --, --))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
>dispose : Symbol(SymbolConstructor.dispose, Decl(lib.esnext.disposable.d.ts, --, --))

return;
}
}
12 changes: 12 additions & 0 deletions tests/baselines/reference/customAsyncIterator.types
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,16 @@ class ConstantIterator<T> implements AsyncIterator<T, undefined, T | undefined>
>false : false
> : ^^^^^
}
async [Symbol.dispose]() {
>[Symbol.dispose] : () => Promise<void>
> : ^^^^^^^^^^^^^^^^^^^
>Symbol.dispose : unique symbol
> : ^^^^^^^^^^^^^
>Symbol : SymbolConstructor
> : ^^^^^^^^^^^^^^^^^
>dispose : unique symbol
> : ^^^^^^^^^^^^^

return;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ const reducerBroken = (state: number, { type, payload }: Action3) => {

declare var it: Iterator<number>;
>it : Symbol(it, Decl(dependentDestructuredVariables.ts, 183, 11))
>Iterator : Symbol(Iterator, Decl(lib.es2015.iterable.d.ts, --, --))
>Iterator : Symbol(Iterator, Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.esnext.disposable.d.ts, --, --))

const { value, done } = it.next();
>value : Symbol(value, Decl(dependentDestructuredVariables.ts, 184, 7))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,35 +163,43 @@ function mapAsyncIterable<T, U, R = undefined>(
>this : Symbol(AsyncGenerator, Decl(lib.es2018.asyncgenerator.d.ts, --, --))

},
async [Symbol.dispose]() {
>[Symbol.dispose] : Symbol([Symbol.dispose], Decl(discriminateWithOptionalProperty2.ts, 46, 6))
>Symbol.dispose : Symbol(SymbolConstructor.dispose, Decl(lib.esnext.disposable.d.ts, --, --))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
>dispose : Symbol(SymbolConstructor.dispose, Decl(lib.esnext.disposable.d.ts, --, --))

return;
},
};
}

const items = [1, 2, 3];
>items : Symbol(items, Decl(discriminateWithOptionalProperty2.ts, 50, 5))
>items : Symbol(items, Decl(discriminateWithOptionalProperty2.ts, 53, 5))

const iterable = {
>iterable : Symbol(iterable, Decl(discriminateWithOptionalProperty2.ts, 52, 5))
>iterable : Symbol(iterable, Decl(discriminateWithOptionalProperty2.ts, 55, 5))

[Symbol.asyncIterator]() {
>[Symbol.asyncIterator] : Symbol([Symbol.asyncIterator], Decl(discriminateWithOptionalProperty2.ts, 52, 18))
>[Symbol.asyncIterator] : Symbol([Symbol.asyncIterator], Decl(discriminateWithOptionalProperty2.ts, 55, 18))
>Symbol.asyncIterator : Symbol(SymbolConstructor.asyncIterator, Decl(lib.es2018.asynciterable.d.ts, --, --))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
>asyncIterator : Symbol(SymbolConstructor.asyncIterator, Decl(lib.es2018.asynciterable.d.ts, --, --))

return this;
>this : Symbol(iterable, Decl(discriminateWithOptionalProperty2.ts, 52, 16))
>this : Symbol(iterable, Decl(discriminateWithOptionalProperty2.ts, 55, 16))

},
next() {
>next : Symbol(next, Decl(discriminateWithOptionalProperty2.ts, 55, 4))
>next : Symbol(next, Decl(discriminateWithOptionalProperty2.ts, 58, 4))

const value = items[0];
>value : Symbol(value, Decl(discriminateWithOptionalProperty2.ts, 57, 9))
>items : Symbol(items, Decl(discriminateWithOptionalProperty2.ts, 50, 5))
>value : Symbol(value, Decl(discriminateWithOptionalProperty2.ts, 60, 9))
>items : Symbol(items, Decl(discriminateWithOptionalProperty2.ts, 53, 5))

items.shift();
>items.shift : Symbol(Array.shift, Decl(lib.es5.d.ts, --, --))
>items : Symbol(items, Decl(discriminateWithOptionalProperty2.ts, 50, 5))
>items : Symbol(items, Decl(discriminateWithOptionalProperty2.ts, 53, 5))
>shift : Symbol(Array.shift, Decl(lib.es5.d.ts, --, --))

return Promise.resolve({
Expand All @@ -200,23 +208,31 @@ const iterable = {
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))

done: items.length === 0,
>done : Symbol(done, Decl(discriminateWithOptionalProperty2.ts, 59, 28))
>done : Symbol(done, Decl(discriminateWithOptionalProperty2.ts, 62, 28))
>items.length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
>items : Symbol(items, Decl(discriminateWithOptionalProperty2.ts, 50, 5))
>items : Symbol(items, Decl(discriminateWithOptionalProperty2.ts, 53, 5))
>length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))

value,
>value : Symbol(value, Decl(discriminateWithOptionalProperty2.ts, 60, 31))
>value : Symbol(value, Decl(discriminateWithOptionalProperty2.ts, 63, 31))

});
},
async [Symbol.dispose]() {
>[Symbol.dispose] : Symbol([Symbol.dispose], Decl(discriminateWithOptionalProperty2.ts, 66, 4))
>Symbol.dispose : Symbol(SymbolConstructor.dispose, Decl(lib.esnext.disposable.d.ts, --, --))
>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.symbol.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2019.symbol.d.ts, --, --))
>dispose : Symbol(SymbolConstructor.dispose, Decl(lib.esnext.disposable.d.ts, --, --))

return;
}
};

const doubles = mapAsyncIterable(iterable, (x) => x + x);
>doubles : Symbol(doubles, Decl(discriminateWithOptionalProperty2.ts, 66, 5))
>doubles : Symbol(doubles, Decl(discriminateWithOptionalProperty2.ts, 72, 5))
>mapAsyncIterable : Symbol(mapAsyncIterable, Decl(discriminateWithOptionalProperty2.ts, 2, 40))
>iterable : Symbol(iterable, Decl(discriminateWithOptionalProperty2.ts, 52, 5))
>x : Symbol(x, Decl(discriminateWithOptionalProperty2.ts, 66, 44))
>x : Symbol(x, Decl(discriminateWithOptionalProperty2.ts, 66, 44))
>x : Symbol(x, Decl(discriminateWithOptionalProperty2.ts, 66, 44))
>iterable : Symbol(iterable, Decl(discriminateWithOptionalProperty2.ts, 55, 5))
>x : Symbol(x, Decl(discriminateWithOptionalProperty2.ts, 72, 44))
>x : Symbol(x, Decl(discriminateWithOptionalProperty2.ts, 72, 44))
>x : Symbol(x, Decl(discriminateWithOptionalProperty2.ts, 72, 44))

Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ function mapAsyncIterable<T, U, R = undefined>(
}

return {
>{ async next() { return mapResult(await iterator.next()); }, async return(): Promise<IteratorResult<U, R>> { return typeof iterator.return === "function" ? mapResult(await iterator.return()) : { value: undefined as any, done: true }; }, async throw(error?: unknown) { if (typeof iterator.throw === "function") { return mapResult(await iterator.throw(error)); } throw error; }, [Symbol.asyncIterator]() { return this; }, } : { next(): Promise<IteratorResult<U, R>>; return(): Promise<IteratorResult<U, R>>; throw(error?: unknown): Promise<IteratorResult<U, R>>; [Symbol.asyncIterator](): AsyncGenerator<U, R, void>; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ async next() { return mapResult(await iterator.next()); }, async return(): Promise<IteratorResult<U, R>> { return typeof iterator.return === "function" ? mapResult(await iterator.return()) : { value: undefined as any, done: true }; }, async throw(error?: unknown) { if (typeof iterator.throw === "function") { return mapResult(await iterator.throw(error)); } throw error; }, [Symbol.asyncIterator]() { return this; }, async [Symbol.dispose]() { return; }, } : { next(): Promise<IteratorResult<U, R>>; return(): Promise<IteratorResult<U, R>>; throw(error?: unknown): Promise<IteratorResult<U, R>>; [Symbol.asyncIterator](): AsyncGenerator<U, R, void>; [Symbol.dispose](): Promise<void>; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^ ^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

async next() {
>next : () => Promise<IteratorResult<U, R>>
Expand Down Expand Up @@ -256,6 +256,18 @@ function mapAsyncIterable<T, U, R = undefined>(
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^

},
async [Symbol.dispose]() {
>[Symbol.dispose] : () => Promise<void>
> : ^^^^^^^^^^^^^^^^^^^
>Symbol.dispose : unique symbol
> : ^^^^^^^^^^^^^
>Symbol : SymbolConstructor
> : ^^^^^^^^^^^^^^^^^
>dispose : unique symbol
> : ^^^^^^^^^^^^^

return;
},
};
}

Expand All @@ -272,14 +284,14 @@ const items = [1, 2, 3];
> : ^

const iterable = {
>iterable : { [Symbol.asyncIterator](): any; next(): Promise<{ done: boolean; value: number; }>; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ [Symbol.asyncIterator]() { return this; }, next() { const value = items[0]; items.shift(); return Promise.resolve({ done: items.length === 0, value, }); },} : { [Symbol.asyncIterator](): { [Symbol.asyncIterator](): any; next(): Promise<{ done: boolean; value: number; }>; }; next(): Promise<{ done: boolean; value: number; }>; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>iterable : { [Symbol.asyncIterator](): any; next(): Promise<{ done: boolean; value: number; }>; [Symbol.dispose](): Promise<void>; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>{ [Symbol.asyncIterator]() { return this; }, next() { const value = items[0]; items.shift(); return Promise.resolve({ done: items.length === 0, value, }); }, async [Symbol.dispose]() { return; }} : { [Symbol.asyncIterator](): { [Symbol.asyncIterator](): any; next(): Promise<{ done: boolean; value: number; }>; [Symbol.dispose](): Promise<void>; }; next(): Promise<{ done: boolean; value: number; }>; [Symbol.dispose](): Promise<void>; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

[Symbol.asyncIterator]() {
>[Symbol.asyncIterator] : () => { [Symbol.asyncIterator](): any; next(): Promise<{ done: boolean; value: number; }>; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>[Symbol.asyncIterator] : () => { [Symbol.asyncIterator](): any; next(): Promise<{ done: boolean; value: number; }>; [Symbol.dispose](): Promise<void>; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>Symbol.asyncIterator : unique symbol
> : ^^^^^^^^^^^^^
>Symbol : SymbolConstructor
Expand All @@ -288,8 +300,8 @@ const iterable = {
> : ^^^^^^^^^^^^^

return this;
>this : { [Symbol.asyncIterator](): any; next(): Promise<{ done: boolean; value: number; }>; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>this : { [Symbol.asyncIterator](): any; next(): Promise<{ done: boolean; value: number; }>; [Symbol.dispose](): Promise<void>; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

},
next() {
Expand Down Expand Up @@ -348,6 +360,18 @@ const iterable = {

});
},
async [Symbol.dispose]() {
>[Symbol.dispose] : () => Promise<void>
> : ^^^^^^^^^^^^^^^^^^^
>Symbol.dispose : unique symbol
> : ^^^^^^^^^^^^^
>Symbol : SymbolConstructor
> : ^^^^^^^^^^^^^^^^^
>dispose : unique symbol
> : ^^^^^^^^^^^^^

return;
}
};

const doubles = mapAsyncIterable(iterable, (x) => x + x);
Expand All @@ -357,8 +381,8 @@ const doubles = mapAsyncIterable(iterable, (x) => x + x);
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>mapAsyncIterable : <T, U, R = undefined>(iterable: AsyncGenerator<T, R, void> | AsyncIterable<T>, callback: (value: T) => PromiseOrValue<U>) => AsyncGenerator<U, R, void>
> : ^ ^^ ^^ ^^^^^^^^^^^^^^ ^^ ^^ ^^ ^^^^^
>iterable : { [Symbol.asyncIterator](): any; next(): Promise<{ done: boolean; value: number; }>; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>iterable : { [Symbol.asyncIterator](): any; next(): Promise<{ done: boolean; value: number; }>; [Symbol.dispose](): Promise<void>; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>(x) => x + x : (x: number) => number
> : ^ ^^^^^^^^^^^^^^^^^^^
>x : number
Expand Down
Loading
Loading