From 6c05d41a2f89900d488cbd15278134fc1ac0571d Mon Sep 17 00:00:00 2001 From: VsevolodX <79542055+VsevolodX@users.noreply.github.com> Date: Thu, 14 Mar 2024 10:18:06 -0700 Subject: [PATCH] update: make work with handlers --- .../other/jupyterlite/JupyterLiteSession.d.ts | 2 +- dist/other/jupyterlite/JupyterLiteSession.js | 23 +++-- src/other/jupyterlite/JupyterLiteSession.tsx | 88 +++++++++---------- 3 files changed, 59 insertions(+), 54 deletions(-) diff --git a/dist/other/jupyterlite/JupyterLiteSession.d.ts b/dist/other/jupyterlite/JupyterLiteSession.d.ts index e7a7f56..ca002f4 100644 --- a/dist/other/jupyterlite/JupyterLiteSession.d.ts +++ b/dist/other/jupyterlite/JupyterLiteSession.d.ts @@ -18,7 +18,7 @@ declare class JupyterLiteSession extends React.Component) => void; - sendMessage: (data: never, variableName: string) => void; + sendMessage: (data: any, variableName: string) => void; render(): React.JSX.Element; } export default JupyterLiteSession; diff --git a/dist/other/jupyterlite/JupyterLiteSession.js b/dist/other/jupyterlite/JupyterLiteSession.js index e7d6eb6..289b160 100644 --- a/dist/other/jupyterlite/JupyterLiteSession.js +++ b/dist/other/jupyterlite/JupyterLiteSession.js @@ -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 = { diff --git a/src/other/jupyterlite/JupyterLiteSession.tsx b/src/other/jupyterlite/JupyterLiteSession.tsx index 530b86c..2c6d8e1 100644 --- a/src/other/jupyterlite/JupyterLiteSession.tsx +++ b/src/other/jupyterlite/JupyterLiteSession.tsx @@ -33,56 +33,56 @@ 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); + 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 ( -