generated from Exabyte-io/template-definitions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
72 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,15 @@ | ||
import { JupyterliteMessageSchema } from "@mat3ra/esse/lib/js/types"; | ||
import React from "react"; | ||
import MessageHandler from "./MessageHandler"; | ||
interface JupyterLiteSessionProps { | ||
originURL: string; | ||
defaultNotebookPath?: string; | ||
frameId: string; | ||
handlers: { | ||
type: string; | ||
filter: { | ||
keys: string[]; | ||
}; | ||
extraParameters: any[]; | ||
handler: (args: any) => void | any; | ||
}[]; | ||
messageHandler?: MessageHandler; | ||
} | ||
declare class JupyterLiteSession extends React.Component<JupyterLiteSessionProps> { | ||
static defaultProps: Partial<JupyterLiteSessionProps>; | ||
componentDidMount(): void; | ||
componentWillUnmount(): void; | ||
receiveMessage: (event: MessageEvent<JupyterliteMessageSchema>) => void; | ||
sendMessage: (data: any, variableName: string) => void; | ||
render(): React.JSX.Element; | ||
} | ||
export default JupyterLiteSession; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type HandlerFunction = (data?: any, variableName?: string) => void; | ||
declare class MessageHandler { | ||
private handlers; | ||
private originURL; | ||
init(originURL: string): void; | ||
destroy(): void; | ||
addHandlers(action: string, handlers: HandlerFunction[]): void; | ||
private receiveMessage; | ||
sendData(data: any, variableName?: string): void; | ||
} | ||
export default MessageHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
class MessageHandler { | ||
constructor() { | ||
this.handlers = {}; | ||
this.originURL = "*"; | ||
this.receiveMessage = (event) => { | ||
if (this.originURL !== "*" && event.origin !== this.originURL) { | ||
return; | ||
} | ||
if (event.data.type === "from-iframe-to-host") { | ||
const { action } = event.data.payload; | ||
// TODO: make action required in ESSE | ||
// @ts-ignore | ||
if (this.handlers[action]) { | ||
// @ts-ignore | ||
this.handlers[action].forEach((handler) => { | ||
handler(event.data.payload.data, event.data.payload.variableName); | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
init(originURL) { | ||
window.addEventListener("message", this.receiveMessage); | ||
this.originURL = originURL; | ||
} | ||
destroy() { | ||
window.removeEventListener("message", this.receiveMessage); | ||
} | ||
addHandlers(action, handlers) { | ||
if (!this.handlers[action]) { | ||
this.handlers[action] = []; | ||
} | ||
this.handlers[action].push(...handlers); | ||
} | ||
sendData(data, variableName = "data") { | ||
const message = { | ||
type: "from-host-to-iframe", | ||
payload: { | ||
action: "set-data", | ||
data, | ||
variableName, | ||
}, | ||
}; | ||
window.parent.postMessage(message, this.originURL); | ||
} | ||
} | ||
export default MessageHandler; |