Skip to content

Commit

Permalink
Add async mode
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Jan 26, 2025
1 parent be53f18 commit 28eece6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 14 additions & 2 deletions packages/seroval/src/core/context/parser/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ import type {
BigIntTypedArrayValue,
TypedArrayValue,
} from '../../utils/typed-array';
import { BaseParserContext } from '../parser';
import { BaseParserContext, ParserNodeType } from '../parser';

type ObjectLikeNode =
| SerovalObjectNode
Expand Down Expand Up @@ -480,6 +480,18 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
throw new SerovalUnsupportedTypeError(current);
}

protected async parseFunction(current: unknown): Promise<SerovalNode> {
const ref = this.getReference(current);
if (ref.type !== ParserNodeType.Fresh) {
return ref.value;
}
const plugin = await this.parsePlugin(ref.value, current);
if (plugin) {
return plugin;
}
throw new SerovalUnsupportedTypeError(current);
}

async parse<T>(current: T): Promise<SerovalNode> {
try {
switch (typeof current) {
Expand All @@ -505,7 +517,7 @@ export default abstract class BaseAsyncParserContext extends BaseParserContext {
case 'symbol':
return this.parseWellKnownSymbol(current);
case 'function':
return this.parseFunction(current as (...args: unknown[]) => unknown);
return this.parseFunction(current);
default:
throw new SerovalUnsupportedTypeError(current);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/seroval/src/core/context/parser/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export default abstract class BaseSyncParserContext extends BaseParserContext {
case 'symbol':
return this.parseWellKnownSymbol(current);
case 'function': {
return this.parseFunction(current as unknown);
return this.parseFunction(current);
}
default:
throw new SerovalUnsupportedTypeError(current);
Expand Down

0 comments on commit 28eece6

Please sign in to comment.