rpc_ts
Version:
Remote Procedure Calls in TypeScript made simple
150 lines • 4.49 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Streams and operations on streams.
*
* @module ModuleRpcClient
*
* @license
* Copyright (c) Aiden.ai
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const common_1 = require("../common");
const errors_1 = require("./errors");
const events_1 = require("events");
/**
* Returns a stream created from an array that emits all the messages in the
* array then either completes or errors if `error` is specified.
*
* This function is useful for testing.
*
* @example ```Typescript
* const stream = streamFromArray([1, 2, 3, 4]);
* stream.on('message', console.log(message)).on('complete', () => {
* console.log('COMPLETE');
* }).start();
*
* const streamWithError = streamFromArray([1, 2, 3], new Error('error'));
* streamWithError.on('error', err => {
* console.error('ERROR:', err);
* }).start();
* ```
*/
function streamFromArray(array, error) {
return new ArrayStream(array, error);
}
exports.streamFromArray = streamFromArray;
class ArrayStream extends events_1.EventEmitter {
constructor(array, error) {
super();
this.array = array;
this.error = error;
}
start() {
for (const message of this.array) {
this.emit('message', message);
}
if (this.error) {
this.emit('error', this.error);
}
else {
this.emit('complete');
}
return this;
}
cancel() {
console.warn('"cancel" called on a stream created with "streamFromArray" does nothing');
this.emit('canceled');
return this;
}
}
/**
* Applies a function to each of the messages of a stream and streams the result.
*
* @typeparam T The type of the inbound messages.
* @typeparam U The type of the outbound messages.
* @param source The inbound stream to source messages from.
* @param fn The transformation function to apply to the messages of the inbound stream.
* @return The outbound stream.
*
* @example ```TypeScript
* const sourceStream = streamFromArray([1, 2, 3]);
* const result = transformStream(sourceStream, n => n * 2);
* result.on('message', console.log).start();
* // Result emits 2, 4, 6.
* ```
*/
function transformStream(source, fn) {
return new TransformingStream(source, fn);
}
exports.transformStream = transformStream;
class TransformingStream extends events_1.EventEmitter {
constructor(source, fn) {
super();
this.source = source;
source.on('message', message => {
try {
this.emit('message', fn(message));
}
catch (err) {
this.emit('error', err);
}
});
source.on('error', err => {
this.emit('error', err);
});
}
start() {
this.source.start();
return this;
}
cancel() {
this.source.cancel();
return this;
}
on(event, callback) {
if (event === 'message' || event === 'error') {
return super.on(event, callback);
}
this.source.on(event, callback);
return this;
}
}
/**
* "Promisifies" a stream.
*
* @typeparam Message The type of the messages transmitted by the stream.
* @param stream The stream to promisify.
* @return A promise that resolves to all the transmitted messages when the
* stream completes, and is rejected when the stream errors or is canceled (in this case,
* the error is a [[ClientRpcError]] of type [[ModuleRpcCommon.RpcErrorType.canceled]]).
*
* @example ```TypeScript
* const result = await streamAsPromise(stream);
* // If stream emits 1, 2, 3, result is [1, 2, 3].
* // If the stream is canceled or errored, the promise is rejected.
* ```
*/
function streamAsPromise(stream) {
return new Promise((accept, reject) => {
const messages = [];
stream
.on('message', message => {
messages.push(message);
})
.on('error', err => {
reject(err);
})
.on('canceled', () => {
reject(new errors_1.ClientRpcError(common_1.ModuleRpcCommon.RpcErrorType.canceled));
})
.on('complete', () => {
accept(messages);
})
.start();
});
}
exports.streamAsPromise = streamAsPromise;
//# sourceMappingURL=stream.js.map