-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
62 lines (47 loc) · 1.41 KB
/
index.ts
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
import * as assert from 'assert';
import { periodic, iterate } from 'most';
import { proxy } from '../src';
function assertExpected (expected: Array<any>, done: Function) {
return function (x: any) {
assert.strictEqual(x, expected.shift());
if (expected.length === 0) {
done();
}
};
}
describe('most-proxy', () => {
it('creates a circular dependency', (done) => {
const { attach, stream } = proxy();
const original = periodic(10, 1).scan((x, y) => x + y, 0);
attach(original);
stream.take(3).observe(assertExpected([0, 1, 2], done));
});
it('handles being attached to itself', (done) => {
const { attach, stream } = proxy<number>();
function makeAssertions (currentValue: number) {
if (currentValue === 8) {
setTimeout(() => done(), 20);
}
if (currentValue > 8) {
done(new Error('You have a memory leak'));
}
}
const original = stream
.tap(makeAssertions)
.startWith(1)
.map((x: number) => x * 2);
// pipe events from original to proxy stream
const proxyStream = attach(original.delay(10));
proxyStream
.take(3)
.drain();
});
it('allows reattaching after completion', () => {
const { attach, stream } = proxy();
const original = iterate(x => x + 1, 0).take(3);
attach(original);
return stream.drain().then(() => {
return attach(original).drain();
});
});
});