Skip to content

Commit

Permalink
Fix async transform not awaiting promise
Browse files Browse the repository at this point in the history
  • Loading branch information
Haaxor1689 committed Dec 11, 2023
1 parent 8d6a3a1 commit 5d7a903
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@haaxor1689/nil",
"version": "0.1.5",
"version": "0.1.6",
"description": "TypeScript-first binary data parsing library with static type inference",
"author": "Maroš Beťko <[email protected]>",
"repository": {
Expand Down
30 changes: 18 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,20 @@ class NilEffects<
this._def.schema._encode(data, value, ctx);
}

async _afterDecode(value: Input, ctx?: ParseContext): Promise<Output> {
async _afterDecode(value: Input, ctx?: ParseContext) {
const { schema } = this._def;
return this._def.transform[0](await schema._afterDecode(value, ctx), ctx);
return await this._def.transform[0](
await schema._afterDecode(value, ctx),
ctx
);
}

async _beforeEncode(value: Output, ctx?: ParseContext): Promise<Input> {
async _beforeEncode(value: Output, ctx?: ParseContext) {
const { schema } = this._def;
return await schema._beforeEncode(this._def.transform[1](value, ctx), ctx);
return await schema._beforeEncode(
await this._def.transform[1](value, ctx),
ctx
);
}
}

Expand Down Expand Up @@ -485,30 +491,30 @@ class NilObject<
});
}

_afterDecode(value: Input, ctx?: ParseContext) {
async _afterDecode(value: Input, ctx?: ParseContext) {
const { shape } = this._def;
return mapValues(shape, (v, k) => {
return (await mapValues(shape, async (v, k) => {
const newCtx: ParseContext = {
// FIXME: Types
value: value as never,
path: [...(ctx?.path ?? []), k],
parent: ctx
};
return v._afterDecode(value[k as keyof Input], newCtx);
}) as Promise<Output>;
return await v._afterDecode(value[k as keyof Input], newCtx);
})) as Output;
}

_beforeEncode(value: Output, ctx?: ParseContext) {
async _beforeEncode(value: Output, ctx?: ParseContext) {
const { shape } = this._def;
return mapValues(shape, (v, k) => {
return (await mapValues(shape, async (v, k) => {
const newCtx: ParseContext = {
// FIXME: Types
value: value as never,
path: [...(ctx?.path ?? []), k],
parent: ctx
};
return v._beforeEncode(value[k as keyof Output], newCtx);
}) as Promise<Input>;
return await v._beforeEncode(value[k as keyof Output], newCtx);
})) as Input;
}
}

Expand Down

0 comments on commit 5d7a903

Please sign in to comment.