Created
February 16, 2018 22:00
-
-
Save lolgesten/318dd558786d6da3d56338c123bdf3c7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference types="mocha"/> | |
/// <reference types="node" /> | |
import xs from '../../src/index'; | |
import * as assert from 'assert'; | |
describe.only('xs.from', () => { | |
it('should have the correct order in an observable', (done: any) => { | |
const START_STATE = { | |
ver: 0, // counts up to see out of order | |
a: false, // we toggle this on a timer | |
b: false, // we toggle this on a different timer | |
c: false, // we derive this from a and b | |
}; | |
function loop() { | |
// imitate cycle | |
const update$ = xs.create(); | |
// the folded state, with ever increasing version number | |
const state$ = update$. | |
fold((p, c) => ({ ...p, ver: p.ver + 1, ...c }), START_STATE); | |
// the timer updates | |
const aUpdate$ = xs.periodic(10).map(n => ({ a: n % 2 === 0 })).take(20); | |
const bUpdate$ = xs.periodic(20).map(n => ({ b: n % 3 === 0 })).take(10); | |
// derived update from a/b | |
const derivedUpdate$ = state$ | |
.filter(state => state.c !== state.a && state.b) | |
.map(state => ({ c: state.a && state.b })); | |
// the proxy that sees the error | |
const proxy$ = xs.from(state$).debug('proxy'); | |
// merge the update streams | |
const realUpdate$ = xs.merge( | |
aUpdate$, | |
bUpdate$, | |
derivedUpdate$ | |
); | |
// and cycle back to top | |
update$.imitate(realUpdate$); | |
return { | |
state$, | |
proxy$, | |
}; | |
} | |
const { state$, proxy$ } = loop(); | |
state$.addListener({ next: v => { }, complete: done }); | |
proxy$ | |
.fold((p, c) => { | |
if (p !== c.ver) { | |
done(Error(`Out of order: ${p} ${c.ver}`)); | |
} | |
return p + 1; | |
}, 0) | |
.addListener({ next: v => { } }); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment