Add api-promise combinator unsubscribe (#520)

* Add api-promise combinator unsubscribe

* Allow combinator lookup

* auto-imports removed

* Promise handling
This commit is contained in:
Jaco Greeff
2018-12-30 15:01:39 +01:00
committed by GitHub
parent 46f0290910
commit 8919ae2edc
2 changed files with 71 additions and 9 deletions
+42 -4
View File
@@ -5,24 +5,43 @@
import { isFunction } from '@polkadot/util';
export type CombinatorCallback = (value: Array<any>) => any;
export type CombinatorFunction = (cb: (value: any) => void) => any;
export type CombinatorFunction = {
(cb: (value: any) => void): Promise<number>,
unsubscribe?: (subscriptionsId: number) => Promise<any>
};
let combinatorId = 5000;
const allCombinators: { [index: number]: Combinator } = {};
export default class Combinator {
protected _allHasFired: boolean = false;
protected _callback: CombinatorCallback;
protected _fired: Array<boolean> = [];
protected _fns: Array<CombinatorFunction> = [];
protected _id: number = ++combinatorId;
protected _results: Array<any> = [];
protected _subscriptionIds: Array<Promise<number>> = [];
constructor (fns: Array<CombinatorFunction>, callback: CombinatorCallback) {
this._callback = callback;
allCombinators[this._id] = this;
fns.forEach((fn, index) => {
this._callback = callback;
this._fns = fns;
this._subscriptionIds = fns.map((fn, index): Promise<number> => {
this._fired.push(false);
fn(this.createCallback(index));
return fn(this.createCallback(index));
});
}
static lookup (id: number): Combinator {
return allCombinators[id];
}
get id (): number {
return this._id;
}
protected allHasFired (): boolean {
if (!this._allHasFired) {
this._allHasFired = this._fired.filter((hasFired) => !hasFired).length === 0;
@@ -51,4 +70,23 @@ export default class Combinator {
// swallow, we don't want the handler to trip us up
}
}
unsubscribe (): Promise<any> {
delete allCombinators[this._id];
return Promise.all(
this._subscriptionIds.map((subscriptionPromise, index) => {
const unsubscribe = this._fns[index].unsubscribe;
return !unsubscribe
? Promise.resolve(true)
: subscriptionPromise
.then((subscriptionId) =>
unsubscribe(subscriptionId)
)
.then(() => true)
.catch(() => false);
})
);
}
}
+29 -5
View File
@@ -10,7 +10,7 @@ describe('Combinator', () => {
const fns: Array<(value: any) => void> = [];
const combinator = new Combinator(
[
(cb) => fns.push(cb)
(cb) => Promise.resolve(fns.push(cb))
],
(value: Array<any>) => {
expect(value[0]).toEqual(`test${count}`);
@@ -34,8 +34,8 @@ describe('Combinator', () => {
const fns: Array<(value: any) => void> = [];
const combinator = new Combinator(
[
(cb) => fns.push(cb),
(cb) => fns.push(cb)
(cb) => Promise.resolve(fns.push(cb)),
(cb) => Promise.resolve(fns.push(cb))
],
(value: Array<any>) => {
expect(value).toEqual(['test0', 'test1']);
@@ -55,8 +55,8 @@ describe('Combinator', () => {
const fns: Array<(value: any) => void> = [];
const combinator = new Combinator(
[
(cb) => fns.push(cb),
(cb) => fns.push(cb)
(cb) => Promise.resolve(fns.push(cb)),
(cb) => Promise.resolve(fns.push(cb))
],
(value: Array<any>) => {
expect(value).toEqual(
@@ -78,4 +78,28 @@ describe('Combinator', () => {
expect(combinator).toBeDefined();
});
it('unsubscribes as required', (done) => {
const mocker = () => Promise.resolve(12345);
mocker.unsubscribe = async (subId: number) => {
if (subId === 12345) {
done();
}
};
const combinator = new Combinator([
mocker,
() => Promise.resolve(98765)
], (value: Array<any>) => {
// ignore
});
combinator
.unsubscribe()
.then(() => {
// ignore
})
.catch(() => {
// ignore
});
});
});