rimmel
Version:
A Stream-Oriented UI library for the Rx.Observable Universe
42 lines (39 loc) • 1.8 kB
JavaScript
import { Subject } from 'rxjs';
/**
* Create an "input pipe" by prepending operators to the input of an Observer, Subject, BehaviorSubject, or plain subscriber function.
* This works the opposite of RxJS's pipe(), which works on the output of an Observable.
**/
const pipeIn = (target, ...pipeline) => {
const source = new Subject();
source
.pipe(...pipeline)
.subscribe(target);
// FIXME: will we need to unsubscribe? Then store a reference for unsubscription
// TODO: can we/should we delay subscription until mounted? Could miss the first events otherwise
// TODO: check if a Subject is needed, or if we can connect directly to the target (e.g. w/ Observature.addSource)
return source;
};
/**
* Create an "input pipe" by prepending operators to an Observer or a Subject
*
* @remarks This works the opposite of the `pipe()` function in RxJS, which
* transforms the output of an observable whilst this transforms the input.
*
* You normally use an input pipe to create Event Adapters.
*
* @template I the input type of the returned stream (the event adapter)
* @template O the output type of the returned stream (= the input type of the actual target stream)
* @example const MyUsefulEventAdapter = inputPipe(...pipeline);
* const template = rml`
* <input onkeypress="${MyUsefulEventAdapter(targetObserver)}">
* `;
**/
const inputPipe = (...pipeline) => (target) => pipeIn(target, ...pipeline);
const feed = pipeIn;
const feedIn = pipeIn;
const reversePipe = inputPipe;
// TBC
const source = (...reversePipeline) => pipeIn(reversePipeline.pop(), ...reversePipeline);
const sink = (source, ...pipeline) => source.pipe(...pipeline);
export { feed, feedIn, inputPipe, pipeIn, reversePipe, sink, source };
//# sourceMappingURL=input-pipe.js.map