Skip to content

Commit

Permalink
update: make work with handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
VsevolodX committed Mar 14, 2024
1 parent cc6e802 commit 6c05d41
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 54 deletions.
2 changes: 1 addition & 1 deletion dist/other/jupyterlite/JupyterLiteSession.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ declare class JupyterLiteSession extends React.Component<JupyterLiteSessionProps
componentDidMount(): void;
componentWillUnmount(): void;
receiveMessage: (event: MessageEvent<JupyterliteMessageSchema>) => void;
sendMessage: (data: never, variableName: string) => void;
sendMessage: (data: any, variableName: string) => void;
render(): React.JSX.Element;
}
export default JupyterLiteSession;
23 changes: 14 additions & 9 deletions dist/other/jupyterlite/JupyterLiteSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@ class JupyterLiteSession extends React.Component {
if (event.origin !== new URL(this.props.originURL).origin)
return;
const message = event.data;
const applicableHandlers = this.props.handlers.filter(handlerConfig => handlerConfig.filter.keys.every(key => message.payload.hasOwnProperty(key)));
applicableHandlers.forEach(handlerConfig => {
const result = handlerConfig.handler(message.payload);
// TODO: solve ts-ignores
// If the handler returns a payload for sending message, use it
// @ts-ignore
if (result && 'variableName' in result) {
const handlerConfig = this.props.handlers.find(handler => {
return handler.type === message.type &&
handler.filter.keys.every(key => message.payload.hasOwnProperty(key));
});
if (handlerConfig) {
const { handler, filter, extraParameters } = handlerConfig;
handler(message.payload);
// TODO: make more generic
const requestData = message.payload.requestData;
const variableName = message.payload.variableName;
if (requestData && variableName) {
// @ts-ignore
this.sendMessage(result.data, result.variableName);
const data = handler(variableName)();
this.sendMessage(data, variableName);
}
});
}
};
this.sendMessage = (data, variableName) => {
const message = {
Expand Down
88 changes: 44 additions & 44 deletions src/other/jupyterlite/JupyterLiteSession.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,56 +33,56 @@ class JupyterLiteSession extends React.Component<JupyterLiteSessionProps> {
if (event.origin !== new URL(this.props.originURL).origin) return;
const message = event.data;

const applicableHandlers = this.props.handlers.filter(handlerConfig =>
handlerConfig.filter.keys.every(key => message.payload.hasOwnProperty(key))
);

applicableHandlers.forEach(handlerConfig => {
const result = handlerConfig.handler(message.payload);
const handlerConfig = this.props.handlers.find(handler => {
return handler.type === message.type &&
handler.filter.keys.every(key => message.payload.hasOwnProperty(key));
});

// TODO: solve ts-ignores
// If the handler returns a payload for sending message, use it
// @ts-ignore
if (result && 'variableName' in result) {
if (handlerConfig) {
const {handler, filter, extraParameters} = handlerConfig;
handler(message.payload);
// TODO: make more generic
const requestData = message.payload.requestData;
const variableName = message.payload.variableName;
if (requestData && variableName) {
// @ts-ignore
this.sendMessage(result.data, result.variableName);
const data = handler(variableName)();
this.sendMessage(data, variableName);
}
});
}
};

sendMessage = (data: never, variableName: string) => {
const message: JupyterliteMessageSchema = {
type: "from-host-to-iframe",
payload: {data, variableName},
};
const iframe = document.getElementById(this.props.frameId) as HTMLIFrameElement | null;
if (iframe && iframe.contentWindow) {
iframe.contentWindow.postMessage(message, this.props.originURL);
} else {
console.error("JupyterLite iframe not found");
}
};

render()
{
const {defaultNotebookPath, originURL, frameId} = this.props;
const src = defaultNotebookPath
? `${originURL}/lab/tree?path=${defaultNotebookPath}`
: `${originURL}/lab`;
return (
<iframe
name="jupyterlite"
title="JupyterLite"
id={frameId}
src={src}
sandbox="allow-scripts allow-same-origin allow-popups allow-forms allow-modals allow-top-navigation-by-user-activation allow-downloads"
width="100%"
height="100%"
/>
);
sendMessage = (data: any, variableName: string) => {
const message: JupyterliteMessageSchema = {
type: "from-host-to-iframe",
payload: {data, variableName},
};
const iframe = document.getElementById(this.props.frameId) as HTMLIFrameElement | null;
if (iframe && iframe.contentWindow) {
iframe.contentWindow.postMessage(message, this.props.originURL);
} else {
console.error("JupyterLite iframe not found");
}
};

render() {
const {defaultNotebookPath, originURL, frameId} = this.props;
const src = defaultNotebookPath
? `${originURL}/lab/tree?path=${defaultNotebookPath}`
: `${originURL}/lab`;
return (
<iframe
name="jupyterlite"
title="JupyterLite"
id={frameId}
src={src}
sandbox="allow-scripts allow-same-origin allow-popups allow-forms allow-modals allow-top-navigation-by-user-activation allow-downloads"
width="100%"
height="100%"
/>
);
}
}

export
default
JupyterLiteSession;
export default JupyterLiteSession;

0 comments on commit 6c05d41

Please sign in to comment.