Add storage subscription API endpoints (#126)
* Add storage subscriptions * StorageResult formatting * Decoding WIP * Storage subsciptions in place * Allow null decodes, ref https://github.com/polkadot-js/apps/issues/215 * e2e tests for subscribeStorage * Cache subscription results, return with changeset
This commit is contained in:
@@ -20,15 +20,10 @@ export default function observable (subName: string, name: string, section: ApiI
|
||||
from(
|
||||
section[name]
|
||||
.apply(null, params)
|
||||
.catch(() => {
|
||||
// FIXME: swallow error - mostly storage failures, undefined as result. Once nodes
|
||||
// rollout new versions, can display the error here again (and/or fail explicitly)
|
||||
.catch((error: Error) => {
|
||||
console.error(error);
|
||||
|
||||
throw error;
|
||||
})
|
||||
// HACK The hammer approach if nothing else works
|
||||
// .then((value: any) =>
|
||||
// isUndefined(value) || isNull(value)
|
||||
// ? undefined
|
||||
// : value
|
||||
// )
|
||||
);
|
||||
}
|
||||
|
||||
@@ -5,18 +5,33 @@
|
||||
import { ApiInterface$Section } from '@polkadot/api/types';
|
||||
|
||||
import { BehaviorSubject, Observable, Subscriber } from 'rxjs';
|
||||
import isUndefined from '@polkadot/util/is/undefined';
|
||||
|
||||
export default function subscription (name: string, params: Array<any>, section: ApiInterface$Section, unsubCallback?: () => void): BehaviorSubject<any> {
|
||||
const subject = new BehaviorSubject(undefined);
|
||||
|
||||
Observable
|
||||
.create((observer: Subscriber<any>): Function => {
|
||||
let cachedResult: any;
|
||||
|
||||
const callback = (error: Error | null, result: any) => {
|
||||
if (error) {
|
||||
return;
|
||||
}
|
||||
|
||||
observer.next(result);
|
||||
if (isUndefined(cachedResult) || !Array.isArray(cachedResult)) {
|
||||
cachedResult = result;
|
||||
} else {
|
||||
const resultArray = (result as Array<any>) || [];
|
||||
|
||||
cachedResult = cachedResult.map((cachedValue, index) =>
|
||||
isUndefined(resultArray[index])
|
||||
? cachedValue
|
||||
: resultArray[index]
|
||||
);
|
||||
}
|
||||
|
||||
observer.next(cachedResult);
|
||||
};
|
||||
|
||||
const fn = section[name];
|
||||
|
||||
Reference in New Issue
Block a user