Giter VIP home page Giter VIP logo

rx-computed's Introduction

RxComputed

Make it possible to use RxJS in a manner similar to Knockout computed observables.

Installation

npm install --save rx-computed

Usage in TypeScript (sync)

import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { RxComputed } from 'rx-computed';
import * as assert from "power-assert";

// debug how many times the computed function is called
let counter = 0;

// variables that our computed will depend on
let n1 = new BehaviorSubject(10);
let n2 = new BehaviorSubject(100);

let computed = RxComputed.sync<number>(context => {
	++counter;
	return context.get(n1) + context.get(n2);
});

// the computed function is called when creating the object
assert.equal(counter, 1);
assert.equal(computed.value, 110);

// changing n1 will re-evaluate the computed
n1.next(11);
assert.equal(counter, 2);
assert.equal(computed.value, 111);

// changing n2 will re-evaluate the computed
n2.next(200);
assert.equal(counter, 3);
assert.equal(computed.value, 211);

// dispose the computed
computed.dispose();

// changing n2 will not re-evaluate the disposed computed
n2.next(300);
assert.equal(counter, 3);

Usage in TypeScript (async)

import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { RxComputed }  from 'rx-computed';

// variable that our computed will depend on
let n1 = new BehaviorSubject(10);

let computed = RxComputed.async<number>(context => {
	let val1 = context.get(n1);

	return new Promise<number>(resolve => {
		setTimeout(() => {
			resolve(val1 + 1);
		}, 10);
	});
});

// ...

// dispose the computed
computed.dispose();

rx-computed's People

Contributors

asfernandes avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.