From 31d82b2b01d981d0e742ba90854ac7cd5e3467ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ti=E1=BA=BFn=20Nguy=E1=BB=85n=20Kh=E1=BA=AFc?= Date: Fri, 5 Jul 2024 15:27:15 +1200 Subject: [PATCH] refactor: instructions concatenation --- packages/core/src/QueryBuilder.ts | 101 ++++++++++++++---------------- 1 file changed, 48 insertions(+), 53 deletions(-) diff --git a/packages/core/src/QueryBuilder.ts b/packages/core/src/QueryBuilder.ts index a507e098..ccb89b93 100644 --- a/packages/core/src/QueryBuilder.ts +++ b/packages/core/src/QueryBuilder.ts @@ -229,10 +229,11 @@ export default class Query< TPallet extends keyof TypedApi["constants"], TConstant extends keyof TypedApi["constants"][TPallet], >(pallet: TPallet, constant: TConstant) { - return new Query([ - ...this.#instructions, - { instruction: "fetch-constant", pallet, constant }, - ]); + return this.#append({ + instruction: "fetch-constant", + pallet, + constant, + }); } readStorage< @@ -245,15 +246,12 @@ export default class Query< TypedApi["query"][TPallet][TStorage] >["options"], >(pallet: TPallet, storage: TStorage, args: TArguments, options?: TOptions) { - return new Query([ - ...this.#instructions, - { - instruction: "read-storage", - pallet, - storage, - args: options === undefined ? args : [...args, options], - }, - ]); + return this.#append({ + instruction: "read-storage", + pallet, + storage, + args: options === undefined ? args : [...args, options], + }); } readStorages< @@ -271,17 +269,14 @@ export default class Query< args: TArguments[], options?: TOptions, ) { - return new Query([ - ...this.#instructions, - { - instruction: "read-storage", - pallet, - storage, - args: - options === undefined ? args : args.map((args) => [...args, options]), - multi: true, - }, - ]); + return this.#append({ + instruction: "read-storage", + pallet, + storage, + args: + options === undefined ? args : args.map((args) => [...args, options]), + multi: true, + }); } readStorageEntries< @@ -298,15 +293,12 @@ export default class Query< >["options"] >, >(pallet: TPallet, storage: TStorage, args: TArguments, options?: TOptions) { - return new Query([ - ...this.#instructions, - { - instruction: "read-storage-entries", - pallet, - storage, - args: options === undefined ? args : [...args, options], - }, - ]); + return this.#append({ + instruction: "read-storage-entries", + pallet, + storage, + args: options === undefined ? args : [...args, options], + }); } callApi< @@ -319,15 +311,12 @@ export default class Query< TypedApi["apis"][TPallet][TApi] >["options"], >(pallet: TPallet, api: TApi, args: TArguments, options?: TOptions) { - return new Query([ - ...this.#instructions, - { - instruction: "call-api", - pallet, - api, - args: options === undefined ? args : [...args, options], - }, - ]); + return this.#append({ + instruction: "call-api", + pallet, + api, + args: options === undefined ? args : [...args, options], + }); } callApis< @@ -340,16 +329,22 @@ export default class Query< TypedApi["apis"][TPallet][TApi] >["options"], >(pallet: TPallet, api: TApi, args: TArguments[], options?: TOptions) { - return new Query([ - ...this.#instructions, - { - instruction: "call-api", - pallet, - api, - args: - options === undefined ? args : args.map((args) => [...args, options]), - multi: true, - }, - ]); + return this.#append({ + instruction: "call-api", + pallet, + api, + args: + options === undefined ? args : args.map((args) => [...args, options]), + multi: true, + }); + } + + #append( + instruction: TInstruction, + ) { + return new Query([...this.#instructions, instruction]) as Query< + [...TInstructions, TInstruction], + TDescriptor + >; } }