UNPKG

2.99 kBTypeScriptView Raw
1import { Event } from './event';
2import { EventStream } from "./observable";
3import { Unsub } from "./types";
4import { Reply } from "./reply";
5export declare type FlexibleSink<V> = (event: EventLike<V>) => Reply;
6export declare type EventLike<V> = V | Event<V> | Event<V>[];
7/**
8Binder function used in [fromBinder](../globals.html#frombinder)
9@typeparam V Type of stream elements
10 */
11export declare type Binder<V> = (sink: FlexibleSink<V>) => Unsub;
12export declare type EventTransformer<V> = (...args: any[]) => EventLike<V>;
13/**
14 If none of the other factory methods above apply, you may of course roll your own EventStream by using `fromBinder`.
15
16 <a name="bacon-frombinder"></a>
17 [`Bacon.fromBinder(subscribe)`](#bacon-frombinder "Bacon.fromBinder(subscribe)") The parameter `subscribe` is a function that accepts a `sink` which is a function that your `subscribe` function can "push" events to.
18
19 For example:
20
21 ```js
22 var stream = Bacon.fromBinder(function(sink) {
23 sink("first value")
24 sink([new Bacon.Next("2nd"), new Bacon.Next("3rd")])
25 sink(new Bacon.Error("oops, an error"))
26 sink(new Bacon.End())
27 return function() {
28 // unsub functionality here, this one's a no-op
29 }
30})
31 stream.log()
32 ```
33
34 As shown in the example, you can push
35
36 - A plain value, like `"first value"`
37 - An [`Event`](#event) object including [`Bacon.Error`](#bacon-error) (wraps an error) and [`Bacon.End`](#bacon-end) (indicates
38 stream end).
39 - An array of [event](#event) objects at once
40
41 Other examples can be found on [JSFiddle](http://jsfiddle.net/PG4c4/) and the
42 [Bacon.js blog](http://baconjs.blogspot.fi/2013/12/wrapping-things-in-bacon.html).
43
44 The `subscribe` function must return a function. Let's call that function
45 `unsubscribe`. The returned function can be used by the subscriber (directly or indirectly) to
46 unsubscribe from the EventStream. It should release all resources that the subscribe function reserved.
47
48 The `sink` function may return [`Bacon.noMore`](#bacon-nomore) (as well as [`Bacon.more`](#bacon-more)
49 or any other value). If it returns [`Bacon.noMore`](#bacon-nomore), no further events will be consumed
50 by the subscriber. The `subscribe` function may choose to clean up all resources at this point (e.g.,
51 by calling `unsubscribe`). This is usually not necessary, because further calls to `sink` are ignored,
52 but doing so can increase performance in [rare cases](https://github.com/baconjs/bacon.js/issues/484).
53
54 The EventStream will wrap your `subscribe` function so that it will
55 only be called when the first stream listener is added, and the `unsubscribe`
56 function is called only after the last listener has been removed.
57 The subscribe-unsubscribe cycle may of course be repeated indefinitely,
58 so prepare for multiple calls to the subscribe function.
59
60
61 @param binder
62 @param eventTransformer
63 @typeparam V Type of stream elements
64
65 */
66export default function fromBinder<V>(binder: Binder<V>, eventTransformer?: EventTransformer<V>): EventStream<V>;