Skip to content

Commit

Permalink
Merge pull request #137 from near/feat/messaging-closure
Browse files Browse the repository at this point in the history
feat: messaging closure
  • Loading branch information
andy-haynes authored Nov 3, 2023
2 parents 6b1a893 + b161f71 commit 6e24dfb
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 99 deletions.
1 change: 0 additions & 1 deletion packages/container/src/callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export function invokeComponentCallback({
callbacks,
componentId,
method: componentMethod,
postMessage,
requestId,
// TODO must specify a real value here
serializeArgs,
Expand Down
16 changes: 8 additions & 8 deletions packages/container/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ export function initContainer({
buildRequest,
buildSafeProxy,
buildUseComponentCallback,
composeMessagingMethods,
composeSerializationMethods,
dispatchRenderEvent,
invokeCallback,
invokeComponentCallback,
isMatchingProps,
postCallbackInvocationMessage,
postCallbackResponseMessage,
postComponentRenderMessage,
postMessage,
preactify,
renderContainerComponent,
},
Expand All @@ -43,14 +40,19 @@ export function initContainer({
const callbacks: { [key: string]: Function } = {};
const requests: { [key: string]: CallbackRequest } = {};

const {
postCallbackInvocationMessage,
postCallbackResponseMessage,
postComponentRenderMessage,
} = composeMessagingMethods();

const { deserializeProps, serializeArgs, serializeNode, serializeProps } =
composeSerializationMethods({
buildRequest,
callbacks,
parentContainerId,
postCallbackInvocationMessage,
preactRootComponentName,
postMessage,
requests,
});

Expand All @@ -77,11 +79,10 @@ export function initContainer({
if (containerComponent && isRootComponent) {
dispatchRenderEvent({
callbacks,
componentId: componentId,
componentId,
node: containerComponent(),
nodeRenders,
postComponentRenderMessage,
postMessage,
preactRootComponentName,
serializeNode,
serializeProps,
Expand All @@ -101,7 +102,6 @@ export function initContainer({
parentContainerId,
postCallbackInvocationMessage,
postCallbackResponseMessage,
postMessage,
renderDom: (node: Node) => preactify({ node, createElement, Component }),
requests,
serializeArgs,
Expand Down
2 changes: 0 additions & 2 deletions packages/container/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ export function buildEventHandler({
parentContainerId,
postCallbackInvocationMessage,
postCallbackResponseMessage,
postMessage,
renderDom,
requests,
serializeArgs,
Expand Down Expand Up @@ -124,7 +123,6 @@ export function buildEventHandler({
postCallbackResponseMessage({
error,
componentId,
postMessage,
requestId,
result: value,
targetId: originator,
Expand Down
8 changes: 1 addition & 7 deletions packages/container/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ export { invokeCallback, invokeComponentCallback } from './callbacks';
export { initContainer } from './container';
export { buildEventHandler } from './events';
export { buildUseComponentCallback } from './hooks';
export {
buildRequest,
postMessage,
postCallbackInvocationMessage,
postCallbackResponseMessage,
postComponentRenderMessage,
} from './messaging';
export { buildRequest, composeMessagingMethods } from './messaging';
export {
buildSafeProxy,
dispatchRenderEvent,
Expand Down
103 changes: 54 additions & 49 deletions packages/container/src/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,65 +24,70 @@ export function buildRequest(): CallbackRequest {
};
}

export function postMessage<T extends PostMessageParams>(message: T) {
window.parent.postMessage(message, '*');
}
export function composeMessagingMethods() {
function postMessage<T extends PostMessageParams>(message: T) {
window.parent.postMessage(message, '*');
}

export function postCallbackInvocationMessage({
args,
callbacks,
componentId,
method,
postMessage,
requestId,
serializeArgs,
targetId,
}: PostMessageComponentCallbackInvocationParams): void {
postMessage<ComponentCallbackInvocation>({
args: serializeArgs({ args, callbacks, componentId }),
function postCallbackInvocationMessage({
args,
callbacks,
componentId,
method,
originator: componentId,
requestId,
serializeArgs,
targetId,
type: 'component.callbackInvocation',
});
}
}: PostMessageComponentCallbackInvocationParams): void {
postMessage<ComponentCallbackInvocation>({
args: serializeArgs({ args, callbacks, componentId }),
method,
originator: componentId,
requestId,
targetId,
type: 'component.callbackInvocation',
});
}

export function postCallbackResponseMessage({
error,
componentId,
postMessage,
requestId,
result,
targetId,
}: PostMessageComponentCallbackResponseParams): void {
const serializedError =
error && JSON.stringify(error, Object.getOwnPropertyNames(error));

postMessage<ComponentCallbackResponse>({
requestId,
function postCallbackResponseMessage({
error,
componentId,
result: JSON.stringify({
value: result,
error: serializedError,
}),
requestId,
result,
targetId,
type: 'component.callbackResponse',
});
}
}: PostMessageComponentCallbackResponseParams): void {
const serializedError =
error && JSON.stringify(error, Object.getOwnPropertyNames(error));

export function postComponentRenderMessage({
childComponents,
componentId,
node,
postMessage,
trust,
}: PostMessageComponentRenderParams): void {
postMessage<ComponentRender>({
postMessage<ComponentCallbackResponse>({
requestId,
componentId,
result: JSON.stringify({
value: result,
error: serializedError,
}),
targetId,
type: 'component.callbackResponse',
});
}

function postComponentRenderMessage({
childComponents,
componentId,
node,
trust,
type: 'component.render',
});
}: PostMessageComponentRenderParams): void {
postMessage<ComponentRender>({
childComponents,
componentId,
node,
trust,
type: 'component.render',
});
}

return {
postCallbackInvocationMessage,
postCallbackResponseMessage,
postComponentRenderMessage,
};
}
8 changes: 2 additions & 6 deletions packages/container/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ export const preactify: PreactifyCallback = ({
}

// TODO handle other builtins
const isComponent = !!props!.src?.match(
/[0-9a-z._-]{5,}\/[0-9a-z._-]+/gi
);
const isComponent = !!props!.src?.match(/[0-9a-z._-]{5,}\/[0-9a-z._-]+/gi);
const { children } = props;
if (!children) {
return undefined;
Expand Down Expand Up @@ -107,7 +105,6 @@ export const dispatchRenderEvent: DispatchRenderEventCallback = ({
node,
nodeRenders,
postComponentRenderMessage,
postMessage,
serializeNode,
trust,
}) => {
Expand Down Expand Up @@ -160,10 +157,9 @@ export const dispatchRenderEvent: DispatchRenderEventCallback = ({

try {
postComponentRenderMessage({
childComponents,
childComponents: childComponents || [],
componentId: componentId,
node: serializedNode,
postMessage,
trust,
});
} catch (error) {
Expand Down
3 changes: 0 additions & 3 deletions packages/container/src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ interface SerializeChildComponentParams {
* @param parentContainerId ID of the parent container
* @param postCallbackInvocationMessage Request invocation on external Component via window.postMessage
* @param preactRootComponentName The name of the root/Fragment Preact function
* @param postMessage Method for calling postMessage on the parent window
* @param requests Set of current callback requests
*/
export const composeSerializationMethods: ComposeSerializationMethodsCallback =
Expand All @@ -36,7 +35,6 @@ export const composeSerializationMethods: ComposeSerializationMethodsCallback =
parentContainerId,
postCallbackInvocationMessage,
preactRootComponentName,
postMessage,
requests,
}) => {
/**
Expand Down Expand Up @@ -185,7 +183,6 @@ export const composeSerializationMethods: ComposeSerializationMethodsCallback =
callbacks,
componentId,
method: __componentMethod, // the key on the props object passed to this Component
postMessage,
requestId,
serializeArgs,
targetId: parentContainerId,
Expand Down
27 changes: 13 additions & 14 deletions packages/container/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ export interface PostMessageParams {
type: EventType;
}

export type PostMessageCallback = <T extends PostMessageParams>(
message: T
) => void;

export type PostMessageComponentInvocationCallback = (
message: PostMessageComponentCallbackInvocationParams
) => void;
Expand All @@ -111,7 +107,6 @@ export interface PostMessageComponentCallbackInvocationParams {
args: any[];
callbacks: CallbackMap;
method: string;
postMessage: PostMessageCallback;
requestId: string;
serializeArgs: SerializeArgsCallback;
targetId: string;
Expand All @@ -131,12 +126,14 @@ export interface ComponentCallbackResponse extends PostMessageParams {
export interface PostMessageComponentCallbackResponseParams {
componentId: string;
error: Error | null;
postMessage: PostMessageCallback;
requestId: string;
result: any;
targetId: string;
}

export type PostMessageComponentRenderCallback = (
message: PostMessageComponentRenderParams
) => void;
export interface ComponentRender extends PostMessageParams {
childComponents: ComponentChildMetadata[];
componentId: string;
Expand All @@ -148,7 +145,6 @@ export interface PostMessageComponentRenderParams {
childComponents: ComponentChildMetadata[];
componentId: string;
node: SerializedNode;
postMessage: PostMessageCallback;
trust: ComponentTrust;
}

Expand All @@ -174,7 +170,6 @@ export interface ComposeSerializationMethodsParams {
parentContainerId: string | null;
postCallbackInvocationMessage: PostMessageComponentInvocationCallback;
preactRootComponentName: string;
postMessage: PostMessageCallback;
requests: RequestMap;
}

Expand All @@ -187,6 +182,12 @@ export type ComposeSerializationMethodsCallback = (
serializeProps: SerializePropsCallback;
};

export type ComposeMessagingMethodsCallback = () => {
postCallbackInvocationMessage: PostMessageComponentInvocationCallback;
postCallbackResponseMessage: PostMessageComponentResponseCallback;
postComponentRenderMessage: PostMessageComponentRenderCallback;
};

export type UpdateContainerPropsCallback = (props: Props) => void;

export interface ProcessEventParams {
Expand All @@ -199,7 +200,6 @@ export interface ProcessEventParams {
parentContainerId: string | null;
postCallbackInvocationMessage: PostMessageComponentInvocationCallback;
postCallbackResponseMessage: PostMessageComponentResponseCallback;
postMessage: PostMessageCallback;
renderDom: (node: any) => object;
requests: RequestMap;
serializeArgs: SerializeArgsCallback;
Expand All @@ -213,6 +213,7 @@ export interface InitContainerParams {
buildRequest: BuildRequestCallback;
buildSafeProxy: BuildSafeProxyCallback;
buildUseComponentCallback: BuildUseComponentCallback;
composeMessagingMethods: ComposeMessagingMethodsCallback;
composeSerializationMethods: ComposeSerializationMethodsCallback;
dispatchRenderEvent: DispatchRenderEventCallback;
invokeCallback: (args: InvokeCallbackParams) => any;
Expand All @@ -221,7 +222,6 @@ export interface InitContainerParams {
postCallbackInvocationMessage: PostMessageComponentInvocationCallback;
postCallbackResponseMessage: PostMessageComponentResponseCallback;
postComponentRenderMessage: (p: any) => void;
postMessage: PostMessageCallback;
preactify: PreactifyCallback;
renderContainerComponent: RenderContainerComponentCallback;
};
Expand All @@ -236,7 +236,7 @@ export interface InitContainerParams {
preactRootComponentName: string;
props: any;
render: PreactRender;
trust: string;
trust: ComponentTrust;
updateContainerProps: UpdateContainerPropsCallback;
};
}
Expand Down Expand Up @@ -332,12 +332,11 @@ export interface DispatchRenderEventParams {
componentId: string;
node: Node;
nodeRenders: Map<string, string>;
postComponentRenderMessage: (p: any) => void;
postMessage: PostMessageCallback;
postComponentRenderMessage: PostMessageComponentRenderCallback;
preactRootComponentName: string;
serializeNode: (p: SerializeNodeParams) => SerializedNode;
serializeProps: SerializePropsCallback;
trust: string;
trust: ComponentTrust;
}
export type DispatchRenderEventCallback = (
params: DispatchRenderEventParams
Expand Down
Loading

0 comments on commit 6e24dfb

Please sign in to comment.