Skip to content

Commit

Permalink
chore: update type and parameter names to reflect callbacks as a cont…
Browse files Browse the repository at this point in the history
…ainer-level concern
  • Loading branch information
andy-haynes committed Jan 3, 2024
1 parent bfb8865 commit b4d8ab6
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion apps/web/components/ComponentMonitor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export function ComponentMonitor({
badgeClass: 'bg-success',
name: 'return',
componentId: parseComponentId(
isFromComponent ? message.componentId : message.targetId
isFromComponent ? message.containerId : message.targetId
)!,
summary: `[${requestId.split('-')[0]}] returned ${result} ${
!isFromComponent ? 'to' : ''
Expand Down
4 changes: 2 additions & 2 deletions packages/application/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ export function onCallbackResponse({
a component has executed a callback invoked from another component
return the value of the callback execution to the calling component
*/
const { requestId, result, targetId, componentId } = data;
const { requestId, result, targetId, containerId } = data;
sendMessage({
componentId: targetId,
message: {
componentId,
containerId,
result,
requestId,
targetId,
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/types/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface ComponentCallbackInvocation extends PostMessageParams {
}

export interface ComponentCallbackResponse extends PostMessageParams {
componentId: string;
containerId: string;
requestId: string;
result: string; // stringified JSON in the form of { result: any, error: string }
targetId: string;
Expand Down
6 changes: 3 additions & 3 deletions packages/container/src/callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function invokeComponentCallback({
args,
buildRequest,
callbacks,
componentId,
containerId,
invokeCallback,
method,
postCallbackInvocationMessage,
Expand All @@ -49,7 +49,7 @@ export function invokeComponentCallback({
}: InvokeComponentCallbackParams): any {
// unknown method
if (!callbacks[method]) {
console.error(`No method ${method} on component ${componentId}`);
console.error(`No method ${method} on container ${containerId}`);
return null;
}

Expand All @@ -72,7 +72,7 @@ export function invokeComponentCallback({
postCallbackInvocationMessage({
args: childArgs,
callbacks,
componentId,
containerId,
method: callbackIdentifier,
requestId,
serializeArgs,
Expand Down
4 changes: 2 additions & 2 deletions packages/container/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function initContainer({
const processEvent = buildEventHandler({
buildRequest,
callbacks,
componentId,
containerId: componentId,
deserializeArgs,
deserializeProps,
invokeCallback,
Expand Down Expand Up @@ -97,7 +97,7 @@ export function initContainer({
const props = buildSafeProxy({
componentId,
props: deserializeProps({
componentId,
containerId: componentId,
props: componentPropsJson,
}),
});
Expand Down
12 changes: 6 additions & 6 deletions packages/container/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ProcessEventParams } from './types';
* Return an event handler function to be registered under `window.addEventHandler('message', fn(event))`
* @param buildRequest Function to build an inter-Component asynchronous callback request
* @param callbacks The set of callbacks defined on the target Component
* @param componentId ID of the target Component on which the
* @param containerId ID of the container handling messages
* @param deserializeProps Function to deserialize props passed on the event
* @param invokeCallback Function to execute the specified function in the current context
* @param invokeComponentCallback Function to execute the specified function, either in the current context or another Component's
Expand All @@ -20,7 +20,7 @@ import type { ProcessEventParams } from './types';
export function buildEventHandler({
buildRequest,
callbacks,
componentId,
containerId,
deserializeArgs,
deserializeProps,
invokeCallback,
Expand All @@ -43,12 +43,12 @@ export function buildEventHandler({
args: SerializedArgs;
method: string;
}) {
const deserializedArgs = deserializeArgs({ args, componentId });
const deserializedArgs = deserializeArgs({ args, containerId });
return invokeComponentCallback({
args: deserializedArgs,
buildRequest,
callbacks,
componentId,
containerId,
invokeCallback,
method,
postCallbackInvocationMessage,
Expand Down Expand Up @@ -111,7 +111,7 @@ export function buildEventHandler({
if (requestId) {
postCallbackResponseMessage({
error,
componentId,
containerId,
requestId,
result: value,
targetId: originator,
Expand Down Expand Up @@ -182,7 +182,7 @@ export function buildEventHandler({
case 'component.update': {
updateProps(
deserializeProps({
componentId,
containerId,
props: event.data.props,
})
);
Expand Down
10 changes: 5 additions & 5 deletions packages/container/src/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ export function composeMessagingMethods() {
function postCallbackInvocationMessage({
args,
callbacks,
componentId,
containerId,
method,
requestId,
serializeArgs,
targetId,
}: PostMessageComponentCallbackInvocationParams): void {
postMessage<ComponentCallbackInvocation>({
args: serializeArgs({ args, callbacks, componentId }),
args: serializeArgs({ args, callbacks, containerId }),
method,
originator: componentId,
originator: containerId,
requestId,
targetId,
type: 'component.callbackInvocation',
Expand All @@ -53,7 +53,7 @@ export function composeMessagingMethods() {

function postCallbackResponseMessage({
error,
componentId,
containerId,
requestId,
result,
targetId,
Expand All @@ -63,7 +63,7 @@ export function composeMessagingMethods() {

postMessage<ComponentCallbackResponse>({
requestId,
componentId,
containerId,
result: JSON.stringify({
value: result,
error: serializedError,
Expand Down
20 changes: 10 additions & 10 deletions packages/container/src/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,13 +188,13 @@ export const composeSerializationMethods: ComposeSerializationMethodsCallback =

const deserializeArgs: DeserializeArgsCallback = ({
args,
componentId,
containerId,
}) => {
return deepTransform({
value: args,
onSerializedCallback: (cb) => {
return deserializePropsCallback({
containerId: componentId,
containerId,
callbackIdentifier: cb.callbackIdentifier,
});
},
Expand All @@ -204,15 +204,15 @@ export const composeSerializationMethods: ComposeSerializationMethodsCallback =
const serializeArgs: SerializeArgsCallback = ({
args,
callbacks,
componentId,
containerId,
}) => {
return (args || []).map((arg) => {
if (!arg) {
return arg;
}

if (Array.isArray(arg)) {
return serializeArgs({ args: arg, callbacks, componentId });
return serializeArgs({ args: arg, callbacks, containerId });
}

if (typeof arg === 'object') {
Expand All @@ -221,7 +221,7 @@ export const composeSerializationMethods: ComposeSerializationMethodsCallback =
serializeArgs({
args: Object.values(arg),
callbacks,
componentId,
containerId,
}).map((value, i) => [argKeys[i], value])
);
}
Expand All @@ -233,13 +233,13 @@ export const composeSerializationMethods: ComposeSerializationMethodsCallback =
const fnKey = buildContainerMethodIdentifier({
callback: arg,
callbackName: arg?.name, // FIXME
containerId: componentId,
containerId,
});

callbacks[fnKey] = arg;
return {
callbackIdentifier: fnKey,
containerId: componentId,
containerId,
};
});
};
Expand All @@ -257,7 +257,7 @@ export const composeSerializationMethods: ComposeSerializationMethodsCallback =
postCallbackInvocationMessage({
args,
callbacks,
componentId: containerId,
containerId,
method: callbackIdentifier,
requestId,
serializeArgs,
Expand All @@ -269,7 +269,7 @@ export const composeSerializationMethods: ComposeSerializationMethodsCallback =
};

const deserializeProps: DeserializePropsCallback = ({
componentId,
containerId,
props,
}) => {
if (!props || Array.isArray(props) || typeof props !== 'object') {
Expand All @@ -280,7 +280,7 @@ export const composeSerializationMethods: ComposeSerializationMethodsCallback =
value: props,
onSerializedCallback: (cb) => {
return deserializePropsCallback({
containerId: componentId,
containerId,
callbackIdentifier: cb.callbackIdentifier,
});
},
Expand Down
14 changes: 7 additions & 7 deletions packages/container/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ export type CallbackMap = { [key: string]: Function };
export type DeserializeArgsCallback = (params: DeserializeArgsParams) => any;
export interface DeserializeArgsParams {
args: any;
componentId: string;
containerId: string;
}

export type DeserializePropsCallback = (params: DeserializePropsParams) => any;
export interface DeserializePropsParams {
componentId: string;
containerId: string;
props: Props;
}

Expand All @@ -41,7 +41,7 @@ export interface InvokeComponentCallbackParams {
args: SerializedArgs;
buildRequest: BuildRequestCallback;
callbacks: CallbackMap;
componentId: string;
containerId: string;
invokeCallback: (args: InvokeCallbackParams) => any;
method: string;
postCallbackInvocationMessage: PostMessageComponentInvocationCallback;
Expand All @@ -56,18 +56,18 @@ export type PostMessageComponentInvocationCallback = (
export interface PostMessageComponentCallbackInvocationParams {
args: any[];
callbacks: CallbackMap;
containerId: string;
method: string;
requestId: string;
serializeArgs: SerializeArgsCallback;
targetId: string;
componentId: string;
}

export type PostMessageComponentResponseCallback = (
message: PostMessageComponentCallbackResponseParams
) => void;
export interface PostMessageComponentCallbackResponseParams {
componentId: string;
containerId: string;
error: Error | null;
requestId: string;
result: any;
Expand Down Expand Up @@ -129,7 +129,7 @@ export type UpdateContainerPropsCallback = (props: Props) => void;
export interface ProcessEventParams {
buildRequest: BuildRequestCallback;
callbacks: CallbackMap;
componentId: string;
containerId: string;
deserializeArgs: DeserializeArgsCallback;
deserializeProps: DeserializePropsCallback;
invokeCallback: (args: InvokeCallbackParams) => any;
Expand Down Expand Up @@ -177,7 +177,7 @@ export type SerializeArgsCallback = (
export interface SerializeArgsParams {
args: any[];
callbacks: CallbackMap;
componentId: string;
containerId: string;
}

export interface PreactElement {
Expand Down

0 comments on commit b4d8ab6

Please sign in to comment.