• Stars
    star
    885
  • Rank 51,582 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An Implementation of Observables for Javascript

zen-observable

An implementation of Observables for JavaScript. Requires Promises or a Promise polyfill.

Install

npm install zen-observable

Usage

import Observable from 'zen-observable';

Observable.of(1, 2, 3).subscribe(x => console.log(x));

API

new Observable(subscribe)

let observable = new Observable(observer => {
  // Emit a single value after 1 second
  let timer = setTimeout(() => {
    observer.next('hello');
    observer.complete();
  }, 1000);

  // On unsubscription, cancel the timer
  return () => clearTimeout(timer);
});

Creates a new Observable object using the specified subscriber function. The subscriber function is called whenever the subscribe method of the observable object is invoked. The subscriber function is passed an observer object which has the following methods:

  • next(value) Sends the next value in the sequence.
  • error(exception) Terminates the sequence with an exception.
  • complete() Terminates the sequence successfully.
  • closed A boolean property whose value is true if the observer's subscription is closed.

The subscriber function can optionally return either a cleanup function or a subscription object. If it returns a cleanup function, that function will be called when the subscription has closed. If it returns a subscription object, then the subscription's unsubscribe method will be invoked when the subscription has closed.

Observable.of(...items)

// Logs 1, 2, 3
Observable.of(1, 2, 3).subscribe(x => {
  console.log(x);
});

Returns an observable which will emit each supplied argument.

Observable.from(value)

let list = [1, 2, 3];

// Iterate over an object
Observable.from(list).subscribe(x => {
  console.log(x);
});
// Convert something 'observable' to an Observable instance
Observable.from(otherObservable).subscribe(x => {
  console.log(x);
});

Converts value to an Observable.

  • If value is an implementation of Observable, then it is converted to an instance of Observable as defined by this library.
  • Otherwise, it is converted to an Observable which synchronously iterates over value.

observable.subscribe([observer])

let subscription = observable.subscribe({
  next(x) { console.log(x) },
  error(err) { console.log(`Finished with error: ${ err }`) },
  complete() { console.log('Finished') }
});

Subscribes to the observable. Observer objects may have any of the following methods:

  • next(value) Receives the next value of the sequence.
  • error(exception) Receives the terminating error of the sequence.
  • complete() Called when the stream has completed successfully.

Returns a subscription object that can be used to cancel the stream.

observable.subscribe(nextCallback[, errorCallback, completeCallback])

let subscription = observable.subscribe(
  x => console.log(x),
  err => console.log(`Finished with error: ${ err }`),
  () => console.log('Finished')
);

Subscribes to the observable with callback functions. Returns a subscription object that can be used to cancel the stream.

observable.forEach(callback)

observable.forEach(x => {
  console.log(`Received value: ${ x }`);
}).then(() => {
  console.log('Finished successfully')
}).catch(err => {
  console.log(`Finished with error: ${ err }`);
})

Subscribes to the observable and returns a Promise for the completion value of the stream. The callback argument is called once for each value in the stream.

observable.filter(callback)

Observable.of(1, 2, 3).filter(value => {
  return value > 2;
}).subscribe(value => {
  console.log(value);
});
// 3

Returns a new Observable that emits all values which pass the test implemented by the callback argument.

observable.map(callback)

Returns a new Observable that emits the results of calling the callback argument for every value in the stream.

Observable.of(1, 2, 3).map(value => {
  return value * 2;
}).subscribe(value => {
  console.log(value);
});
// 2
// 4
// 6

observable.reduce(callback [,initialValue])

Observable.of(0, 1, 2, 3, 4).reduce((previousValue, currentValue) => {
  return previousValue + currentValue;
}).subscribe(result => {
  console.log(result);
});
// 10

Returns a new Observable that applies a function against an accumulator and each value of the stream to reduce it to a single value.

observable.concat(...sources)

Observable.of(1, 2, 3).concat(
  Observable.of(4, 5, 6),
  Observable.of(7, 8, 9)
).subscribe(result => {
  console.log(result);
});
// 1, 2, 3, 4, 5, 6, 7, 8, 9

Merges the current observable with additional observables.

observable.all()

let observable = Observable.of(1, 2, 3);
for (let value of await observable.all()) {
  console.log(value);
}
// 1, 2, 3

Returns a Promise for an array containing all of the values produced by the observable.

More Repositories

1

esparse

A Beautiful Parser for the ECMAScript Language
JavaScript
116
star
2

js-classes-1.1

HTML
83
star
3

esdown

ES6+ to ES5 Compiler
JavaScript
46
star
4

es-cancel-token

Cancel Tokens for ECMAScript
44
star
5

node-default-module-proposal

A Modest Proposal for ES Modules in Node
36
star
6

es6now

ECMAScript 6 to 5 Compiler and Runtime Environment
JavaScript
29
star
7

skert

JavaScript-to-JavaScript compiler tools for holistic language design
JavaScript
27
star
8

proposal-private-symbols

A proposal for private symbols in JavaScript
JavaScript
21
star
9

es-abstract-refs

Abstract References Proposal for ECMAScript
20
star
10

htmltag

HTML Template Tag Compiler
JavaScript
18
star
11

es-typed-catch

Catch anything you like!
10
star
12

zen-push

Observable Push Streams
JavaScript
9
star
13

v8-promise

A Promise polyfill adapted from V8
JavaScript
9
star
14

hidden-state

Attach hidden state to object instances
JavaScript
8
star
15

node-web-modules

Web Modules for Node.js
JavaScript
8
star
16

storelax

An easy observable object store
JavaScript
8
star
17

straylight

A templating and rendering library for HTML
JavaScript
7
star
18

proposal-async-block

Eliminating the IIAAFE so you don't have to
6
star
19

proposal-async-context

4
star
20

streamware

Functional Tools for Asynchronous Streaming
JavaScript
4
star
21

zen-sh

Shelling with Tagged Template Strings
JavaScript
4
star
22

geneviv

Generators are the new observables
JavaScript
4
star
23

chromium-futures

C++
3
star
24

reflect.apply

Thoughts on a life in software
CSS
2
star
25

lightstyle

JavaScript
1
star
26

event-stream

JavaScript
1
star
27

async-iteration-buffer

JavaScript
1
star
28

hissyfit

Easy JS Errors
JavaScript
1
star
29

observe-protocol

JavaScript
1
star
30

es-observable-cancellation

JavaScript
1
star
31

htmltag-string

HTML string template tag
JavaScript
1
star
32

ziptar

Zip and Tar for Node.js
JavaScript
1
star
33

moon-unit

Unit Testing Library for JavaScript
JavaScript
1
star