Skip to content

Commit

Permalink
streams - introduce ReadableStreamEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
bpasero committed Nov 2, 2019
1 parent d3405c4 commit 593c968
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/vs/base/common/buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export function bufferToStream(buffer: VSBuffer): streams.ReadableStream<VSBuffe
return streams.toStream<VSBuffer>(buffer, chunks => VSBuffer.concat(chunks));
}

export function streamToBufferReadableStream(stream: streams.ReadableStream<Uint8Array | string>): streams.ReadableStream<VSBuffer> {
export function streamToBufferReadableStream(stream: streams.ReadableStreamEvents<Uint8Array | string>): streams.ReadableStream<VSBuffer> {
return streams.transform<Uint8Array | string, VSBuffer>(stream, data => typeof data === 'string' ? VSBuffer.fromString(data) : VSBuffer.wrap(data), chunks => VSBuffer.concat(chunks));
}

Expand Down
17 changes: 10 additions & 7 deletions src/vs/base/common/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

/**
* A interface that emulates the API shape of a node.js readable
* stream for use in desktop and web environments.
*/
export interface ReadableStream<T> {
export interface ReadableStreamEvents<T> {

/**
* The 'data' event is emitted whenever the stream is
* relinquishing ownership of a chunk of data to a consumer.
*/
on(event: 'data', callback: (chunk: T) => void): void;
on(event: 'data', callback: (data: T) => void): void;

/**
* Emitted when any error occurs.
Expand All @@ -26,6 +22,13 @@ export interface ReadableStream<T> {
* not be emitted unless the data is completely consumed.
*/
on(event: 'end', callback: () => void): void;
}

/**
* A interface that emulates the API shape of a node.js readable
* stream for use in desktop and web environments.
*/
export interface ReadableStream<T> extends ReadableStreamEvents<T> {

/**
* Stops emitting any events until resume() is called.
Expand Down Expand Up @@ -352,7 +355,7 @@ export function toReadable<T>(t: T): Readable<T> {
};
}

export function transform<S, T>(stream: ReadableStream<S>, transformer: ITransformer<S, T>, reducer: IReducer<T>): ReadableStream<T> {
export function transform<S, T>(stream: ReadableStreamEvents<S>, transformer: ITransformer<S, T>, reducer: IReducer<T>): ReadableStream<T> {
const target = newWriteableStream<T>(reducer);

stream.on('data', data => target.write(transformer(data)));
Expand Down

0 comments on commit 593c968

Please sign in to comment.