From 9d139b8fa6c2bbf0bb151e5a1521ebc50418a367 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Luiz=20dos=20Santos?= Date: Thu, 28 Apr 2016 10:28:35 -0300 Subject: [PATCH] Add type definitions for RxJS bindings for Node --- rx-node/rx.node-tests.ts | 52 +++++++++++++++++++++++++++ rx-node/rx.node.d.ts | 77 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 rx-node/rx.node-tests.ts create mode 100644 rx-node/rx.node.d.ts diff --git a/rx-node/rx.node-tests.ts b/rx-node/rx.node-tests.ts new file mode 100644 index 0000000000..254ef6e242 --- /dev/null +++ b/rx-node/rx.node-tests.ts @@ -0,0 +1,52 @@ +// Type definitions for RxJS bindings for Node +// Project: https://github.com/Reactive-Extensions/rx-node +// Definitions by: Andre Luiz dos Santos +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +/// + +import RxNode = require('rx-node'); + +{ + var source = Rx.Observable.return(42); + var emitter = RxNode.toEventEmitter(source, 'data'); + + emitter.on('data', function(data: number) { + console.log('Data: ' + data); + }); + + emitter.on('end', function() { + console.log('End'); + }); + + // Ensure to call publish to fire events from the observable + emitter.publish(); +} +{ + var subscription = RxNode.fromStream(process.stdin, 'end') + .subscribe(function(x) { console.log(x); }); +} +{ + var subscription = RxNode.fromReadableStream(process.stdin) + .subscribe(function(x) { console.log(x); }); +} +{ + var readline = require('readline'); + var fs = require('fs'); + + var rl = readline.createInterface({ + input: fs.createReadStream('sample.txt') + }); + + var subscription = RxNode.fromReadLineStream(rl) + .subscribe(function(x) { console.log(x); }); +} +{ + var subscription = RxNode.fromWritableStream(process.stdout) + .subscribe(function(x) { console.log(x); }); +} +{ + var source = Rx.Observable.range(0, 5); + var subscription = RxNode.writeToStream(source, process.stdout, 'utf8'); +} diff --git a/rx-node/rx.node.d.ts b/rx-node/rx.node.d.ts new file mode 100644 index 0000000000..14af714359 --- /dev/null +++ b/rx-node/rx.node.d.ts @@ -0,0 +1,77 @@ +// Type definitions for RxJS bindings for Node +// Project: https://github.com/Reactive-Extensions/rx-node +// Definitions by: Andre Luiz dos Santos +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// +/// + +declare namespace RxNode { + + export interface PublishableEventEmitter extends NodeJS.EventEmitter { + publish(): void; + } + + /** + * Converts the given observable sequence to an event emitter with the given event name. + * The errors are handled on the 'error' event and completion on the 'end' event. + * You must call publish in order to invoke the subscription on the Observable sequence. + * @param {Observable} observable The observable sequence to convert to an EventEmitter. + * @param {String} eventName The event name to emit onNext calls. + * @returns {EventEmitter} An EventEmitter which emits the given eventName for each onNext call in addition to 'error' and 'end' events. + */ + function toEventEmitter(observable: Rx.Observable, eventName: string): RxNode.PublishableEventEmitter; + + /** + * Converts a flowing stream to an Observable sequence. + * @param {Stream} stream A stream to convert to a observable sequence. + * @param {String} [finishEventName] Event that notifies about closed stream. ("end" by default) + * @param {String} [dataEventName] Event that notifies about incoming data. ("data" by default) + * @returns {Observable} An observable sequence which fires on each 'data' event as well as handling 'error' and finish events like `end` or `finish`. + */ + function fromStream(stream: NodeJS.ReadableStream, finishEventName?: string, dataEventName?: string): Rx.Observable; + + /** + * Converts a flowing readable stream to an Observable sequence. + * @param {Stream} stream A stream to convert to a observable sequence. + * @param {String} [dataEventName] Event that notifies about incoming data. ("data" by default) + * @returns {Observable} An observable sequence which fires on each 'data' event as well as handling 'error' and 'end' events. + */ + function fromReadableStream(stream: NodeJS.ReadableStream, dataEventName?: string): Rx.Observable; + + /** + * Converts a flowing readline stream to an Observable sequence. + * @param {Stream} stream A stream to convert to a observable sequence. + * @returns {Observable} An observable sequence which fires on each 'data' event as well as handling 'error' and 'end' events. + */ + function fromReadLineStream(stream: NodeJS.ReadableStream): Rx.Observable; + + /** + * Converts a flowing writeable stream to an Observable sequence. + * @param {Stream} stream A stream to convert to a observable sequence. + * @returns {Observable} An observable sequence which fires on each 'data' event as well as handling 'error' and 'finish' events. + */ + function fromWritableStream(stream: NodeJS.WritableStream): Rx.Observable; + + /** + * Converts a flowing transform stream to an Observable sequence. + * @param {Stream} stream A stream to convert to a observable sequence. + * @param {String} [dataEventName] Event that notifies about incoming data. ("data" by default) + * @returns {Observable} An observable sequence which fires on each 'data' event as well as handling 'error' and 'finish' events. + */ + function fromTransformStream(stream: NodeJS.ReadWriteStream, dataEventName?: string): Rx.Observable; + + /** + * Writes an observable sequence to a stream + * @param {Observable} observable Observable sequence to write to a stream. + * @param {Stream} stream The stream to write to. + * @param {String} [encoding] The encoding of the item to write. + * @returns {Disposable} The subscription handle. + */ + function writeToStream(observable: Rx.Observable, stream: NodeJS.WritableStream, encoding: string): Rx.Disposable; + +} + +declare module "rx-node" { + export = RxNode; +}