Install Kefir via NPM
masterInstall Kefir as a dependency using npm.
npm install kefirrepository·master·Indexed 23 days ago
https://github.com/kefirjs/kefirKefir is a high-performance, low-memory Reactive Programming library for JavaScript inspired by Bacon.js and RxJS. It provides tools for creating, transforming, and combining Streams and Properties, featuring a rich set of operators for filtering, time-based buffering, and error handling. Version 3.8.8 includes Flow type annotations for type-checking streams.
Install Kefir as a dependency using npm.
npm install kefirThe NPM package includes Flow definitions. You can use them to type-check streams and benefit from automatic type inference for values within a stream.
// @flow
import Kefir from 'kefir'
function foo(numberStream: Kefir.Observable<number>) {
numberStream.onValue(x => {
// Flow knows x is a number here
})
}
const s = Kefir.constant(5)
// Flow can automatically infer the type of values in the stream and determine
// that `s` is of type Kefir.Observable<number> here.
foo(s)Install Kefir using Bower.
bower install kefirKefir provides several ways to combine multiple observables into one:
Combining Multiple Sources:
Kefir.combine(obss, [fn]): Combines multiple observables. The fn is an optional combinator.Kefir.combine(obss, passiveObs, [fn]): Combines multiple observables, sampled by a passive observable.Kefir.zip(sources, [combinator]): Zips sources together. sources can include ordinary arrays alongside observables.Kefir.merge(obss): Merges multiple observables into one.Kefir.concat(obss): Concatenates observables in sequence.Kefir.pool(): Equivalent to a Bacon Bus; allows manual injection of values.Combining Two Observables:
obs.combine(otherObs, [fn]): Combines two observables.obs.zip(otherObs, [fn]): Zips two observables.obs.merge(otherObs): Merges two observables.obs.concat(otherObs): Concatenates two observables.obs.sampledBy(otherObs, [fn]): Samples the current observable using another observable.obs.filterBy(otherObs): Filters values based on another observable.obs.takeWhileBy(otherObs): Takes values while another observable emits.obs.skipWhileBy(otherObs): Skips values while another observable emits.obs.skipUntilBy(otherObs): Skips values until another observable emits.obs.takeUntilBy(otherObs): Takes values until another observable emits.obs.awaiting(otherObs): Waits for another observable to emit.obs.bufferBy(otherObs, [options]): Buffers values based on another observable.Use these methods to transform or filter observable data:
Transformation:
obs.map(fn): Transforms values.obs.errorsToValues(fn): Converts errors to values. The function fn should return an object: {convert: Boolean, value: Any}.obs.mapErrors(fn): Maps errors without converting them to values.obs.scan(fn, [seed]): Accumulates values (note: seed is the second argument and is optional).obs.reduce(fn, [seed]): Reduces values (note: seed is the second argument and is optional).obs.diff([fn], [seed]): Computes differences (note: arguments are optional and order is [fn], [seed]).obs.beforeEnd(fn): Runs a function before the observable ends.Filtering and Control:
obs.filter(predicate): Filters values based on a predicate.obs.skip(n): Skips the first n values.obs.skipWhile(predicate): Skips values while the predicate is true.obs.skipDuplicates([comparator]): Skips consecutive duplicate values.obs.take(n): Takes only the first n values.obs.takeWhile(predicate): Takes values until the predicate is false.obs.skipErrors(): Ignores error events.obs.skipEnd(): Ignores end events.obs.flatten([transformer]): Flattens nested observables.obs.throttle(delay, [options]): Throttles events (supports an options object).obs.debounce(delay, [options]): Debounces events (supports an options object). Use {immediate: true} for immediate debouncing.obs.delay(delay): Delays all events.Other:
obs.flatMap([fn]): Flattens mapped observables.obs.flatMapLatest([fn]): Flattens mapped observables, keeping only the latest.obs.flatMapFirst([fn]): Flattens mapped observables, keeping only the first.obs.flatMapConcurLimit([fn], limit): Limits concurrency of flatMap operations.obs.flatMapConcat([fn]): Concatenates mapped observables.obs.withHandler(handler): Provides low-level control over the observable stream via a handler.Kefir uses on... methods for subscription and off... methods for unsubscription. Unlike Bacon, Kefir methods return this to allow chaining, rather than returning an unsubscribe function.
Subscription Methods:
obs.onValue(fn): Called for every value.obs.onError(fn): Called for every error.obs.onEnd(fn): Called when the observable ends.obs.onAny(fn): Called for any event (value, error, or end).obs.log([name]): Logs events to the console.Unsubscription Methods:
obs.offValue(fn)obs.offError(fn)obs.offEnd(fn)obs.offAny(fn)obs.offLog([name])Metadata:
obs.setName(newName): Sets a name for the observable.Kefir properties represent values that change over time. Use these methods to create them:
Kefir.constant(value): Creates a property that always holds the same value.Kefir.constantError(error): Creates a property that holds an error (Note: Bacon properties typically only hold values).Kefir.fromPromise(promise): Unlike Bacon, this returns a Property instead of a Stream.Transform existing observables between Streams and Properties:
property.changes(): Returns a stream of changes in the property.stream.toProperty([getCurrent]): Converts a stream to a property. The argument can be a value or a callback function getCurrent to retrieve the current value.Kefir provides a rich set of operators on the Observable.prototype to transform data flows. These work on both Streams and Properties:
map(fn): Transforms each value using fn.filter(fn): Only emits values that satisfy fn.take(n): Emits only the first n values.skip(n): Skips the first n values.skipWhile(fn): Skips values as long as fn is true.takeWhile(fn): Emits values as long as fn is true.scan(fn, seed): Accumulates state over time, similar to Array.reduce.diff(fn, seed): Emits values that are different from the previous value (based on fn).skipDuplicates(fn): Emits values only if they are different from the previous value (based on fn).delay(wait): Delays all emissions by wait.throttle(wait, options): Limits emissions to at most one per wait period. options can include leading and trailing booleans.debounce(wait, options): Emits a value only after wait has passed without another emission. options can include immediate boolean.bufferWithCount(count, options): Buffers values into arrays of size count.bufferWhile(fn, options): Buffers values into arrays as long as fn is true. options can include flushOnEnd.bufferWithTimeOrCount(wait, count, options): Buffers values by either time or count.slidingWindow(max, min): Emits a sliding window of values.mapErrors(fn): Transforms error notifications.filterErrors(fn): Filters error notifications.ignoreErrors(): Silences all errors.mapErrors(fn): Transforms error notifications.flatten(fn): Flattens nested observables.transduce(transducer): Applies a transducer to the stream.Kefir allows you to merge or combine multiple sources:
combine(other, combinator): Combines this observable with another using a combinator function.zip(other, combinator): Zips this observable with another using a combinator function.merge(other): Merges this observable with another.concat(other): Concatenates this observable with another.flatMap(fn): Maps each value to a new observable and flattens the result. Variants include:flatMapLatest(fn): Only keeps the most recent inner observable (drops older ones).flatMapFirst(fn): Keeps the first inner observable and ignores subsequent ones.flatMapConcat(fn): Queues inner observables to run one after another.flatMapConcurLimit(fn, limit): Runs multiple inner observables with a concurrency limit.Pool: A utility to manage multiple sources. Use pool() to create a new instance.repeat(fn): Repeats the observable based on a function.filterBy(other): Filters values of this observable based on emissions from other.sampledBy(other, combinator): Samples values from this observable whenever other emits.skipUntilBy(other): Skips values from this observable until other emits.takeUntilBy(other): Takes values from this observable until other emits.bufferBy(other, options): Buffers values of this observable based on emissions from other.bufferWhileBy(other, options): Buffers values of this observable while other emits.awaiting(other): (Deprecated) Awaits the completion of other.You can transform Streams and Properties into other types using these methods:
toProperty(fn): Converts a Stream or Property into a Property.changes(): Returns a Stream of the values that a Property changes to.toPromise(PromiseConstructor): Converts a Stream or Property into a Promise.fromPromise(promise): Converts a Promise into a Property.fromESObservable(esObservable): Converts an ES Observable into a Kefir Stream.toESObservable(): Converts a Kefir Observable into an ES7 Observable.