-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bundler: Handle export * properly (#1083)
- Loading branch information
Showing
11 changed files
with
321 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"jsc": { | ||
"target": "es2020", | ||
"parser": { | ||
"syntax": "typescript", | ||
"decorators": true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. | ||
// TODO(ry) It'd be better to make Deferred a class that inherits from | ||
// Promise, rather than an interface. This is possible in ES2016, however | ||
// typescript produces broken code when targeting ES5 code. | ||
// See https://github.com/Microsoft/TypeScript/issues/15202 | ||
// At the time of writing, the github issue is closed but the problem remains. | ||
export interface Deferred<T> extends Promise<T> { | ||
resolve: (value?: T | PromiseLike<T>) => void; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
reject: (reason?: any) => void; | ||
} | ||
|
||
/** Creates a Promise with the `reject` and `resolve` functions | ||
* placed as methods on the promise object itself. It allows you to do: | ||
* | ||
* const p = deferred<number>(); | ||
* // ... | ||
* p.resolve(42); | ||
*/ | ||
export function deferred<T>(): Deferred<T> { | ||
let methods; | ||
const promise = new Promise<T>((resolve, reject): void => { | ||
methods = { resolve, reject }; | ||
}); | ||
return Object.assign(promise, methods) as Deferred<T>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. | ||
/* Resolves after the given number of milliseconds. */ | ||
export function delay(ms: number): Promise<void> { | ||
return new Promise((res): number => | ||
setTimeout((): void => { | ||
res(); | ||
}, ms) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. | ||
export * from "./deferred"; | ||
export * from "./delay"; | ||
export * from "./mux_async_iterator"; | ||
export * from "./pool"; |
69 changes: 69 additions & 0 deletions
69
spack/tests/pass/deno-002/full/input/async/mux_async_iterator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. | ||
import { Deferred, deferred } from "./deferred.ts"; | ||
|
||
interface TaggedYieldedValue<T> { | ||
iterator: AsyncIterableIterator<T>; | ||
value: T; | ||
} | ||
|
||
/** The MuxAsyncIterator class multiplexes multiple async iterators into a | ||
* single stream. It currently makes an assumption: | ||
* - The final result (the value returned and not yielded from the iterator) | ||
* does not matter; if there is any, it is discarded. | ||
*/ | ||
export class MuxAsyncIterator<T> implements AsyncIterable<T> { | ||
private iteratorCount = 0; | ||
private yields: Array<TaggedYieldedValue<T>> = []; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
private throws: any[] = []; | ||
private signal: Deferred<void> = deferred(); | ||
|
||
add(iterator: AsyncIterableIterator<T>): void { | ||
++this.iteratorCount; | ||
this.callIteratorNext(iterator); | ||
} | ||
|
||
private async callIteratorNext( | ||
iterator: AsyncIterableIterator<T>, | ||
): Promise<void> { | ||
try { | ||
const { value, done } = await iterator.next(); | ||
if (done) { | ||
--this.iteratorCount; | ||
} else { | ||
this.yields.push({ iterator, value }); | ||
} | ||
} catch (e) { | ||
this.throws.push(e); | ||
} | ||
this.signal.resolve(); | ||
} | ||
|
||
async *iterate(): AsyncIterableIterator<T> { | ||
while (this.iteratorCount > 0) { | ||
// Sleep until any of the wrapped iterators yields. | ||
await this.signal; | ||
|
||
// Note that while we're looping over `yields`, new items may be added. | ||
for (let i = 0; i < this.yields.length; i++) { | ||
const { iterator, value } = this.yields[i]; | ||
yield value; | ||
this.callIteratorNext(iterator); | ||
} | ||
|
||
if (this.throws.length) { | ||
for (const e of this.throws) { | ||
throw e; | ||
} | ||
this.throws.length = 0; | ||
} | ||
// Clear the `yields` list and reset the `signal` promise. | ||
this.yields.length = 0; | ||
this.signal = deferred(); | ||
} | ||
} | ||
|
||
[Symbol.asyncIterator](): AsyncIterableIterator<T> { | ||
return this.iterate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. | ||
|
||
/** | ||
* pooledMap transforms values from an (async) iterable into another async | ||
* iterable. The transforms are done concurrently, with a max concurrency | ||
* defined by the poolLimit. | ||
* | ||
* @param poolLimit The maximum count of items being processed concurrently. | ||
* @param array The input array for mapping. | ||
* @param iteratorFn The function to call for every item of the array. | ||
*/ | ||
export function pooledMap<T, R>( | ||
poolLimit: number, | ||
array: Iterable<T> | AsyncIterable<T>, | ||
iteratorFn: (data: T) => Promise<R>, | ||
): AsyncIterableIterator<R> { | ||
// Create the async iterable that is returned from this function. | ||
const res = new TransformStream<Promise<R>, R>({ | ||
async transform( | ||
p: Promise<R>, | ||
controller: TransformStreamDefaultController<R>, | ||
): Promise<void> { | ||
controller.enqueue(await p); | ||
}, | ||
}); | ||
// Start processing items from the iterator | ||
(async (): Promise<void> => { | ||
const writer = res.writable.getWriter(); | ||
const executing: Array<Promise<unknown>> = []; | ||
for await (const item of array) { | ||
const p = Promise.resolve().then(() => iteratorFn(item)); | ||
writer.write(p); | ||
const e: Promise<unknown> = p.then(() => | ||
executing.splice(executing.indexOf(e), 1) | ||
); | ||
executing.push(e); | ||
if (executing.length >= poolLimit) { | ||
await Promise.race(executing); | ||
} | ||
} | ||
// Wait until all ongoing events have processed, then close the writer. | ||
await Promise.all(executing); | ||
writer.close(); | ||
})(); | ||
return res.readable.getIterator(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './async/mod' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. | ||
/* Resolves after the given number of milliseconds. */ export function delay(ms) { | ||
return new Promise((res)=>setTimeout(()=>{ | ||
res(); | ||
}, ms) | ||
); | ||
} | ||
function deferred1() { | ||
let methods; | ||
const promise = new Promise((resolve, reject)=>{ | ||
}); | ||
return Object.assign(promise, methods); | ||
} | ||
var tmp = Symbol.asyncIterator; | ||
/** The MuxAsyncIterator class multiplexes multiple async iterators into a | ||
* single stream. It currently makes an assumption: | ||
* - The final result (the value returned and not yielded from the iterator) | ||
* does not matter; if there is any, it is discarded. | ||
*/ export class MuxAsyncIterator { | ||
add(iterator) { | ||
++this.iteratorCount; | ||
this.callIteratorNext(iterator); | ||
} | ||
async callIteratorNext(iterator) { | ||
try { | ||
const { value , done } = await iterator.next(); | ||
if (done) --this.iteratorCount; | ||
else this.yields.push({ | ||
iterator, | ||
value | ||
}); | ||
} catch (e) { | ||
this.throws.push(e); | ||
} | ||
this.signal.resolve(); | ||
} | ||
async *iterate() { | ||
while(this.iteratorCount > 0){ | ||
// Sleep until any of the wrapped iterators yields. | ||
await this.signal; | ||
// Note that while we're looping over `yields`, new items may be added. | ||
for(let i = 0; i < this.yields.length; i++){ | ||
const { iterator , value } = this.yields[i]; | ||
yield value; | ||
this.callIteratorNext(iterator); | ||
} | ||
if (this.throws.length) { | ||
for (const e of this.throws)throw e; | ||
this.throws.length = 0; | ||
} | ||
// Clear the `yields` list and reset the `signal` promise. | ||
this.yields.length = 0; | ||
this.signal = deferred1(); | ||
} | ||
} | ||
[tmp]() { | ||
return this.iterate(); | ||
} | ||
constructor(){ | ||
this.iteratorCount = 0; | ||
this.yields = []; | ||
this.throws = []; | ||
this.signal = deferred1(); | ||
} | ||
} | ||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. | ||
/** | ||
* pooledMap transforms values from an (async) iterable into another async | ||
* iterable. The transforms are done concurrently, with a max concurrency | ||
* defined by the poolLimit. | ||
* | ||
* @param poolLimit The maximum count of items being processed concurrently. | ||
* @param array The input array for mapping. | ||
* @param iteratorFn The function to call for every item of the array. | ||
*/ export function pooledMap(poolLimit, array, iteratorFn) { | ||
// Create the async iterable that is returned from this function. | ||
const res = new TransformStream({ | ||
async transform (p, controller) { | ||
controller.enqueue(await p); | ||
} | ||
}); | ||
// Start processing items from the iterator | ||
(async ()=>{ | ||
const writer = res.writable.getWriter(); | ||
const executing = []; | ||
for await (const item of array){ | ||
const p = Promise.resolve().then(()=>iteratorFn(item) | ||
); | ||
writer.write(p); | ||
const e = p.then(()=>executing.splice(executing.indexOf(e), 1) | ||
); | ||
executing.push(e); | ||
if (executing.length >= poolLimit) await Promise.race(executing); | ||
} | ||
// Wait until all ongoing events have processed, then close the writer. | ||
await Promise.all(executing); | ||
writer.close(); | ||
})(); | ||
return res.readable.getIterator(); | ||
} | ||
export { deferred1 as deferred }; |