forked from lelandmiller/micro-signals
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(): add reduce() to extended signal
Add a reduce() method to the ReadableSignal interface, which behaves similarly to Array.prototype.reduce().
- Loading branch information
Kevin Kirchhoff
committed
Oct 30, 2019
1 parent
ccc139d
commit cdb89e9
Showing
5 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import test = require('tape'); | ||
import {Accumulator, ReadableSignal, Signal} from '../../src'; | ||
import {LeakDetectionSignal} from '../lib/leak-detection-signal'; | ||
import {parentChildSuite} from './parent-child-suite'; | ||
|
||
export type ReducedSignalCreationFunction = <T, U>( | ||
baseSignal: ReadableSignal<T>, | ||
accumulator: Accumulator<T, U>, | ||
initialValue: U, | ||
) => ReadableSignal<U>; | ||
|
||
export function reducedSuite(prefix: string, createReducedSignal: ReducedSignalCreationFunction) { | ||
parentChildSuite(prefix, () => { | ||
const parentSignal = new Signal(); | ||
const childSignal = createReducedSignal(parentSignal, (_, payload) => payload, undefined); | ||
return { parentSignal, childSignal }; | ||
}); | ||
|
||
test(`${prefix} should dispatch with the accumulated payload`, t => { | ||
const baseSignal = new Signal<number>(); | ||
|
||
const reducedSignal = baseSignal.reduce((accum, curr) => accum + curr, 5); | ||
|
||
const addResults: number[] = []; | ||
const addOnceResults: number[] = []; | ||
|
||
reducedSignal.add(x => addResults.push(x)); | ||
reducedSignal.addOnce(x => addOnceResults.push(x)); | ||
|
||
baseSignal.dispatch(50); | ||
baseSignal.dispatch(0); | ||
baseSignal.dispatch(100); | ||
|
||
t.deepEqual(addResults, [55, 55, 155]); | ||
t.deepEqual(addOnceResults, [55]); | ||
|
||
t.end(); | ||
}); | ||
|
||
test('ReducedSignal should not leak', t => { | ||
const signal = new LeakDetectionSignal<void>(); | ||
const mappedSignal = createReducedSignal(signal, () => true, false); | ||
|
||
const listener = () => { /* empty listener */ }; | ||
mappedSignal.add(listener); | ||
signal.dispatch(undefined); | ||
mappedSignal.remove(listener); | ||
|
||
t.equal(signal.listenerCount, 0); | ||
t.end(); | ||
}); | ||
} |