-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathderivables.js
70 lines (50 loc) · 1.76 KB
/
derivables.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
let Rx = require('rxjs')
let Dv = require('derivable')
let atom = Dv.atom;
let derive = Dv.derive;
let transact = Dv.transact;
const name = atom("World");
const countryCode = atom("en");
const greetings = {
en: "Hello",
nl: "Hallo",
es: "Hola",
fr: "Bonjour"
};
const greeting = countryCode.derive(cc => greetings[cc]);
countryCode.react(msg => console.log("Atom 'countryCode' set to "+msg));
// greeting.react(msg => console.log("greeting set to "+msg));
const getDerivableObservable = function(myDerivable) {
return Rx.Observable.create( function (observer) {
const stopSignal = atom(false);
myDerivable.react(val=>{ observer.next(val) }, {until: stopSignal});
return function () {stopSignal.set(true)}
});
}
const observable = getDerivableObservable(greeting);
observable.take(3).subscribe(
(x) => console.log('next: %s', x.login ? x.login : x) ,
(e) => console.log('error: %s', e) ,
() => console.log('completed')
);
countryCode.set("nl")
countryCode.set("es")
countryCode.set("fr")
setTimeout(()=>{countryCode.set("en")},3000);
function erlangInterval(ratePerMinute)
{
return (-1/ratePerMinute*Math.log(Math.random()))*60000;
}
Rx.Observable.prototype.naturalDelay = function (ratePerMinute) {
return this.concatMap(u=>Rx.Observable.interval(erlangInterval(ratePerMinute)).take(1), u => u);
}
Rx.Observable.prototype.toAtom = function (toAtom) {
return this.do(u=>toAtom.set(u), u => u);
}
const observable2 = Rx.Observable.of('en','nl','fr','es','nl').naturalDelay(60).take(3).toAtom(countryCode);
// observable2.subscribe(
// (x) => console.log('observable: %s', x.login ? x.login : x) ,
// (e) => console.log('error: %s', e) ,
// () => console.log('completed')
// );
// setTimeout(()=>{countryCode.set("en")},3000);