Skip to content

Commit

Permalink
Add CustomEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
lxsmnsyc committed Oct 9, 2023
1 parent 9d5f239 commit 0c13399
Show file tree
Hide file tree
Showing 14 changed files with 343 additions and 6 deletions.
1 change: 1 addition & 0 deletions docs/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ By default, all feature flags are enabled. The following are the feature flags a
| [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request) ||[^9][^13] |[^9][^13] |
| [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) ||[^9][^13] |[^9][^13] |
| [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event) |[^9] |[^9] |[^9] |
| [`CustomEvent`](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent) |[^9] |[^9] |[^9] |
| Cyclic references ||||
| Isomorphic references ||||

Expand Down
1 change: 1 addition & 0 deletions packages/seroval/src/core/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const enum SerovalNodeType {
Request = 35,
Response = 36,
Event = 37,
CustomEvent = 38,
}

export const enum SerovalObjectFlags {
Expand Down
11 changes: 11 additions & 0 deletions packages/seroval/src/core/constructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,14 @@ export function createEventOptions(
composed: current.composed,
};
}

export function createCustomEventOptions(
current: CustomEvent,
): CustomEventInit {
return {
detail: current.detail as unknown,
bubbles: current.bubbles,
cancelable: current.cancelable,
composed: current.composed,
};
}
31 changes: 30 additions & 1 deletion packages/seroval/src/core/cross/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type {
SerovalAggregateErrorNode,
SerovalArrayNode,
SerovalBoxedNode,
SerovalCustomEventNode,
SerovalErrorNode,
SerovalEventNode,
SerovalFormDataNode,
Expand Down Expand Up @@ -72,7 +73,12 @@ import {
} from '../base-primitives';
import { createURLNode, createURLSearchParamsNode } from '../web-api';
import promiseToResult from '../promise-to-result';
import { createEventOptions, createRequestOptions, createResponseOptions } from '../constructors';
import {
createCustomEventOptions,
createEventOptions,
createRequestOptions,
createResponseOptions,
} from '../constructors';

type ObjectLikeNode =
| SerovalObjectNode
Expand Down Expand Up @@ -531,6 +537,27 @@ async function generateEventNode(
};
}

async function generateCustomEventNode(
ctx: CrossParserContext,
id: number,
current: CustomEvent,
): Promise<SerovalCustomEventNode> {
return {
t: SerovalNodeType.CustomEvent,
i: id,
s: serializeString(current.type),
l: undefined,
c: undefined,
m: undefined,
p: undefined,
e: undefined,
a: undefined,
f: await parseObject(ctx, createCustomEventOptions(current)),
b: undefined,
o: undefined,
};
}

async function parseObject(
ctx: CrossParserContext,
current: object | null,
Expand Down Expand Up @@ -655,6 +682,8 @@ async function parseObject(
return generateResponseNode(ctx, id, current as unknown as Response);
case Event:
return generateEventNode(ctx, id, current as unknown as Event);
case CustomEvent:
return generateCustomEventNode(ctx, id, current as unknown as CustomEvent);
default:
break;
}
Expand Down
13 changes: 13 additions & 0 deletions packages/seroval/src/core/cross/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import type {
SerovalRequestNode,
SerovalResponseNode,
SerovalEventNode,
SerovalCustomEventNode,
} from '../types';
import {
SerovalObjectRecordSpecialKey,
Expand Down Expand Up @@ -797,6 +798,16 @@ function serializeEvent(
);
}

function serializeCustomEvent(
ctx: CrossSerializerContext,
node: SerovalCustomEventNode,
): string {
return assignIndexedValue(
node.i,
'new CustomEvent("' + node.s + '",' + crossSerializeTree(ctx, node.f) + ')',
);
}

export default function crossSerializeTree(
ctx: CrossSerializerContext,
node: SerovalNode,
Expand Down Expand Up @@ -877,6 +888,8 @@ export default function crossSerializeTree(
return serializeResponse(ctx, node);
case SerovalNodeType.Event:
return serializeEvent(ctx, node);
case SerovalNodeType.CustomEvent:
return serializeCustomEvent(ctx, node);
default:
throw new Error('invariant');
}
Expand Down
31 changes: 30 additions & 1 deletion packages/seroval/src/core/cross/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import type {
SerovalRequestNode,
SerovalResponseNode,
SerovalEventNode,
SerovalCustomEventNode,
} from '../types';
import {
SerovalObjectRecordSpecialKey,
Expand All @@ -72,7 +73,12 @@ import {
createStringNode,
} from '../base-primitives';
import { createURLNode, createURLSearchParamsNode } from '../web-api';
import { createEventOptions, createRequestOptions, createResponseOptions } from '../constructors';
import {
createCustomEventOptions,
createEventOptions,
createRequestOptions,
createResponseOptions,
} from '../constructors';

type ObjectLikeNode =
| SerovalObjectNode
Expand Down Expand Up @@ -659,6 +665,27 @@ function generateEventNode(
};
}

function generateCustomEventNode(
ctx: StreamingCrossParserContext,
id: number,
current: CustomEvent,
): SerovalCustomEventNode {
return {
t: SerovalNodeType.CustomEvent,
i: id,
s: serializeString(current.type),
l: undefined,
c: undefined,
m: undefined,
p: undefined,
e: undefined,
a: undefined,
f: parseObject(ctx, createCustomEventOptions(current)),
b: undefined,
o: undefined,
};
}

function parseObject(
ctx: StreamingCrossParserContext,
current: object | null,
Expand Down Expand Up @@ -776,6 +803,8 @@ function parseObject(
return generateResponseNode(ctx, id, current as unknown as Response);
case Event:
return generateEventNode(ctx, id, current as unknown as Event);
case CustomEvent:
return generateCustomEventNode(ctx, id, current as unknown as CustomEvent);
default:
break;
}
Expand Down
26 changes: 25 additions & 1 deletion packages/seroval/src/core/cross/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type {
SerovalAggregateErrorNode,
SerovalArrayNode,
SerovalBoxedNode,
SerovalCustomEventNode,
SerovalErrorNode,
SerovalEventNode,
SerovalFormDataNode,
Expand Down Expand Up @@ -58,7 +59,7 @@ import {
createStringNode,
} from '../base-primitives';
import { createURLNode, createURLSearchParamsNode } from '../web-api';
import { createEventOptions } from '../constructors';
import { createCustomEventOptions, createEventOptions } from '../constructors';

type ObjectLikeNode = SerovalObjectNode | SerovalNullConstructorNode;

Expand Down Expand Up @@ -434,6 +435,27 @@ function generateEventNode(ctx: CrossParserContext, id: number, current: Event):
};
}

function generateCustomEventNode(
ctx: CrossParserContext,
id: number,
current: CustomEvent,
): SerovalCustomEventNode {
return {
t: SerovalNodeType.CustomEvent,
i: id,
s: serializeString(current.type),
l: undefined,
c: undefined,
m: undefined,
p: undefined,
e: undefined,
a: undefined,
f: parseObject(ctx, createCustomEventOptions(current)),
b: undefined,
o: undefined,
};
}

function parseObject(
ctx: CrossParserContext,
current: object | null,
Expand Down Expand Up @@ -544,6 +566,8 @@ function parseObject(
return generateFormDataNode(ctx, id, current as unknown as FormData);
case Event:
return generateEventNode(ctx, id, current as unknown as Event);
case CustomEvent:
return generateCustomEventNode(ctx, id, current as unknown as CustomEvent);
default:
break;
}
Expand Down
31 changes: 30 additions & 1 deletion packages/seroval/src/core/tree/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import type {
SerovalAggregateErrorNode,
SerovalArrayNode,
SerovalBoxedNode,
SerovalCustomEventNode,
SerovalErrorNode,
SerovalEventNode,
SerovalFormDataNode,
Expand Down Expand Up @@ -72,7 +73,12 @@ import {
} from '../base-primitives';
import { createURLNode, createURLSearchParamsNode } from '../web-api';
import promiseToResult from '../promise-to-result';
import { createEventOptions, createRequestOptions, createResponseOptions } from '../constructors';
import {
createCustomEventOptions,
createEventOptions,
createRequestOptions,
createResponseOptions,
} from '../constructors';

type ObjectLikeNode =
| SerovalObjectNode
Expand Down Expand Up @@ -532,6 +538,27 @@ async function generateEventNode(
};
}

async function generateCustomEventNode(
ctx: ParserContext,
id: number,
current: CustomEvent,
): Promise<SerovalCustomEventNode> {
return {
t: SerovalNodeType.CustomEvent,
i: id,
s: serializeString(current.type),
l: undefined,
c: undefined,
m: undefined,
p: undefined,
e: undefined,
a: undefined,
f: await parseObject(ctx, createCustomEventOptions(current)),
b: undefined,
o: undefined,
};
}

async function parseObject(
ctx: ParserContext,
current: object | null,
Expand Down Expand Up @@ -654,6 +681,8 @@ async function parseObject(
return generateResponseNode(ctx, id, current as unknown as Response);
case Event:
return generateEventNode(ctx, id, current as unknown as Event);
case CustomEvent:
return generateCustomEventNode(ctx, id, current as unknown as CustomEvent);
default:
break;
}
Expand Down
17 changes: 17 additions & 0 deletions packages/seroval/src/core/tree/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
SerovalBigIntTypedArrayNode,
SerovalBlobNode,
SerovalBoxedNode,
SerovalCustomEventNode,
SerovalDataViewNode,
SerovalDateNode,
SerovalErrorNode,
Expand Down Expand Up @@ -425,6 +426,20 @@ function deserializeEvent(
);
}

function deserializeCustomEvent(
ctx: DeserializerContext,
node: SerovalCustomEventNode,
): CustomEvent {
return assignIndexedValue(
ctx,
node.i,
new CustomEvent(
deserializeString(node.s),
deserializeTree(ctx, node.f) as CustomEventInit,
),
);
}

export default function deserializeTree(
ctx: DeserializerContext,
node: SerovalNode,
Expand Down Expand Up @@ -490,6 +505,8 @@ export default function deserializeTree(
return deserializeResponse(ctx, node);
case SerovalNodeType.Event:
return deserializeEvent(ctx, node);
case SerovalNodeType.CustomEvent:
return deserializeCustomEvent(ctx, node);
default:
throw new Error('invariant');
}
Expand Down
14 changes: 14 additions & 0 deletions packages/seroval/src/core/tree/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import type {
SerovalRequestNode,
SerovalResponseNode,
SerovalEventNode,
SerovalCustomEventNode,
} from '../types';
import {
SerovalObjectRecordSpecialKey,
Expand Down Expand Up @@ -757,6 +758,17 @@ function serializeEvent(
);
}

function serializeCustomEvent(
ctx: SerializerContext,
node: SerovalCustomEventNode,
): string {
return assignIndexedValue(
ctx,
node.i,
'new CustomEvent("' + node.s + '",' + serializeTree(ctx, node.f) + ')',
);
}

export default function serializeTree(
ctx: SerializerContext,
node: SerovalNode,
Expand Down Expand Up @@ -823,6 +835,8 @@ export default function serializeTree(
return serializeResponse(ctx, node);
case SerovalNodeType.Event:
return serializeEvent(ctx, node);
case SerovalNodeType.CustomEvent:
return serializeCustomEvent(ctx, node);
default:
throw new Error('invariant');
}
Expand Down
Loading

0 comments on commit 0c13399

Please sign in to comment.