observable-tests.ts
52 lines
| 1.1 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r102 | import { observe } from "./observable"; | ||
| import * as t from "tap"; | ||||
| const subj1 = observe<number>(({ next, complete }) => { | ||||
| next(1); | ||||
| complete(); | ||||
| next(2); | ||||
| }); | ||||
| const consumer1 = { | ||||
| sum: 0, | ||||
| next(v: number) { | ||||
| this.sum += v; | ||||
| } | ||||
|
|
r108 | }; | ||
|
|
r102 | |||
| subj1.subscribe(consumer1); | ||||
| t.equal(consumer1.sum, 1, "Should get only one value"); | ||||
| subj1.subscribe(consumer1); | ||||
| t.equal(consumer1.sum, 2, "Should get the value again"); | ||||
| const consumer2 = { | ||||
| value: 0, | ||||
| completed: false, | ||||
| next(v: number) { this.value = v; }, | ||||
| complete() { this.completed = true; } | ||||
| }; | ||||
| let maps = 0; | ||||
| subj1 | ||||
| .map(v => { | ||||
|
|
r108 | t.comment(`map1: ${v * 2}`); | ||
|
|
r102 | maps++; | ||
| return v * 2; | ||||
| }) | ||||
| .map (v => { | ||||
|
|
r108 | t.comment(`map2: ${v * 2}`); | ||
|
|
r102 | maps++; | ||
| return v * 2; | ||||
| }) | ||||
| .map(v => { | ||||
|
|
r108 | t.comment(`map3: ${v * 2}`); | ||
|
|
r102 | maps++; | ||
|
|
r108 | return v * 2; | ||
|
|
r102 | }) | ||
| .subscribe(consumer2); | ||||
| t.equal(consumer2.value, 8, "Should map"); | ||||
| t.equal(maps, 3, "The map chain should not be executed after completion"); | ||||
|
|
r110 | t.ok(consumer2.completed, "The completion signal should pass through"); | ||
