Sub/unsub cleanups (#1535)

* Sub/unsub cleanups

* drr full idAndIndex

* Memo on nick call

* refCountDelay

* Remove scheduler param, always asap

* Flatten

* Test cleanups

* Make tests pass

* Remove logs

* Drop nick memo

* sourceConnection.unsubscribe

* Small cleanups
This commit is contained in:
Jaco Greeff
2019-11-01 14:17:47 +01:00
committed by GitHub
parent f41ad2eee7
commit dc105e6b31
42 changed files with 209 additions and 110 deletions
-32
View File
@@ -1,32 +0,0 @@
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { of, timer } from 'rxjs';
import { drr } from './drr';
describe('drr', (): void => {
it('should not fire twice the same value', (done): void => {
let count = 0;
of(1, 1).pipe(drr()).subscribe((): void => { ++count; });
setTimeout((): void => {
expect(count).toBe(1);
done();
}, 50);
});
it('should be a ReplaySubject(1)', (done): void => {
const obs = timer(0, 100).pipe(drr()); // Starts at 0, increments every 100ms
obs.subscribe(); // Fire the observable
// Subscribe another time after some time, i.e. after the observable has fired
setTimeout((): void => {
obs.subscribe((value): void => {
expect(value > 1).toBe(true);
done();
});
}, 500);
});
});
-30
View File
@@ -1,30 +0,0 @@
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { catchError, distinctUntilChanged, publishReplay, refCount } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { logger } from '@polkadot/util';
type DrrResult = <T> (source$: Observable<T>) => Observable<T>;
const l = logger('drr');
/**
* Shorthand for distinctUntilChanged(), publishReplay(1) and refCount().
*
* @ignore
*/
export const drr = (): DrrResult => <T> (source$: Observable<T>): Observable<T> =>
source$.pipe(
catchError((error): Observable<never> => {
l.error(error);
throw error;
}),
distinctUntilChanged((a: any, b: any): boolean =>
JSON.stringify({ value: a }) === JSON.stringify({ value: b })
),
publishReplay(1),
refCount()
);
+2
View File
@@ -2,4 +2,6 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
export { drr } from '@polkadot/rpc-core/rxjs';
export * from './approvalFlagsToBools';