declarations
Version:
[](https://www.npmjs.com/package/declarations)
987 lines (912 loc) • 159 kB
TypeScript
// Type definitions for Bacon.js 0.7.0
// Project: https://baconjs.github.io/
// Definitions by: Alexander Matsievsky <https://github.com/alexander-matsievsky>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="../jquery/jquery.d.ts" />
/// <reference path="../node/node.d.ts" />
interface JQuery {
/**
* @method
* @description Creates an [EventStream]{@link Bacon.EventStream} from events on a jQuery or Zepto.js object.
* @param {string} eventName
* @returns {EventStream<ErrorEvent, JQueryEventObject>}
* @example
* $("#my-div").asEventStream("click");
*/
asEventStream(eventName:string):Bacon.EventStream<ErrorEvent, JQueryEventObject>;
/**
* @method
* @description Creates an [EventStream]{@link Bacon.EventStream} from events on a jQuery or Zepto.js object. You can pass an argument to add a jQuery live `selector`.
* @param {string} eventName
* @param {string} selector
* @returns {EventStream<ErrorEvent, JQueryEventObject>}
* @example
* $("#my-div").asEventStream("click", ".more-specific-selector");
*/
asEventStream(eventName:string, selector:string):Bacon.EventStream<ErrorEvent, JQueryEventObject>;
/**
* @callback JQuery#asEventStream1~f
* @param {JQueryEventObject} event
* @param {*[]} args
* @returns {A}
*/
/**
* @method JQuery#asEventStream1
* @description Creates an [EventStream]{@link Bacon.EventStream} from events on a jQuery or Zepto.js object. You can pass an argument to add a function `f` that processes the jQuery event and its parameters.
* @param {string} eventName
* @param {JQuery#asEventStream1~f} f
* @returns {EventStream<ErrorEvent, A>}
* @example
* $("#my-div").asEventStream("click", (event, args) => args[0]);
*/
asEventStream<A>(eventName:string, f:(event:JQueryEventObject, args:any[]) => A):Bacon.EventStream<ErrorEvent, A>;
/**
* @callback JQuery#asEventStream2~f
* @param {JQueryEventObject} event
* @param {*[]} args
* @returns {A}
*/
/**
* @method JQuery#asEventStream2
* @description Creates an [EventStream]{@link Bacon.EventStream} from events on a jQuery or Zepto.js object. You can pass an argument to add a jQuery live `selector` and a function `f` that processes the jQuery event and its parameters.
* @param {string} eventName
* @param {string} selector
* @param {JQuery#asEventStream2~f} f
* @returns {Bacon.EventStream<ErrorEvent, A>}
* @example
* $("#my-div").asEventStream("click", ".more-specific-selector", (event, args) => args[0]);
*/
asEventStream<A>(eventName:string, selector:string, f:(event:JQueryEventObject, args:any[]) => A):Bacon.EventStream<ErrorEvent, A>;
}
/** @module Bacon */
declare namespace Bacon {
/**
* @function
* @description Creates an [EventStream]{@link Bacon.EventStream} from a `promise` Promise object such as JQuery Ajax. This stream will contain a single value or an error, followed immediately by stream end. You can use the optional `abort` flag (i.e. ´Bacon.fromPromise(p, true)´ to have the `abort` method of the given promise be called when all subscribers have been removed from the created stream.
* @param {Promise<A>|JQueryXHR} promise
* @param {boolean} [abort]
* @returns {EventStream<E, A>}
* @example
* Bacon.fromPromise($.ajax("https://baconjs.github.io/"));
* Bacon.fromPromise(Promise.resolve(1));
* Bacon.fromPromise($.ajax("https://baconjs.github.io/"), true);
* Bacon.fromPromise(Promise.resolve(1), false);
*/
function fromPromise<E, A>(promise:Promise<A>|JQueryXHR, abort?:boolean):EventStream<E, A>;
/**
* @callback Bacon.fromPromise~eventTransformer
* @param {A} value
* @returns {(Initial<B>|Next<B>|End<B>|Error<E>)[]}
*/
/**
* @function Bacon.fromPromise
* @description Creates an [EventStream]{@link Bacon.EventStream} from a `promise` Promise object such as JQuery Ajax. This stream will contain a single value or an error, followed immediately by stream end. You can use the `abort` flag (i.e. ´Bacon.fromPromise(p, true)´ to have the `abort` method of the given promise be called when all subscribers have been removed from the created stream, and also pass a function `eventTransformer` that transforms the promise value into Events. The default is to transform the value into `[new Bacon.Next(value), new Bacon.End()]`.
* @param {Promise<A>|JQueryXHR} promise
* @param {boolean} abort
* @param {Bacon.fromPromise~eventTransformer} eventTransformer
* @returns {EventStream<E, B>}
* @example
* Bacon.fromPromise($.ajax("https://baconjs.github.io/"), true, (n:string) => {
* return [new Bacon.Next(n), new Bacon.Next(() => n), new Bacon.End()];
* });
* Bacon.fromPromise(Promise.resolve(1), false, n => {
* return [new Bacon.Next(n), new Bacon.Next(() => n), new Bacon.End()];
* });
*/
function fromPromise<E, A, B>(promise:Promise<A>|JQueryXHR, abort:boolean, eventTransformer:(value:A) => (Initial<B>|Next<B>|End<B>|Error<E>)[]):EventStream<E, B>;
/**
* @function
* @description Creates an [EventStream]{@link Bacon.EventStream} from events on a DOM EventTarget or Node.JS EventEmitter object, or an object that supports event listeners using `on`/`off` methods.
* @param {EventTarget|NodeJS.EventEmitter|JQuery} target
* @param {string} eventName
* @returns {EventStream<E, A>}
* @example
* Bacon.fromEvent(document.body, "click").onValue(() => {
* alert("Bacon!");
* });
* Bacon.fromEvent(process.stdin, "readable", () => {
* alert("Bacon!");
* });
* Bacon.fromEvent($("body"), "click").onValue(() => {
* alert("Bacon!");
* });
*/
function fromEvent<E, A>(target:EventTarget|NodeJS.EventEmitter|JQuery, eventName:string):EventStream<E, A>;
/**
* @callback Bacon.fromEvent~eventTransformer
* @param {A} event
* @returns {B}
*/
/**
* @function Bacon.fromEvent
* @description Creates an [EventStream]{@link Bacon.EventStream} from events on a DOM EventTarget or Node.JS EventEmitter object, or an object that supports event listeners using `on`/`off` methods. You can pass a function `eventTransformer` that transforms the emitted events' parameters.
* @param {EventTarget|NodeJS.EventEmitter|JQuery} target
* @param {string} eventName
* @param {Bacon.fromEvent~eventTransformer} eventTransformer
* @returns {EventStream<E, B>}
* @example
* Bacon.fromEvent(document.body, "click", (event:MouseEvent) => event.clientX).onValue(clientX => {
* alert("Bacon!");
* });
*/
function fromEvent<E, A, B>(target:EventTarget|NodeJS.EventEmitter|JQuery, eventName:string, eventTransformer:(event:A) => B):EventStream<E, B>;
/**
* @callback Bacon.fromCallback1~f
* @param {Bacon.fromCallback1~callback} callback
* @returns {void}
*/
/**
* @callback Bacon.fromCallback1~callback
* @param {...*} args
* @returns {void}
*/
/**
* @function Bacon.fromCallback1
* @description Creates an [EventStream]{@link Bacon.EventStream} from a function `f` that accepts a `callback`. The function is supposed to call its callback just once.
* @param {Bacon.fromCallback1~f} f
* @returns {EventStream<E, A>}
* @example
* // This would create a stream that outputs a single value "Bacon!" and ends after that. The use of setTimeout causes the value to be delayed by 1 second.
* Bacon.fromCallback(callback => {
* setTimeout(() => {
* callback("Bacon!");
* }, 1000);
* });
*/
function fromCallback<E, A>(f:(callback:(...args:any[]) => void) => void):EventStream<E, A>;
/**
* @callback Bacon.fromCallback2~f
* @param {...*} args
* @returns {void}
*/
/**
* @function Bacon.fromCallback2
* @description Creates an [EventStream]{@link Bacon.EventStream} from a function `f` that accepts a `callback`. The function is supposed to call its callback just once.
* @param {Bacon.fromCallback2~f} f
* @param {...*} args
* @returns {EventStream<E, A>}
* @example
* // You can also give any number of arguments to `fromCallback`, which will be passed to the function. These arguments can be simple variables, Bacon EventStreams or Properties. For example the following will output "Bacon rules":
* Bacon.fromCallback((a, b, callback) => {
* callback(a + " " + b);
* }, Bacon.constant("bacon"), "rules").log();
*/
function fromCallback<E, A>(f:(...args:any[]) => void, ...args:any[]):EventStream<E, A>;
/**
* @function
* @description Creates an [EventStream]{@link Bacon.EventStream} from a `methodName` method of a given `object`. The function is supposed to call its callback just once.
* @param {Object} object
* @param {string} methodName
* @param {...*} args
* @returns {EventStream<E, A>}
*/
function fromCallback<E, A>(object:Object, methodName:string, ...args:any[]):EventStream<E, A>;
/**
* @callback Bacon.fromNodeCallback~f
* @param {Bacon.fromNodeCallback~callback} callback
* @returns {void}
*/
/**
* @callback Bacon.fromNodeCallback~callback
* @param {E} error
* @param {A} data
* @returns {void}
*/
/**
* @function Bacon.fromNodeCallback
* @description Creates an [EventStream]{@link Bacon.EventStream} from a function `f` that accepts a Node.js `callback`: callback(error, data), where error is `null` if everything is fine. The function is supposed to call its callback just once.
* @param {Bacon.fromNodeCallback~f} f
* @param {...*} args
* @returns {EventStream<E, A>}
* @example
* {
* let fs = require("fs"),
* read = Bacon.fromNodeCallback(fs.readFile, "input.txt");
* read.onError(error => {
* console.log("Reading failed: " + error);
* });
* read.onValue(value => {
* console.log("Read contents: " + value);
* });
* }
*/
function fromNodeCallback<E, A>(f:(callback:(error:E, data:A) => void) => void, ...args:any[]):EventStream<E, A>;
/**
* @function
* @description Creates an [EventStream]{@link Bacon.EventStream} from a `methodName` method of a given `object`.
* @param {Object} object
* @param {string} methodName
* @param {...*} args
* @returns {EventStream<E, A>}
*/
function fromNodeCallback<E, A>(object:Object, methodName:string, ...args:any[]):EventStream<E, A>;
/**
* @callback Bacon.fromPoll~f
* @returns {Next<A>|End<A>}
*/
/**
* @function Bacon.fromPoll
* @description Polls given function `f` with given `interval`. Function should return events: either [Next]{@link Bacon.Next} or [End]{@link Bacon.End}. Polling occurs only when there are subscribers to the stream. Polling ends permanently when `f` returns [End]{@link Bacon.End}.
* @param {number} interval
* @param {Bacon.fromPoll~f} f
* @returns {EventStream<E, A>}
*/
function fromPoll<E, A>(interval:number, f:() => Next<A>|End<A>):EventStream<E, A>;
/**
* @function Bacon.once
* @description Creates an [EventStream]{@link Bacon.EventStream} that delivers the given single `value` for the first subscriber. The stream will end immediately after this value. You can also send an [Error]{@link Bacon.Error} event instead of a `value`.
* @param {A|Error<E>} value
* @returns {EventStream<E, A>}
* @example
* Bacon.once(new Bacon.Error("fail"));
*/
function once<E, A>(value:A|Error<E>):EventStream<E, A>;
/**
* @function
* @description Creates an [EventStream]{@link Bacon.EventStream} that delivers the given series of `values` (given as array) to the first subscriber. The stream ends after these values have been delivered. You can also send [Error]{@link Bacon.Error} events, or any combination of pure values and error events.
* @param {(A|Error<E>)[]} values
* @returns {EventStream<E, A>}
* @example
* Bacon.fromArray([1, new Bacon.Error("")]);
*/
function fromArray<E, A>(values:(A|Error<E>)[]):EventStream<E, A>;
/**
* @function
* @description Repeats the single `value` indefinitely with the given `interval` (in milliseconds).
* @param {number} interval
* @param {A} value
* @returns {EventStream<E, A>}
*/
function interval<E, A>(interval:number, value:A):EventStream<E, A>;
/**
* @function
* @description Creates a [EventStream]{@link Bacon.EventStream} containing given `values` (given as array) with the given `interval` (in milliseconds).
* @param {number} interval
* @param {A[]} values
* @returns {EventStream<E, A>}
*/
function sequentially<E, A>(interval:number, values:A[]):EventStream<E, A>;
/**
* @function
* @description Repeats given `values` indefinitely with then given `interval` (in milliseconds).
* @param {number} interval
* @param {A[]} values
* @returns {EventStream<E, A>}
* @example
* // The following would lead to `1,2,3,1,2,3...` to be repeated indefinitely:
* Bacon.fromArray([1, new Bacon.Error("")]);
*/
function repeatedly<E, A>(interval:number, values:A[]):EventStream<E, A>;
/**
* @callback Bacon.repeat~f
* @param {number} iteration
* @returns {boolean|Observable<E, A>}
*/
/**
* @function Bacon.repeat
* @description Calls generator function `f` which is expected to return an [Observable]{@link Bacon.Observable}. The returned [EventStream]{@link Bacon.EventStream} contains values and errors from the spawned observable. When the spawned Observable ends, the generator `f` is called again to spawn a new Observable. This is repeated until the generator `f` returns a falsy value (such as `undefined` or `false`). The generator `f` is called with one argument — `iteration` number starting from `0`.
* @param {Bacon.repeat~f} f
* @returns {EventStream<E, A>}
* @example
* // The following will produce values `0,1,2`.
* Bacon.repeat(i => {
* if (i < 3) {
* return Bacon.once(i);
* } else {
* return false;
* }
* }).log();
*/
function repeat<E, A>(f:(iteration:number) => boolean|Observable<E, A>):EventStream<E, A>;
/**
* @function Bacon.never
* @description Creates an [EventStream]{@link Bacon.EventStream} that immediately ends.
* @returns {EventStream<E, A>}
*/
function never<E, A>():EventStream<E, A>;
/**
* @function
* @description Creates a single-element [EventStream]{@link Bacon.EventStream} that produces given `value` after a given `delay` (in milliseconds).
* @param {number} delay
* @param {A} value
* @returns {EventStream<E, A>}
*/
function later<E, A>(delay:number, value:A):EventStream<E, A>;
/**
* @function
* @description Creates a constant [Property]{@link Bacon.Property} with value `x`.
* @param {A} x
* @returns {Property<E, A>}
*/
function constant<E, A>(x:A):Property<E, A>;
/**
* @callback Bacon.fromBinder~subscribe
* @param {Bacon.fromBinder~sink} sink
* @returns {Bacon.fromBinder~unsubscribe}
*/
/**
* @callback Bacon.fromBinder~sink
* @param {More|NoMore|(A|Initial<A>|Next<A>|End<A>|Error<E>)|(A|Initial<A>|Next<A>|End<A>|Error<E>)[]} value
* @returns {void}
*/
/**
* @callback Bacon.fromBinder~unsubscribe
* @returns {void}
*/
/**
* @function Bacon.fromBinder
* @description Creates an [EventStream]{@link Bacon.EventStream} with the given [subscribe]{@link Bacon.fromBinder~subscribe} function. The parameter `subscribe` is a function that accepts a [sink]{@link Bacon.fromBinder~sink} which is a function that your `subscribe` function can "push" events to. You can push: a plain value, like `"first value"`; an [Event]{@link Bacon.Event} object including [Error]{@link Bacon.Error} (wraps an error) and [End]{@link Bacon.End} (indicates stream end); an array of event objects at once. The `subscribe` function must return a function. Let's call that function [unsubscribe]{@link Bacon.fromBinder~unsubscribe}. The returned function can be used by the subscriber (directly or indirectly) to unsubscribe from the EventStream. It should release all resources that the `subscribe` function reserved. The `sink` function may return [noMore]{@link Bacon.noMore} (as well as [more]{@link Bacon.more} or any other value). If it returns `noMore`, no further events will be consumed by the subscriber. The `subscribe` function may choose to clean up all resources at this point (e.g., by calling `unsubscribe`). This is usually not necessary, because further calls to `sink` are ignored, but doing so can increase performance in rare cases. The EventStream will wrap your `subscribe` function so that it will only be called when the first stream listener is added, and the `unsubscribe` function is called only after the last listener has been removed. The subscribe-unsubscribe cycle may of course be repeated indefinitely, so prepare for multiple calls to the `subscribe` function.
* @param {Bacon.fromBinder~subscribe} subscribe
* @returns {EventStream<E, A>}
* @example
* let stream = Bacon.fromBinder(sink => {
* sink("first value");
* sink([new Bacon.Next("2nd"), new Bacon.Next("3rd")]);
* sink(new Bacon.Next(() => {
* return "This one will be evaluated lazily"
* }));
* sink(new Bacon.Error("oops, an error"));
* sink(new Bacon.End());
* return () => {
* // unsub functionality here, this one's a no-op
* };
* });
* stream.log();
*/
function fromBinder<E, A>(subscribe:(sink:(value:More|NoMore|(A|Initial<A>|Next<A>|End<A>|Error<E>)|(A|Initial<A>|Next<A>|End<A>|Error<E>)[]) => void) => (() => void)):EventStream<E, A>;
/**
* @interface
* @see Bacon.more
*/
interface More {
}
/**
* @property more
* @constant
* @description The opaque value `sink` function may return. See [Bacon.fromBinder]{@link Bacon.fromBinder}.
*/
var more:More;
/**
* @interface
* @see Bacon.noMore
*/
interface NoMore {
}
/**
* @property noMore
* @constant
* @description The opaque value `sink` function may return. See [Bacon.fromBinder]{@link Bacon.fromBinder}.
*/
var noMore:NoMore;
/**
* @class Observable
* @description A superclass for [EventStream]{@link Bacon.EventStream} and [Property]{@link Bacon.Property}.
* */
interface Observable<E, A> {
/**
* @callback Observable#onValue~f
* @param {A} value
* @returns {void}
*/
/**
* @callback Observable#onValue~unsubscribe
* @returns {void}
*/
/**
* @method Observable#onValue
* @description Subscribes a given handler function `f` to the [Observable]{@link Bacon.Observable}. Function will be called for each new value. This is the simplest way to assign a side-effect to an Observable. The difference to the [EventStream.subscribe]{@link Bacon.EventStream#subscribe} and [Property.subscribe]{@link Bacon.Property#subscribe} methods is that the actual stream `value`s are received, instead of [Event]{@link Bacon.Event} objects. [EventStream.onValue]{@link Bacon.EventStream#onValue} and [Property.onValue]{@link Bacon.Property#onValue} behave similarly, except that the latter also pushes the initial value of the Property, in case there is one.
* @param {Observable#onValue~f} f
* @returns {Observable#onValue~unsubscribe}
*/
onValue(f:(value:A) => void):() => void;
/**
* @callback Observable#onError~f
* @param {E} error
* @returns {void}
*/
/**
* @callback Observable#onError~unsubscribe
* @returns {void}
*/
/**
* @method Observable#onError
* @description Subscribes a given handler function `f` to [Error]{@link Bacon.Error} events. The function `f` will be called for each error in the [Observable]{@link Bacon.Observable}.
* @param {Observable#onError~f} f
* @returns {Observable#onError~unsubscribe}
*/
onError(f:(error:E) => void):() => void;
/**
* @callback Observable#onEnd~f
* @returns {void}
*/
/**
* @callback Observable#onEnd~unsubscribe
* @returns {void}
*/
/**
* @method Observable#onEnd
* @description Subscribes a given handler function `f` to [End]{@link Bacon.End} event. The function `f` will be called when the [Observable]{@link Bacon.Observable} ends. Just like [EventStream.subscribe]{@link Bacon.EventStream#subscribe} and [Property.subscribe]{@link Bacon.Property#subscribe}, this method returns a function for `unsubscribe`ing.
* @param {Observable#onEnd~f} f
* @returns {Observable#onEnd~unsubscribe}
*/
onEnd(f:() => void):() => void;
/**
* @callback Observable#toPromise~promiseCtr
* @param {A} value
* @returns {Promise<A>}
*/
/**
* @method Observable#toPromise
* @description Returns a Promise which will be resolved with the last event coming from an [Observable]{@link Bacon.Observable}. The global ES6 promise implementation will be used unless a promise constructor `promiseCtr` is given. Use a shim if you need to support legacy browsers or platforms.
* @param {Observable#toPromise~promiseCtr} [promiseCtr]
* @returns {Promise<A>}
*/
toPromise(promiseCtr?:(value:A) => Promise<A>):Promise<A>;
/**
* @callback Observable#firstToPromise~promiseCtr
* @param {A} value
* @returns {Promise<A>}
*/
/**
* @method Observable#firstToPromise
* @description Returns a Promise which will be resolved with the first event coming from an [Observable]{@link Bacon.Observable}. Like [Observable.toPromise]{@link Bacon.Observable#toPromise}, the global ES6 promise implementation will be used unless a promise constructor `promiseCtr` is given.
* @param {Observable#firstToPromise~promiseCtr} [promiseCtr]
* @returns {Promise<A>}
*/
firstToPromise(promiseCtr?:(value:A) => Promise<A>):Promise<A>;
/**
* @method
* @description Throttles the [Observable]{@link Bacon.Observable} using a buffer so that at most one value event in `minimumInteval` is issued. Unlike [EventStream.throttle]{@link Bacon.EventStream#throttle} and [Property.throttle]{@link Bacon.Property#throttle}, it doesn't discard the excessive events but buffers them instead, outputting them with a rate of at most one value per `minimumInterval`.
* @param {number} minimumInterval
* @returns {EventStream<E, A>}
*/
bufferingThrottle(minimumInterval:number):EventStream<E, A>;
/**
* @callback Observable#flatMap~f
* @param {A} value
* @returns {B|Initial<B>|Next<B>|End<B>|Error<E>|Observable<E, B>}
*/
/**
* @method Observable#flatMap
* @description For each element in the source [Observable]{@link Bacon.Observable}, spawn a new stream using the function `f`, and collect events from each of the spawned streams into the result [EventStream]{@link Bacon.EventStream}. The return value of function `f` can be either an Observable (EventStream/[Property]{@link Bacon.Property}) or a constant value. The result of [flatMap]{@link Bacon.Observable#flatMap} is always an EventStream. The "Function Construction rules" apply here. `flatMap` can be used conveniently with [Bacon.once]{@link Bacon.once} and [Bacon.never]{@link Bacon.never} for converting and filtering at the same time, including only some of the results.
* @param {Observable#flatMap~f} f
* @returns {EventStream<E, B>}
* @example
* // Converting strings to integers, skipping empty values:
* Bacon.once("").flatMap(text => {
* return text != "" ? parseInt(text) : Bacon.never();
* });
*/
flatMap<B>(f:(value:A) => B|Initial<B>|Next<B>|End<B>|Error<E>|Observable<E, B>):EventStream<E, B>;
/**
* @callback Observable#flatMapLatest~f
* @param {A} value
* @returns {B|Initial<B>|Next<B>|End<B>|Error<E>|Observable<E, B>}
*/
/**
* @method Observable#flatMapLatest
* @description For each element in the source [Observable]{@link Bacon.Observable}, spawn a new stream using the function `f`, but instead of including events from all spawned streams, only includes them from the latest spawned stream into the result [EventStream]{@link Bacon.EventStream}. The return value of function `f` can be either an Observable (EventStream/[Property]{@link Bacon.Property}) or a constant value. The result of [flatMapLatest]{@link Bacon.Observable#flatMapLatest} is always an EventStream.
* @param {Observable#flatMapLatest~f} f
* @returns {EventStream<E, B>}
*/
flatMapLatest<B>(f:(value:A) => B|Initial<B>|Next<B>|End<B>|Error<E>|Observable<E, B>):EventStream<E, B>;
/**
* @callback Observable#flatMapFirst~f
* @param {A} value
* @returns {B|Initial<B>|Next<B>|End<B>|Error<E>|Observable<E, B>}
*/
/**
* @method Observable#flatMapFirst
* @description For each element in the source [Observable]{@link Bacon.Observable}, spawn a new stream using the function `f` only if the previously spawned stream has ended, and collect events from each of the spawned streams into the result [EventStream]{@link Bacon.EventStream}. The return value of function `f` can be either an Observable (EventStream/[Property]{@link Bacon.Property}) or a constant value. The result of [flatMapFirst]{@link Bacon.Observable#flatMapFirst} is always an EventStream.
* @param {Observable#flatMapFirst~f} f
* @returns {EventStream<E, B>}
*/
flatMapFirst<B>(f:(value:A) => B|Initial<B>|Next<B>|End<B>|Error<E>|Observable<E, B>):EventStream<E, B>;
/**
* @callback Observable#flatMapError~f
* @param {E} error
* @returns {B|Initial<B>|Next<B>|End<B>|Error<E>|Observable<E, B>}
*/
/**
* @method Observable#flatMapError
* @description For each [Error]{@link Bacon.Error} event in the source [Observable]{@link Bacon.Observable}, spawn a new stream using the function `f`, and collect events from each of the spawned streams into the result [EventStream]{@link Bacon.EventStream}. The return value of function `f` can be either an Observable (EventStream/[Property]{@link Bacon.Property}) or a constant value. The result of [flatMapError]{@link Bacon.Observable#flatMapError} is always an EventStream.
* @param {Observable#flatMapError~f} f
* @returns {EventStream<E, B>}
*/
flatMapError<B>(f:(error:E) => B|Initial<B>|Next<B>|End<B>|Error<E>|Observable<E, B>):EventStream<E, B>;
/**
* @callback Observable#flatMapWithConcurrencyLimit~f
* @param {A} value
* @returns {B|Initial<B>|Next<B>|End<B>|Error<E>|Observable<E, B>}
*/
/**
* @method Observable#flatMapWithConcurrencyLimit
* @description For each element in the source [Observable]{@link Bacon.Observable}, spawn a new stream using the function `f`, and collect events from each of the spawned streams into the result [EventStream]{@link Bacon.EventStream}, but limit the number of open spawned streams and buffers incoming events by `limit` amount. The return value of function `f` can be either an Observable (EventStream/[Property]{@link Bacon.Property}) or a constant value. [flatMapConcat]{@link Bacon.Observable#flatMapConcat} is [flatMapWithConcurrencyLimit]{@link Bacon.Observable#flatMapWithConcurrencyLimit}(1) (only one input active), and [flatMap]{@link Bacon.Observable#flatMap} is [flatMapWithConcurrencyLimit]{@link Bacon.Observable#flatMapWithConcurrencyLimit}(∞) (all inputs are piped to output). The result of `flatMapWithConcurrencyLimit` is always an EventStream.
* @param {number} limit
* @param {Observable#flatMapWithConcurrencyLimit~f} f
* @returns {EventStream<E, B>}
*/
flatMapWithConcurrencyLimit<B>(limit:number, f:(value:A) => B|Initial<B>|Next<B>|End<B>|Error<E>|Observable<E, B>):EventStream<E, B>;
/**
* @callback Observable#flatMapConcat~f
* @param {A} value
* @returns {B|Initial<B>|Next<B>|End<B>|Error<E>|Observable<E, B>}
*/
/**
* @method Observable#flatMapConcat
* @description For each element in the source [Observable]{@link Bacon.Observable}, spawn a new stream using the function `f`, and collect events from each of the spawned streams into the result [EventStream]{@link Bacon.EventStream}, but limit the number of open spawned streams and buffers incoming events to 1. The return value of function `f` can be either an Observable (EventStream/[Property]{@link Bacon.Property}) or a constant value. The result of `flatMapConcat` is always an EventStream.
* @param {Observable#flatMapConcat~f} f
* @returns {EventStream<E, B>}
*/
flatMapConcat<B>(f:(value:A) => B|Initial<B>|Next<B>|End<B>|Error<E>|Observable<E, B>):EventStream<E, B>;
/**
* @callback Observable#scan~f
* @param {B} acc
* @param {A} next
* @returns {B}
*/
/**
* @method Observable#scan
* @description Scans [Observable]{@link Bacon.Observable} with given `seed` value and accumulator function `f`, resulting to a [Property]{@link Bacon.Property}. For example, you might use zero as `seed` and a "plus" function as the accumulator to create an "integral" Property. When applied to a Property as in `r = p.scan(seed, f)`, there's a (hopefully insignificant) catch: the starting value for `r` depends on whether `p` has an initial value when `scan` is applied. If there's no initial value, this works identically to `[EventStream]{@link Bacon.EventStream}.scan`: the `seed` will be the initial value of `r`. However, if `r` already has a current/initial value `x`, the seed won't be output as is. Instead, the initial value of `r` will be `f(seed, x)`. This makes sense, because there can only be 1 initial value for a Property at a time.
* @param {B} seed
* @param {Observable#scan~f} f
* @returns {Property<E, B>}
* @example
* Bacon.sequentially(1, [1, 2, 3]).scan(0, (a, b) => a + b);
*/
scan<B>(seed:B, f:(acc:B, next:A) => B):Property<E, B>;
/**
* @callback Observable#fold~f
* @param {B} acc
* @param {A} next
* @returns {B}
*/
/**
* @method Observable#fold
* @description Scans [Observable]{@link Bacon.Observable} with given `seed` value and accumulator function `f`, but only emits the final value, i.e. the value just before the Observable ends. Returns a [Property]{@link Bacon.Property}.
* @param {B} seed
* @param {Observable#fold~f} f
* @returns {Property<E, B>}
*/
fold<B>(seed:B, f:(acc:B, next:A) => B):Property<E, B>;
/**
* @callback Observable#reduce~f
* @param {B} acc
* @param {A} next
* @returns {B}
*/
/**
* @method Observable#reduce
* @description Scans [Observable]{@link Bacon.Observable} with given `seed` value and accumulator function `f`, but only emits the final value, i.e. the value just before the Observable ends. Returns a [Property]{@link Bacon.Property}.
* @param {B} seed
* @param {Observable#reduce~f} f
* @returns {Property<E, B>}
*/
reduce<B>(seed:B, f:(acc:B, next:A) => B):Property<E, B>;
/**
* @callback Observable#diff~f
* @param {A} a
* @param {B} b
* @returns {B}
*/
/**
* @method Observable#diff
* @description Returns a [Property]{@link Bacon.Property} that represents the result of a comparison `f` between the previous and current value of the [Observable]{@link Bacon.Observable}. For the initial value of the Observable, the previous value will be the given `start`.
* @param {A} start
* @param {Observable#diff~f} f
* @returns {Property<E, B>}
* @example
* Bacon.sequentially(1, [1, 2, 3]).diff(0, (a, b) => Math.abs(b - a));
*/
diff<B>(start:A, f:(a:A, b:A) => B):Property<E, B>;
/**
* @callback Observable#zip~f
* @param {A} a
* @param {B} b
* @returns {C}
*/
/**
* @method Observable#zip
* @description Returns an [EventStream]{@link Bacon.EventStream} with elements pair-wise lined up with events from this and the `other` EventStream. A zipped EventStream will publish only when it has a value from each EventStream and will only produce values up to when any single EventStream ends. The given function `f` is used to create the result value from value in the two source EventStream. If no function `f` is given, the values are zipped into an array. Be careful not to have too much "drift" between streams. If one stream produces many more values than some other excessive buffering will occur inside the zipped observable.
* @param {EventStream<E, B>} other
* @param {Observable#zip~f} f
* @returns {EventStream<E, C>}
* @example
* {
* let x = Bacon.fromArray([1, 2]),
* y = Bacon.fromArray([3, 4]);
* x.zip(y, (x, y) => x + y);
* }
*/
zip<B, C>(other:EventStream<E, B>, f:(a:A, b:B) => C):EventStream<E, C>;
/**
* @method
* @description Returns a [Property]{@link Bacon.Property} that represents a "sliding window" into the history of the values of the [Observable]{@link Bacon.Observable}. The resulting Property will have a value that is an array containing the last `n` values of the original Observable, where `n` is at most the value of the `max` argument, and at least the value of the `min` argument. If the `min` argument is omitted, there's no lower limit of values.
* @param {number} max
* @param {number} [min]
* @returns {Property<E, A[]>}
* @example
* // If you have a EventStream `s` with a value sequence `1,2,3,4,5`, the respective values in `s.slidingWindow(2)` would be `[],[1],[1,2],[2,3],[3,4],[4,5]`:
* Bacon.fromArray([1, 2, 3, 4, 5]).slidingWindow(2);
* // The values of `s.slidingWindow(2,2)`would be `[1,2],[2,3],[3,4],[4,5]`:
* Bacon.fromArray([1, 2, 3, 4, 5]).slidingWindow(2, 2);
*/
slidingWindow(max:number, min?:number):Property<E, A[]>;
/**
* @callback Observable#combine~f
* @param {A} a
* @param {B} b
* @returns {C}
*/
/**
* @method Observable#combine
* @description Combines the latest values of the two [EventStream]{@link Bacon.EventStream}s or [Property]{@link Bacon.Property}s using a two-arg function `f`. The result is a Property.
* @param {Property<E, B>} property2
* @param {Observable#combine~f} f
* @returns {Property<E, C>}
*/
combine<B, C>(property2:Property<E, B>, f:(a:A, b:B) => C):Property<E, C>;
/**
* @callback Observable#withStateMachine~f
* @param {B} state
* @param {Initial<A>|Next<A>|End<A>|Error<E>} event
* @returns {[B, (Initial<C>|Next<C>|End<C>|Error<E>)[]]}
*/
/**
* @method Observable#withStateMachine
* @description Lets you run a state machine on an [Observable]{@link Bacon.Observable}. Give it an initial state `initState` object and a state transformation function `f` that processes each incoming [Event]{@link Bacon.Event} and returns and array containing the next `state` and an array of output Event's.
* @param {B} initState
* @param {Observable#withStateMachine~f} f
* @returns {EventStream<E, C>}
* @example
* // Calculate the total sum of all numbers in the stream and output the value on stream end:
* Bacon.fromArray([1, 2, 3]).withStateMachine(0, (sum, event) => {
* if (event.hasValue()) {
* // had to cast to `number` because event:Bacon.Next<number>|Bacon.Error<{}>
* return [sum + <number>event.value(), []];
* } else if (event.isEnd()) {
* return [undefined, [new Bacon.Next(sum), event]];
* } else {
* return [sum, [event]];
* }
* });
*/
withStateMachine<B, C>(initState:B, f:(state:B, event:Initial<A>|Next<A>|End<A>|Error<E>) => [B, (Initial<C>|Next<C>|End<C>|Error<E>)[]]):EventStream<E, C>;
/**
* @method
* @description Decodes input [Observable]{@link Bacon.Observable} using the given `mapping`. Is a bit like a switch-case or the decode function in Oracle SQL. The return value of `decode` is always a [Property]{@link Bacon.Property}.
* @param {Object} mapping
* @returns {Property<E, B>}
* @example
* {
* let property = Bacon.fromArray([1, 2, 3]).toProperty(),
* who = Bacon.fromArray(["A", "B", "C"]).toProperty();
* // The following would map the value 1 into the string "mike" and the value 2 into the value of the `who` property:
* property.decode({1: "mike", 2: who});
* // You can compose static and dynamic data quite freely, as in:
* property.decode({1: {type: "mike"}, 2: {type: "other", whoThen: who}});
* }
*/
decode<B>(mapping:Object):Property<E, B>;
/**
* @method
* @description Creates a [Property]{@link Bacon.Property} that indicates whether Observable is awaiting `otherObservable`, i.e. has produced a value after the latest value from `otherObservable`.
* @param {Observable<E, B>} otherObservable
* @returns {Property<E, boolean>}
* @example
* {
* // This is handy for keeping track whether we are currently awaiting an AJAX response:
* let ajaxRequest = <Bacon.Observable<Error, JQueryXHR>>{},
* ajaxResponse = <Bacon.Observable<Error, JQueryXHR>>{},
* showAjaxIndicator = ajaxRequest.awaiting(ajaxResponse);
* }
*/
awaiting<B>(otherObservable:Observable<E, B>):Property<E, boolean>;
}
/**
* @class EventStream
* @augments Bacon.Observable
* @description A stream of events.
* */
interface EventStream<E, A> extends Observable<E, A> {
/**
* @callback EventStream#map~f
* @param {A} value
* @returns {B}
*/
/**
* @method EventStream#map
* @description Maps [EventStream]{@link Bacon.EventStream} values using given function `f`, returning a new EventStream. The `map` method, among many others, uses lazy evaluation.
* @param {EventStream#map~f} f
* @returns {EventStream<E, B>}
* */
map<B>(f:(value:A) => B):EventStream<E, B>;
/**
* @method
* @description Maps [EventStream]{@link Bacon.EventStream} values using given `constant` value, returning a new EventStream. The `map` method, among many others, uses lazy evaluation.
* @param {B} constant
* @returns {EventStream<E, B>}
* */
map<B>(constant:B):EventStream<E, B>;
/**
* @method
* @description Maps [EventStream]{@link Bacon.EventStream} values using given `propertyExtractor` string like ".keyCode", returning a new EventStream. So, if `propertyExtractor` is a string starting with a dot, the elements will be mapped to the corresponding field/function in the event value. For instance map(".keyCode") will pluck the keyCode field from the input values. If `keyCode` was a function, the result EventStream would contain the values returned by the function. The "Function Construction rules" apply here. The `map` method, among many others, uses lazy evaluation.
* @param {string} propertyExtractor
* @returns {EventStream<E, B>}
* */
map<B>(propertyExtractor:string):EventStream<E, B>;
/**
* @method
* @description Maps [EventStream]{@link Bacon.EventStream} events to the current value of the given [Property]{@link Bacon.Property} `property`. This is equivalent to [Property.sampledBy]{@link Bacon.Property#sampledBy}.
* @param {Property<E, B>} property
* @returns {EventStream<E, B>}
*/
map<B>(property:Property<E, B>):EventStream<E, B>;
/**
* @callback EventStream#mapError~f
* @param {E} error
* @returns {B}
*/
/**
* @method EventStream#mapError
* @description Maps [EventStream]{@link Bacon.EventStream} [Error]{@link Bacon.Error}s using given function `f`. More specifically, feeds the "error" field of the Error event to the function and produces a [Next]{@link Bacon.Next} event based on the return value. The "Function Construction rules" apply here.
* @param {EventStream#mapError~f} f
* @returns {EventStream<E, A|B>}
*/
mapError<B>(f:(error:E) => B):EventStream<E, A|B>;
/**
* @method
* @description Returns an [EventStream]{@link Bacon.EventStream} containing [Error]{@link Bacon.Error} events only. Same as filtering with a function that always returns `false`.
* @returns {EventStream<E, A>}
*/
errors():EventStream<E, A>;
/**
* @method
* @description Skips all [Error]{@link Bacon.Error}s.
* @returns {EventStream<E, A>}
*/
skipErrors():EventStream<E, A>;
/**
* @callback EventStream#mapEnd~f
* @returns {A}
*/
/**
* @method EventStream#mapEnd
* @description Adds an extra [Next]{@link Bacon.Next} event just before [End]{@link Bacon.End} to [EventStream]{@link Bacon.EventStream}. The value is created by calling the given function `f` when the source [EventStream]{@link Bacon.EventStream} ends.
* @param {EventStream#mapEnd~f} f
* @returns {EventStream<E, A>}
*/
mapEnd(f:() => A):EventStream<E, A>;
/**
* @method
* @description Adds an extra [Next]{@link Bacon.Next} event just before [End]{@link Bacon.End} to [EventStream]{@link Bacon.EventStream}. A static `value` is used.
* @param {A} value
* @returns {EventStream<E, A>}
*/
mapEnd(value:A):EventStream<E, A>;
/**
* @callback EventStream#filter~f
* @param {A} value
* @returns {boolean}
*/
/**
* @method EventStream#filter
* @description Filters [EventStream]{@link Bacon.EventStream} `value`s using a given predicate function `f`.
* @param {EventStream#filter~f} f
* @returns {EventStream<E, A>}
*/
filter(f:(value:A) => boolean):EventStream<E, A>;
/**
* @method
* @description Filters [EventStream]{@link Bacon.EventStream} values using a given `constant` value (`true` to include all, `false` to exclude all).
* @param {boolean} bool
* @returns {EventStream<E, A>}
*/
filter(bool:boolean):EventStream<E, A>;
/**
* @method
* @description Filters [EventStream]{@link Bacon.EventStream} values using a given `propertyExtractor` string (like ".isValuable").
* @param {string} propertyExtractor
* @returns {EventStream<E, A>}
*/
filter(propertyExtractor:string):EventStream<E, A>;
/**
* @method
* @description Filters [EventStream]{@link Bacon.EventStream} values based on the value of a [Property]{@link Bacon.Property} `property`. [Event]{@link Bacon.Event} will be included in output IF AND ONLY IF the `property` holds `true` at the time of the event.
* @param {Property<E, boolean>} property
* @returns {EventStream<E, A>}
*/
filter(property:Property<E, boolean>):EventStream<E, A>;
/**
* @callback EventStream#takeWhile~f
* @param {A} value
* @returns {boolean}
*/
/**
* @method EventStream#takeWhile
* @description Takes [EventStream]{@link Bacon.EventStream} values while given predicate function `f` holds `true`, and then ends.
* @param {EventStream#takeWhile} f
* @returns {EventStream<E, A>}
*/
takeWhile(f:(value:A) => boolean):EventStream<E, A>;
/**
* @method
* @description Takes [EventStream]{@link Bacon.EventStream} values while the value of a `property` holds `true`, and then ends.
* @param {Property<E, boolean>} property
* @returns {EventStream<E, A>}
*/
takeWhile(property:Property<E, boolean>):EventStream<E, A>;
/**
* @method
* @description Takes at most n elements from the [EventStream]{@link Bacon.EventStream}. Equal to `Bacon.never()` if `n <= 0`.
* @param {number} n
* @returns {EventStream<E, A>}
*/
take(n:number):EventStream<E, A>;
/**
* @method
* @description Takes elements from [EventStream]{@link Bacon.EventStream} until a [Next]{@link Bacon.Next} event appears in the EventStream `stream`. If `stream` ends without value, it is ignored.
* @param {EventStream<E, B>} stream
* @returns {EventStream<E, A>}
*/
takeUntil<B>(stream:EventStream<E, B>):EventStream<E, A>;
/**
* @method
* @description Takes the first element from the [EventStream]{@link Bacon.EventStream}. Essentially [Observable.take]{@link Bacon.EventStream#take}(1).
* @returns {EventStream<E, A>}
*/
first():EventStream<E, A>;
/**
* @method
* @description Takes the last element from the [EventStream]{@link Bacon.EventStream}. None, if EventStream is empty.
* @returns {EventStream<E, A>}
* @example
* // This creates the stream which doesn't produce any events and never ends:
* Bacon.interval(1e1, 0).last();
*/
last():EventStream<E, A>;
/**
* @method
* @description Skips the first `n` elements from the [EventStream]{@link Bacon.EventStream}.
* @param {number} n
* @returns {EventStream<E, A>}
*/
skip(n:number):EventStream<E, A>;
/**
* @method
* @description Delays the [EventStream]{@link Bacon.EventStream} by given `delay` (in milliseconds).
* @param {number} delay
* @returns {EventStream<E, A>}
*/
delay(delay:number):EventStream<E, A>;
/**
* @method EventStream#throttle
* @description Throttles the [EventStream]{@link Bacon.EventStream} by given `delay` (in milliseconds). Events are emitted with the minimum interval of `delay`. The implementation is based on [EventStream.bufferWithTime]{@link Bacon.EventStream#bufferWithTime}.
* @param {number} delay
* @returns {EventStream<E, A>}
*/
throttle(delay:number):EventStream<E, A>;
/**
* @method EventStream#debounce
* @description Throttles the [EventStream]{@link Bacon.EventStream} by given `delay` (in milliseconds), but so that event is only emitted after the given "quiet period". The difference of [throttle]{@link Bacon.EventStream#throttle} and [debounce]{@link Bacon.EventStream#debounce} is the same as it is in the same methods in jQuery.
* @param {number} delay
* @returns {EventStream<E, A>}
*/
debounce(delay:number):EventStream<E, A>;
/**
* @method
* @description Passes the first event in the [EventStream]{@link Bacon.EventStream} through, but after that, only passes events after a given `delay` (in milliseconds) have passed since previous output.
* @param {number} delay
* @returns {EventStream<E, A>}
*/
debounceImmediate(delay:number):EventStream<E, A>;
/**
* @callback EventStream#doAction~f
* @param {A} value
* @returns {void}
*/
/**
* @method EventStream#doAction
* @description Returns an [EventStream]{@link Bacon.EventStream} where the function `f` is executed for each value, before dispatching to subscribers. This is useful for debugging, but also for stuff like calling the `preventDefault()` method for events.
* @param {EventStream#doAction~f} f
* @returns {EventStream<E, A>}