import { ObservableLike, Subscription } from '@dws/muster-observable'; import { NodeDefinition, NodeLike, Params, StatefulGraphNode, StatefulNodeType } from '../../types/graph'; /** * An instance of the [[fromStream]] node. * See the [[fromStream]] documentation to find out more. */ export interface FromStreamNode extends StatefulGraphNode<'fromStream', FromStreamNodeProperties, FromStreamNodeState, FromStreamNodeData> { } /** * A definition of the [[fromStream]] node. * See the [[fromStream]] documentation to find out more. */ export interface FromStreamNodeDefinition extends NodeDefinition<'fromStream', FromStreamNodeProperties, FromStreamNodeState, FromStreamNodeData> { } export declare type ValueStream = ObservableLike; export declare type ValueStreamFactory = ((params: Params) => ValueStream); export interface FromStreamNodeProperties { factory: ValueStream | ValueStreamFactory; } export interface FromStreamNodeState { currentValue: NodeDefinition | undefined; } export interface FromStreamNodeData { subscription: Subscription; } /** * The implementation of the [[fromStream]] node. * See the [[fromStream]] documentation to learn more. */ export declare const FromStreamNodeType: StatefulNodeType<'fromStream', FromStreamNodeProperties, FromStreamNodeState>; /** * Creates an instance of a [[fromStream]] node, which is a type of [[NodeDefinition]] that allows plugging * streams/observables into Muster. This node will emit new values every time the underlying stream emits. * The [[fromStream]] opens the subscription to the source stream only when * subscribed to. It also unsubscribes from the source stream when all subscriptions * to this graph node are closed. * * The [[fromStream]] works with any stream library that conforms to the * [Observable API](https://tc39.github.io/proposal-observable/). * Examples of libraries that should work with the [[fromStream]]: * - [RxJS](http://reactivex.io/rxjs/) * - [most.js](https://github.com/cujojs/most) * * * @example **Simple stream** * ```ts * import { BehaviorSubject } from '@dws/muster-observable'; * import muster, { fromStream, ref, value } from '@dws/muster'; * * const subject = new BehaviorSubject(value('initial')); * const app = muster({ * myStreamedValue: fromStream(subject), * }); * * app.resolve(ref('myStreamedValue')).subscribe((res) => { * console.log(res); * }); * * subject.next(value('updated')); * * // Console output: * // initial * // updated * ``` * This example shows how to plug a Behaviour Subject into Muster. Note that in the subscribe * callback the `res` is a [value](../modules/_nodes_graph_value_.html#value). The [[fromStream]] automatically wraps the value * returned from the stream with a `value` node. * * * @example **Connecting two instances of Muster** * ```ts * import muster, { fromStream, ref, set, variable } from '@dws/muster'; * * const otherApp = muster({ * name: variable('initial'), * }); * * const app = muster({ * remoteName: fromStream(otherApp.resolve(ref('name'))), * }); * * app.resolve(ref('remoteName')).subscribe((res) => { * console.log(res); * }); * * await otherApp.resolve(set('name', 'updated')); * * // Console output: * // initial * // updated * ``` * Because the output value of the `muster.resolve` also conforms to the Observable API, * you can create connections between two instances of Muster. **This is not a recommended * way of making that connection.** It only serves as an example how to handle different kinds of * streams. */ export declare function fromStream(factory: ValueStream | ValueStreamFactory): FromStreamNodeDefinition; export declare function isFromStreamNodeDefinition(value: NodeDefinition): value is FromStreamNodeDefinition;