UNPKG

3.69 kBTypeScriptView Raw
1import "./scan";
2import Observable, { Property } from "./observable";
3/**
4 * [Update](#update) pattern consisting of a single EventStream and a accumulator function.
5 */
6export declare type UpdatePattern1<I1, O> = [Observable<I1>, O | ((acc: O, a: I1) => O)];
7/**
8 * [Update](#update) pattern consisting of 2 Observables and an accumulrator function. At least one of the Observables must be an EventStream.
9 */
10export declare type UpdatePattern2<I1, I2, O> = [Observable<I1>, Observable<I1>, O | ((acc: O, a: I1, b: I2) => O)];
11/**
12 * [Update](#update) pattern consisting of 3 Observables and an accumulrator function. At least one of the Observables must be an EventStream.
13 */
14export declare type UpdatePattern3<I1, I2, I3, O> = [Observable<I1>, Observable<I1>, Observable<I3>, O | ((acc: O, a: I1, b: I2, c: I3) => O)];
15/**
16 * [Update](#update) pattern consisting of 4 Observables and an accumulrator function. At least one of the Observables must be an EventStream.
17 */
18export declare type UpdatePattern4<I1, I2, I3, I4, O> = [Observable<I1>, Observable<I1>, Observable<I3>, Observable<I4>, O | ((acc: O, a: I1, b: I2, c: I3, d: I4) => O)];
19/**
20 * [Update](#update) pattern consisting of 5 Observables and an accumulrator function. At least one of the Observables must be an EventStream.
21 */
22export declare type UpdatePattern5<I1, I2, I3, I4, I5, O> = [Observable<I1>, Observable<I1>, Observable<I3>, Observable<I4>, Observable<I5>, O | ((acc: O, a: I1, b: I2, c: I3, d: I4, e: I5) => O)];
23/**
24 * [Update](#update) pattern consisting of 6 Observables and an accumulrator function. At least one of the Observables must be an EventStream.
25 */
26export declare type UpdatePattern6<I1, I2, I3, I4, I5, I6, O> = [Observable<I1>, Observable<I1>, Observable<I3>, Observable<I4>, Observable<I5>, Observable<I6>, O | ((acc: O, a: I1, b: I2, c: I3, d: I4, e: I5, f: I6) => O)];
27/**
28 * [Update](#update) pattern type, allowing up to 6 sources per pattern.
29 */
30export declare type UpdatePattern<O> = UpdatePattern1<any, O> | UpdatePattern2<any, any, O> | UpdatePattern3<any, any, any, O> | UpdatePattern4<any, any, any, any, O> | UpdatePattern5<any, any, any, any, any, O> | UpdatePattern6<any, any, any, any, any, any, O>;
31/**
32 Creates a Property from an initial value and updates the value based on multiple inputs.
33 The inputs are defined similarly to [`Bacon.when`](#bacon-when), like this:
34
35 ```js
36 var result = Bacon.update(
37 initial,
38 [x,y,z, (previous,x,y,z) => { ... }],
39 [x,y, (previous,x,y) => { ... }])
40 ```
41
42 As input, each function above will get the previous value of the `result` Property, along with values from the listed Observables.
43 The value returned by the function will be used as the next value of `result`.
44
45 Just like in [`Bacon.when`](#when), only EventStreams will trigger an update, while Properties will be just sampled.
46 So, if you list a single EventStream and several Properties, the value will be updated only when an event occurs in the EventStream.
47
48 Here's a simple gaming example:
49
50 ```js
51 let scoreMultiplier = Bacon.constant(1)
52 let hitUfo = Bacon.interval(1000)
53 let hitMotherShip = Bacon.later(10000)
54 let score = Bacon.update(
55 0,
56 [hitUfo, scoreMultiplier, (score, _, multiplier) => score + 100 * multiplier ],
57 [hitMotherShip, (score, _) => score + 2000 ]
58 )
59 ```
60
61 In the example, the `score` property is updated when either `hitUfo` or `hitMotherShip` occur. The `scoreMultiplier` Property is sampled to take multiplier into account when `hitUfo` occurs.
62
63 * @param initial
64 * @param {UpdatePattern<Out>} patterns
65 * @returns {Property<Out>}
66 */
67export declare function update<Out>(initial: Out, ...patterns: UpdatePattern<Out>[]): Property<Out>;
68
\No newline at end of file