From 8919ae2edc1c3b38c3afb01d94d5c3b6080808d6 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 30 Dec 2018 15:01:39 +0100 Subject: [PATCH] Add api-promise combinator unsubscribe (#520) * Add api-promise combinator unsubscribe * Allow combinator lookup * auto-imports removed * Promise handling --- packages/api/src/promise/Combinator.ts | 46 ++++++++++++++++++-- packages/api/src/promise/Combinators.spec.ts | 34 ++++++++++++--- 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/packages/api/src/promise/Combinator.ts b/packages/api/src/promise/Combinator.ts index 6dec68307d..76856506cc 100644 --- a/packages/api/src/promise/Combinator.ts +++ b/packages/api/src/promise/Combinator.ts @@ -5,24 +5,43 @@ import { isFunction } from '@polkadot/util'; export type CombinatorCallback = (value: Array) => any; -export type CombinatorFunction = (cb: (value: any) => void) => any; +export type CombinatorFunction = { + (cb: (value: any) => void): Promise, + unsubscribe?: (subscriptionsId: number) => Promise +}; + +let combinatorId = 5000; +const allCombinators: { [index: number]: Combinator } = {}; export default class Combinator { protected _allHasFired: boolean = false; protected _callback: CombinatorCallback; protected _fired: Array = []; + protected _fns: Array = []; + protected _id: number = ++combinatorId; protected _results: Array = []; + protected _subscriptionIds: Array> = []; constructor (fns: Array, 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 => { 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 { + 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); + }) + ); + } } diff --git a/packages/api/src/promise/Combinators.spec.ts b/packages/api/src/promise/Combinators.spec.ts index 740a6d3718..92538ed5a6 100644 --- a/packages/api/src/promise/Combinators.spec.ts +++ b/packages/api/src/promise/Combinators.spec.ts @@ -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) => { 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) => { 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) => { 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) => { + // ignore + }); + + combinator + .unsubscribe() + .then(() => { + // ignore + }) + .catch(() => { + // ignore + }); + }); });