Update api.derive to use api.queryMulti (#911)
* session api.queryMulti * Make a single subscription to retrieve all session info * Adjustments * WIP * Seems mostly comprehensive, need to full test e2e * e2e tests for operations * Allow "create from empty" * skip * Remove comment * Remove ancient ?'s * Update types.ts
This commit is contained in:
+15
-12
@@ -495,9 +495,10 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
|
||||
private decorateMulti<C, S> (onCall: OnCallDefinition<C, S>): QueryableStorageMulti<C, S> {
|
||||
return ((calls: QueryableStorageMultiArgs<C, S>, callback?: CodecCallback): S => {
|
||||
const mapped = calls.map((arg: QueryableStorageMultiArg<C, S>): [QueryableStorageFunction<CodecResult, SubscriptionResult>, ...Array<CodecArg>] =>
|
||||
// the input is a QueryableStorageFunction, convert to StorageFunction
|
||||
Array.isArray(arg)
|
||||
? arg
|
||||
: [arg] as any
|
||||
? [arg[0].creator, ...arg.slice(1)]
|
||||
: [arg.creator] as any
|
||||
);
|
||||
|
||||
return onCall(
|
||||
@@ -543,7 +544,7 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
|
||||
}, {} as QueryableStorage<C, S>);
|
||||
}
|
||||
|
||||
private decorateStorageEntry<C, S> (method: StorageFunction, onCall: OnCallDefinition<C, S>): QueryableStorageFunction<C, S> {
|
||||
private decorateStorageEntry<C, S> (creator: StorageFunction, onCall: OnCallDefinition<C, S>): QueryableStorageFunction<C, S> {
|
||||
// These signatures are allowed and exposed here -
|
||||
// (arg?: CodecArg): CodecResult;
|
||||
// (arg: CodecArg, callback: CodecCallback): SubscriptionResult;
|
||||
@@ -557,13 +558,13 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
|
||||
params = args.slice(0, args.length - 1);
|
||||
}
|
||||
|
||||
if (method.headKey && params.length === 0) {
|
||||
return this.decorateStorageEntryLinked(method, onCall, callback);
|
||||
if (creator.headKey && params.length === 0) {
|
||||
return this.decorateStorageEntryLinked(creator, onCall, callback);
|
||||
}
|
||||
|
||||
return onCall(
|
||||
(arg: CodecArg) => this._rpcRx.state
|
||||
.subscribeStorage([[method, arg]])
|
||||
.subscribeStorage([[creator, arg]])
|
||||
.pipe(
|
||||
// state_storage returns an array of values, since we have just subscribed to
|
||||
// a single entry, we pull that from the array and return it as-is
|
||||
@@ -576,9 +577,11 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
|
||||
);
|
||||
}) as QueryableStorageFunction<C, S>;
|
||||
|
||||
decorated.creator = creator;
|
||||
|
||||
decorated.at = (hash: Hash | Uint8Array | string, arg?: CodecArg): C =>
|
||||
onCall(
|
||||
(arg: CodecArg) => this._rpcRx.state.getStorage([method, arg], hash),
|
||||
(arg: CodecArg) => this._rpcRx.state.getStorage([creator, arg], hash),
|
||||
[arg]
|
||||
) as C;
|
||||
|
||||
@@ -586,27 +589,27 @@ export default abstract class ApiBase<CodecResult, SubscriptionResult> implement
|
||||
// be converted from C to the actual result required
|
||||
decorated.hash = (arg?: CodecArg): HashResult<C, S> =>
|
||||
onCall(
|
||||
(arg: CodecArg) => this._rpcRx.state.getStorageHash([method, arg]),
|
||||
(arg: CodecArg) => this._rpcRx.state.getStorageHash([creator, arg]),
|
||||
[arg]
|
||||
) as unknown as HashResult<C, S>;
|
||||
|
||||
decorated.key = (arg?: CodecArg): string =>
|
||||
u8aToHex(compactStripLength(method(arg))[1]);
|
||||
u8aToHex(compactStripLength(creator(arg))[1]);
|
||||
|
||||
decorated.multi = (args: Array<CodecArg>, callback?: CodecCallback): S =>
|
||||
onCall(
|
||||
() => this._rpcRx.state.subscribeStorage(args.map((arg) => [method, arg])),
|
||||
() => this._rpcRx.state.subscribeStorage(args.map((arg) => [creator, arg])),
|
||||
[],
|
||||
callback
|
||||
) as unknown as S;
|
||||
|
||||
decorated.size = (arg?: CodecArg): U64Result<C, S> =>
|
||||
onCall(
|
||||
(arg: CodecArg) => this._rpcRx.state.getStorageSize([method, arg]),
|
||||
(arg: CodecArg) => this._rpcRx.state.getStorageSize([creator, arg]),
|
||||
[arg]
|
||||
) as unknown as U64Result<C, S>;
|
||||
|
||||
return this.decorateFunctionMeta(method, decorated) as QueryableStorageFunction<C, S>;
|
||||
return this.decorateFunctionMeta(creator, decorated) as QueryableStorageFunction<C, S>;
|
||||
}
|
||||
|
||||
private decorateStorageEntryLinked<C, S> (method: StorageFunction, onCall: OnCallDefinition<C, S>, callback: CodecCallback | undefined): C | S {
|
||||
|
||||
@@ -50,6 +50,7 @@ export type U64Result<CodecResult, SubscriptionResult> =
|
||||
export interface QueryableStorageFunctionBase<CodecResult, SubscriptionResult> extends StorageFunction {
|
||||
(arg?: CodecArg): CodecResult;
|
||||
at: (hash: Hash | Uint8Array | string, arg?: CodecArg) => CodecResult;
|
||||
creator: StorageFunction;
|
||||
hash: (arg?: CodecArg) => HashResult<CodecResult, SubscriptionResult>;
|
||||
key: (arg?: CodecArg) => string;
|
||||
multi: (args: Array<CodecArg>, callback?: CodecCallback) => SubscriptionResult;
|
||||
|
||||
@@ -98,13 +98,28 @@ describe.skip('e2e queries', () => {
|
||||
api.query.balances.freeBalance.multi([
|
||||
keyring.alice.address(),
|
||||
keyring.bob.address(),
|
||||
keyring.ferdie.address(),
|
||||
'5DTestUPts3kjeXSTMyerHihn1uwMfLj8vU8sqF7qYrFabHE'
|
||||
'5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y',
|
||||
keyring.ferdie.address()
|
||||
], (balances) => {
|
||||
console.error(balances);
|
||||
|
||||
expect(balances).toHaveLength(4);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('subscribes to multiple results (api.queryMulti)', (done) => {
|
||||
return api.queryMulti([
|
||||
[api.query.balances.freeBalance, keyring.alice.address()],
|
||||
[api.query.balances.freeBalance, keyring.bob.address()],
|
||||
[api.query.balances.freeBalance, '5FLSigC9HGRKVhB9FiEo4Y3koPsNmBmLJbpXg2mp1hXcS59Y'],
|
||||
[api.query.balances.freeBalance, keyring.ferdie.address()]
|
||||
], (balances) => {
|
||||
console.error(balances);
|
||||
|
||||
expect(balances).toHaveLength(4);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user