import { EventStream } from "./observable"; import { Source } from "./internal/source"; import Observable, { ObservableConstructor } from "./observable"; import { Property } from "./observable"; export declare type ObservableOrSource = Observable | Source; /** * Join pattern consisting of a single EventStream and a mapping function. */ export declare type Pattern1 = [ObservableOrSource, O | ((a: I1) => O)]; /** * Join pattern consisting of a 2 Observables and a combinator function. At least one of the Observables must be an EventStream. */ export declare type Pattern2 = [ObservableOrSource, ObservableOrSource, O | ((a: I1, b: I2) => O)]; /** * Join pattern consisting of a 3 Observables and a combinator function. At least one of the Observables must be an EventStream. */ export declare type Pattern3 = [ObservableOrSource, ObservableOrSource, ObservableOrSource, O | ((a: I1, b: I2, c: I3) => O)]; /** * Join pattern consisting of a 4 Observables and a combinator function. At least one of the Observables must be an EventStream. */ export declare type Pattern4 = [ObservableOrSource, ObservableOrSource, ObservableOrSource, ObservableOrSource, O | ((a: I1, b: I2, c: I3, d: I4) => O)]; /** * Join pattern consisting of a 5 Observables and a combinator function. At least one of the Observables must be an EventStream. */ export declare type Pattern5 = [ObservableOrSource, ObservableOrSource, ObservableOrSource, ObservableOrSource, ObservableOrSource, O | ((a: I1, b: I2, c: I3, d: I4, e: I5) => O)]; /** * Join pattern consisting of a 6 Observables and a combinator function. At least one of the Observables must be an EventStream. */ export declare type Pattern6 = [ObservableOrSource, ObservableOrSource, ObservableOrSource, ObservableOrSource, ObservableOrSource, ObservableOrSource, O | ((a: I1, b: I2, c: I3, d: I4, e: I5, f: I6) => O)]; /** @hidden */ export declare type RawPattern = [AnyObservableOrSource[], AnyFunction]; /** * Join pattern type, allowing up to 6 sources per pattern. */ export declare type Pattern = Pattern1 | Pattern2 | Pattern3 | Pattern4 | Pattern5 | Pattern6 | RawPattern; /** @hidden */ export declare type AnySource = Source; /** @hidden */ export declare type AnyFunction = Function; /** @hidden */ export declare type AnyObservable = Observable; /** @hidden */ export declare type AnyObservableOrSource = AnyObservable | AnySource; /** The `when` method provides a generalization of the [`zip`](classes/observable.html#zip) function. While zip synchronizes events from multiple streams pairwse, the join patterns used in `when` allow the implementation of more advanced synchronization patterns. Consider implementing a game with discrete time ticks. We want to handle key-events synchronized on tick-events, with at most one key event handled per tick. If there are no key events, we want to just process a tick. ```js Bacon.when( [tick, keyEvent, function(_, k) { handleKeyEvent(k); return handleTick(); }], [tick, handleTick]) ``` Order is important here. If the [tick] patterns had been written first, this would have been tried first, and preferred at each tick. Join patterns are indeed a generalization of zip, and for EventStreams, zip is equivalent to a single-rule join pattern. The following observables have the same output, assuming that all sources are EventStreams. ```js Bacon.zipWith(a,b,c, combine) Bacon.when([a,b,c], combine) ``` Note that [`Bacon.when`](#bacon-when) does not trigger updates for events from Properties though; if you use a Property in your pattern, its value will be just sampled when all the other sources (EventStreams) have a value. This is useful when you need a value of a Property in your calculations. If you want your pattern to fire for a Property too, you can convert it into an EventStream using [`property.changes()`](#property-changes) or [`property.toEventStream()`](#property-toeventstream) * @param {Pattern} patterns Join patterns * @typeparam O result type */ export declare function when(...patterns: Pattern[]): EventStream; /** @hidden */ export declare function whenP(...patterns: Pattern[]): Property; export default when; /** @hidden */ export declare function when_(ctor: ObservableConstructor, patterns: Pattern[]): Observable; /** @hidden */ export declare function extractRawPatterns(patterns: Pattern[]): RawPattern[];