0.0.2-alpha.6 • Published 7 years ago

rx-computed v0.0.2-alpha.6

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

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();
0.0.2-alpha.6

7 years ago

0.0.2-alpha.5

7 years ago

0.0.2-alpha.4

7 years ago

0.0.2-alpha.3

7 years ago

0.0.2-alpha.2

7 years ago

0.0.2-alpha.1

7 years ago

0.0.1-alpha.4

7 years ago

0.0.1-alpha.3

7 years ago

0.0.1-alpha.2

7 years ago

0.0.1-alpha.1a

7 years ago

0.0.1-alpha.1

7 years ago