Skip to content

Commit

Permalink
refactor: instructions concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Jul 5, 2024
1 parent 942348d commit 31d82b2
Showing 1 changed file with 48 additions and 53 deletions.
101 changes: 48 additions & 53 deletions packages/core/src/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,11 @@ export default class Query<
TPallet extends keyof TypedApi<TDescriptor>["constants"],
TConstant extends keyof TypedApi<TDescriptor>["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<
Expand All @@ -245,15 +246,12 @@ export default class Query<
TypedApi<TDescriptor>["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<
Expand All @@ -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<
Expand All @@ -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<
Expand All @@ -319,15 +311,12 @@ export default class Query<
TypedApi<TDescriptor>["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<
Expand All @@ -340,16 +329,22 @@ export default class Query<
TypedApi<TDescriptor>["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<const TInstruction extends QueryInstruction>(
instruction: TInstruction,
) {
return new Query([...this.#instructions, instruction]) as Query<
[...TInstructions, TInstruction],
TDescriptor
>;
}
}

0 comments on commit 31d82b2

Please sign in to comment.