-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
186 additions
and
20 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2017-2018 @polkadot/api authors & contributors | ||
// This software may be modified and distributed under the terms | ||
// of the ISC license. See the LICENSE file for details. | ||
|
||
import { isFunction } from '@polkadot/util'; | ||
|
||
export type CombinatorCallback = (value: Array<any>) => any; | ||
|
||
type CombinatorFunction = (value: any) => void; | ||
|
||
export default class Combinator { | ||
protected _callback?: CombinatorCallback; | ||
protected _results: Array<any>; | ||
|
||
constructor (callback?: CombinatorCallback) { | ||
this._callback = callback; | ||
this._results = []; | ||
} | ||
|
||
next (): CombinatorFunction { | ||
const index = this._results.length; | ||
|
||
// Add an empty value, so we are not operating a sparse array | ||
this._results[index] = undefined; | ||
|
||
return (value: any): void => { | ||
this._results[index] = value; | ||
|
||
this.triggerUpdate(); | ||
}; | ||
} | ||
|
||
subscribe (callback: CombinatorCallback): void { | ||
this._callback = callback; | ||
|
||
this.triggerUpdate(); | ||
} | ||
|
||
protected triggerUpdate (): void { | ||
if (!isFunction(this._callback) || !this._results.length) { | ||
return; | ||
} | ||
|
||
try { | ||
this._callback(this._results); | ||
} catch (error) { | ||
// swallow, we don't want the handler to trip us up | ||
} | ||
} | ||
} |
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,97 @@ | ||
// Copyright 2017-2018 @polkadot/api authors & contributors | ||
// This software may be modified and distributed under the terms | ||
// of the ISC license. See the LICENSE file for details. | ||
|
||
import Combinator from './Combinator'; | ||
|
||
describe('Combinator', () => { | ||
it('creates a simple combinator, trigerring on all values', (done) => { | ||
let count = 0; | ||
|
||
const combinator = new Combinator((value: Array<any>) => { | ||
expect(value[0]).toEqual(`test${count}`); | ||
|
||
count++; | ||
|
||
if (count === 3) { | ||
done(); | ||
} | ||
}); | ||
|
||
const fn = combinator.next(); | ||
|
||
fn('test0'); | ||
fn('test1'); | ||
fn('test2'); | ||
}); | ||
|
||
it('creates a combinator that combines values from 2 sources', (done) => { | ||
let count = 0; | ||
|
||
const combinator = new Combinator((value: Array<any>) => { | ||
expect(value).toEqual( | ||
count === 0 | ||
? ['test0'] | ||
: ['test0', 'test1'] | ||
); | ||
|
||
count++; | ||
|
||
if (count === 2) { | ||
done(); | ||
} | ||
}); | ||
|
||
combinator.next()('test0'); | ||
combinator.next()('test1'); | ||
}); | ||
|
||
it('creates a combinator that combines values from 2 sources (filling empty)', (done) => { | ||
let count = 0; | ||
|
||
const combinator = new Combinator((value: Array<any>) => { | ||
expect(value).toEqual( | ||
count === 0 | ||
? ['test0', undefined] | ||
: ['test0', 'test1'] | ||
); | ||
|
||
count++; | ||
|
||
if (count === 2) { | ||
done(); | ||
} | ||
}); | ||
|
||
const fn0 = combinator.next(); | ||
const fn1 = combinator.next(); | ||
|
||
fn0('test0'); | ||
fn1('test1'); | ||
}); | ||
|
||
it('allows subscription after use, and subsequent next()', (done) => { | ||
let count = 0; | ||
const combinator = new Combinator(); | ||
|
||
combinator.next()('test0'); | ||
combinator.next()('test1'); | ||
combinator.next()('test2'); | ||
|
||
combinator.subscribe((value: Array<any>) => { | ||
expect(value).toEqual( | ||
count === 0 | ||
? ['test0', 'test1', 'test2'] | ||
: ['test0', 'test1', 'test2', 'test3'] | ||
); | ||
|
||
count++; | ||
|
||
if (count === 2) { | ||
done(); | ||
} | ||
}); | ||
|
||
combinator.next()('test3'); | ||
}); | ||
}); |
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