Bubble isSubscription from rpc-rx (#627)
* Bubble isSubscription from rpc-rx * Fix logic error
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
import { RpcInterface$Method, RpcInterface$Section } from '@polkadot/rpc-core/types';
|
||||
import { ProviderInterface } from '@polkadot/rpc-provider/types';
|
||||
import { RpcRxInterface, RpcRxInterface$Events, RpcRxInterface$Section } from './types';
|
||||
import { RpcRxInterface, RpcRxInterface$Events, RpcRxInterface$Section, RpcRxObervable } from './types';
|
||||
|
||||
import EventEmitter from 'eventemitter3';
|
||||
import { BehaviorSubject, Observable, Subscriber, from } from 'rxjs';
|
||||
@@ -100,21 +100,27 @@ export default class RpcRx implements RpcRxInterface {
|
||||
}, ({} as RpcRxInterface$Section));
|
||||
}
|
||||
|
||||
private createObservable (subName: string, name: string, section: RpcInterface$Section): (...params: Array<any>) => Observable<any> {
|
||||
if (isFunction(section[name].unsubscribe)) {
|
||||
return this.createCachedObservable(subName, name, section);
|
||||
}
|
||||
private createObservable (subName: string, name: string, section: RpcInterface$Section): RpcRxObervable {
|
||||
const isSubscription = isFunction(section[name].unsubscribe);
|
||||
const fn = isSubscription
|
||||
? this.createCachedObservable(subName, name, section)
|
||||
: (...params: Array<any>): Observable<any> =>
|
||||
from(
|
||||
section[name]
|
||||
.apply(null, params)
|
||||
.catch((error: Error) => {
|
||||
console.error(error);
|
||||
})
|
||||
);
|
||||
|
||||
return (...params: Array<any>): Observable<any> =>
|
||||
from(
|
||||
section[name]
|
||||
.apply(null, params)
|
||||
.catch((error: Error) => {
|
||||
console.error(error);
|
||||
})
|
||||
);
|
||||
const result = fn as RpcRxObervable;
|
||||
|
||||
result.isSubscription = isSubscription;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// FIXME Memoization as in derive, https://github.com/polkadot-js/api/issues/553
|
||||
private createCachedObservable (subName: string, name: string, section: RpcInterface$Section): (...params: Array<any>) => Observable<any> {
|
||||
if (!this._cacheMap[subName]) {
|
||||
this._cacheMap[subName] = {};
|
||||
|
||||
@@ -5,10 +5,13 @@
|
||||
import { ReplaySubject, Observable } from 'rxjs';
|
||||
import { ProviderInterface$Emitted } from '@polkadot/rpc-provider/types';
|
||||
|
||||
export type RpcRxInterface$Method = (...params: Array<any>) => Observable<any> | ReplaySubject<any>;
|
||||
export interface RpcRxObervable {
|
||||
(...params: Array<any>): Observable<any> | ReplaySubject<any>;
|
||||
isSubscription: boolean;
|
||||
}
|
||||
|
||||
export type RpcRxInterface$Section = {
|
||||
[index: string]: RpcRxInterface$Method
|
||||
[index: string]: RpcRxObervable
|
||||
};
|
||||
|
||||
export type RpcRxInterface$Events = ProviderInterface$Emitted;
|
||||
|
||||
Reference in New Issue
Block a user