| 492 | const HaltChainable = Symbol('HaltChainable'); |
| 493 | |
| 494 | class ChainableSynthesis implements IChainableSythensis<any> { |
| 495 | private readonly steps: ((input: any) => any)[] = []; |
| 496 | |
| 497 | map<O>(fn: (i: any) => O): this { |
| 498 | this.steps.push(fn); |
| 499 | return this; |
| 500 | } |
| 501 | |
| 502 | forEach(fn: (i: any) => void): this { |
| 503 | this.steps.push(v => { |
| 504 | fn(v); |
| 505 | return v; |
| 506 | }); |
| 507 | return this; |
| 508 | } |
| 509 | |
| 510 | filter(fn: (e: any) => boolean): this { |
| 511 | this.steps.push(v => fn(v) ? v : HaltChainable); |
| 512 | return this; |
| 513 | } |
| 514 | |
| 515 | reduce<R>(merge: (last: R | undefined, event: any) => R, initial?: R | undefined): this { |
| 516 | let last = initial; |
| 517 | this.steps.push(v => { |
| 518 | last = merge(last, v); |
| 519 | return last; |
| 520 | }); |
| 521 | return this; |
| 522 | } |
| 523 | |
| 524 | latch(equals: (a: any, b: any) => boolean = (a, b) => a === b): ChainableSynthesis { |
| 525 | let firstCall = true; |
| 526 | let cache: any; |
| 527 | this.steps.push(value => { |
| 528 | const shouldEmit = firstCall || !equals(value, cache); |
| 529 | firstCall = false; |
| 530 | cache = value; |
| 531 | return shouldEmit ? value : HaltChainable; |
| 532 | }); |
| 533 | |
| 534 | return this; |
| 535 | } |
| 536 | |
| 537 | public evaluate(value: any) { |
| 538 | for (const step of this.steps) { |
| 539 | value = step(value); |
| 540 | if (value === HaltChainable) { |
| 541 | break; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | return value; |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | export interface IChainableSythensis<T> { |
| 550 | map<O>(fn: (i: T) => O): IChainableSythensis<O>; |
nothing calls this directly
no outgoing calls
no test coverage detected