/** @module hareactive/behavior */
import { Observer } from "./frp-common";
import { Future } from "./Future";
import { Stream } from "./Stream";
/**
* A behavior is a value that changes over time. Conceptually it can
* be though of as a function from time to a value. I.e. `type
* Behavior = (t: Time) => A`.
*/
export declare abstract class Behavior implements Observer {
pushing: boolean;
last: A;
nrOfListeners: number;
child: Observer;
abstract push(a: any): void;
abstract pull(): A;
constructor();
map(fn: (a: A) => B): Behavior;
of: (v: A) => Behavior;
static of: (v: A) => Behavior;
ap(f: Behavior<(a: A) => B>): Behavior;
lift(f: (t: T1) => R, m: Behavior): Behavior;
lift(f: (t: T1, u: T2) => R, m1: Behavior, m2: Behavior): Behavior;
lift(f: (t1: T1, t2: T2, t3: T3) => R, m1: Behavior, m2: Behavior, m3: Behavior): Behavior;
chain(fn: (a: A) => Behavior): Behavior;
endPulling(): void;
beginPulling(): void;
subscribe(cb: (a: A) => void): Observer;
addListener(c: Observer): void;
removeListener(listener: Observer): void;
}
export declare function of(val: B): Behavior;
export declare function at(b: Behavior): B;
/**
* Map a function over a behavior. This means that if at some point in
* time the value of `b` is `bVal` then the value of the returned
* behavior is `fn(bVal)`.
*/
export declare function map(fn: (a: A) => B, b: Behavior): Behavior;
/**
* Apply a function valued behavior to a value behavior.
*
* @param fnB behavior of functions from `A` to `B`
* @param valB A behavior of `A`
* @returns Behavior of the function in `fnB` applied to the value in `valB`
*/
export declare function ap(fnB: Behavior<(a: A) => B>, valB: Behavior): Behavior;
/**
* Creates a behavior for imperative impure pushing.
*/
export declare function sink(initialValue: A): Behavior;
/**
* A placeholder behavior is a behavior without any value. It is used
* to do value recursion in `./framework.ts`.
* @private
*/
export declare class PlaceholderBehavior extends Behavior {
private source;
constructor();
push(v: B): void;
pull(): B;
replaceWith(b: Behavior): void;
}
export declare function placeholder(): PlaceholderBehavior;
/**
* Take a behavior `b` of booleans and return a behavior that for time
* `t` contains a future that occurs when `b` is true after `t`.
* @param b - A boolean valued behavior.
*/
export declare function when(b: Behavior): Behavior>;
/**
* Creates a future than on occurence samples the current value of the
* behavior and occurs with that value. That is, the original value of
* the future is overwritten with the behavior value at the time when
* the future occurs.
*/
export declare function snapshot(behavior: Behavior, future: Future): Behavior>;
/**
* From an initial behavior and a future of a behavior `switcher`
* creates a new behavior that acts exactly like `initial` until
* `next` occurs after which it acts like the behavior it contains.
*/
export declare function switcher(init: Behavior, next: Future>): Behavior;
/**
* Creates a Behavior whose value is the last occurrence in the stream.
* @param initial the initial value that the behavior has
* @param steps the stream that will change the value of the behavior
*/
export declare function stepper(initial: B, steps: Stream): Behavior;
/**
* The returned behavior initially has the initial value, on each
* occurence in `source` the function is applied to the current value
* of the behaviour and the value of the occurence, the returned value
* becomes the next value of the behavior.
*/
export declare function scan(fn: (a: A, b: B) => B, init: B, source: Stream): Behavior>;
/**
* This takes an impure function that varies over time and returns a
* pull-driven behavior. This is particulairly useful if the function
* is contionusly changing, like `Date.now`.
*/
export declare function fromFunction(fn: () => B): Behavior;
/**
* Subscribe to a behavior in order to run imperative actions when the
* value in the behavior changes.
*/
export declare function subscribe(fn: (a: A) => void, b: Behavior): void;
export declare class CbObserver implements Observer {
private _push;
private _beginPulling;
private _endPulling;
private source;
constructor(_push: (a: A) => void, _beginPulling: () => void, _endPulling: () => void, source: Behavior);
push(a: A): void;
beginPulling(): void;
endPulling(): void;
}
/**
* Observe a behavior for the purpose of executing imperative actions
* based on the value of the behavior.
*/
export declare function observe(push: (a: A) => void, beginPulling: () => void, endPulling: () => void, b: Behavior): CbObserver;
/**
* Imperatively push a value into a behavior.
*/
export declare function publish(a: A, b: Behavior): void;
export declare function isBehavior(b: any): b is Behavior;
export declare type Time = number;
/**
* A behavior whose value is the number of milliseconds elapsed win
* UNIX epoch. I.e. its current value is equal to the value got by
* calling `Date.now`.
*/
export declare const time: Behavior