UNPKG

4.77 kBTypeScriptView Raw
1import { EventStream } from "./observable";
2import { Source } from "./internal/source";
3import Observable, { ObservableConstructor } from "./observable";
4import { Property } from "./observable";
5export declare type ObservableOrSource<V> = Observable<V> | Source<any, V>;
6/**
7 * Join pattern consisting of a single EventStream and a mapping function.
8 */
9export declare type Pattern1<I1, O> = [ObservableOrSource<I1>, O | ((a: I1) => O)];
10/**
11 * Join pattern consisting of a 2 Observables and a combinator function. At least one of the Observables must be an EventStream.
12 */
13export declare type Pattern2<I1, I2, O> = [ObservableOrSource<I1>, ObservableOrSource<I1>, O | ((a: I1, b: I2) => O)];
14/**
15 * Join pattern consisting of a 3 Observables and a combinator function. At least one of the Observables must be an EventStream.
16 */
17export declare type Pattern3<I1, I2, I3, O> = [ObservableOrSource<I1>, ObservableOrSource<I1>, ObservableOrSource<I3>, O | ((a: I1, b: I2, c: I3) => O)];
18/**
19 * Join pattern consisting of a 4 Observables and a combinator function. At least one of the Observables must be an EventStream.
20 */
21export declare type Pattern4<I1, I2, I3, I4, O> = [ObservableOrSource<I1>, ObservableOrSource<I1>, ObservableOrSource<I3>, ObservableOrSource<I4>, O | ((a: I1, b: I2, c: I3, d: I4) => O)];
22/**
23 * Join pattern consisting of a 5 Observables and a combinator function. At least one of the Observables must be an EventStream.
24 */
25export declare type Pattern5<I1, I2, I3, I4, I5, O> = [ObservableOrSource<I1>, ObservableOrSource<I1>, ObservableOrSource<I3>, ObservableOrSource<I4>, ObservableOrSource<I5>, O | ((a: I1, b: I2, c: I3, d: I4, e: I5) => O)];
26/**
27 * Join pattern consisting of a 6 Observables and a combinator function. At least one of the Observables must be an EventStream.
28 */
29export declare type Pattern6<I1, I2, I3, I4, I5, I6, O> = [ObservableOrSource<I1>, ObservableOrSource<I1>, ObservableOrSource<I3>, ObservableOrSource<I4>, ObservableOrSource<I5>, ObservableOrSource<I6>, O | ((a: I1, b: I2, c: I3, d: I4, e: I5, f: I6) => O)];
30/** @hidden */
31export declare type RawPattern = [AnyObservableOrSource[], AnyFunction];
32/**
33 * Join pattern type, allowing up to 6 sources per pattern.
34 */
35export declare type Pattern<O> = Pattern1<any, O> | Pattern2<any, any, O> | Pattern3<any, any, any, O> | Pattern4<any, any, any, any, O> | Pattern5<any, any, any, any, any, O> | Pattern6<any, any, any, any, any, any, O> | RawPattern;
36/** @hidden */
37export declare type AnySource = Source<any, any>;
38/** @hidden */
39export declare type AnyFunction = Function;
40/** @hidden */
41export declare type AnyObservable = Observable<any>;
42/** @hidden */
43export declare type AnyObservableOrSource = AnyObservable | AnySource;
44/**
45 The `when` method provides a generalization of the [`zip`](classes/observable.html#zip) function. While zip
46 synchronizes events from multiple streams pairwse, the join patterns used in `when` allow
47 the implementation of more advanced synchronization patterns.
48
49 Consider implementing a game with discrete time ticks. We want to
50 handle key-events synchronized on tick-events, with at most one key
51 event handled per tick. If there are no key events, we want to just
52 process a tick.
53
54 ```js
55 Bacon.when(
56 [tick, keyEvent, function(_, k) { handleKeyEvent(k); return handleTick(); }],
57 [tick, handleTick])
58 ```
59
60 Order is important here. If the [tick] patterns had been written
61 first, this would have been tried first, and preferred at each tick.
62
63 Join patterns are indeed a generalization of zip, and for EventStreams, zip is
64 equivalent to a single-rule join pattern. The following observables
65 have the same output, assuming that all sources are EventStreams.
66
67 ```js
68 Bacon.zipWith(a,b,c, combine)
69 Bacon.when([a,b,c], combine)
70 ```
71
72 Note that [`Bacon.when`](#bacon-when) does not trigger updates for events from Properties though;
73 if you use a Property in your pattern, its value will be just sampled when all the
74 other sources (EventStreams) have a value. This is useful when you need a value of a Property
75 in your calculations. If you want your pattern to fire for a Property too, you can
76 convert it into an EventStream using [`property.changes()`](#property-changes) or [`property.toEventStream()`](#property-toeventstream)
77
78 * @param {Pattern<O>} patterns Join patterns
79 * @typeparam O result type
80 */
81export declare function when<O>(...patterns: Pattern<O>[]): EventStream<O>;
82/** @hidden */
83export declare function whenP<O>(...patterns: Pattern<O>[]): Property<O>;
84export default when;
85/** @hidden */
86export declare function when_<O>(ctor: ObservableConstructor, patterns: Pattern<O>[]): Observable<O>;
87/** @hidden */
88export declare function extractRawPatterns<O>(patterns: Pattern<O>[]): RawPattern[];
89
\No newline at end of file