From 516b703167fe1a845366d79e25cad922eebd92f1 Mon Sep 17 00:00:00 2001 From: Aurelien Franky Date: Sat, 26 Oct 2024 16:35:41 +0200 Subject: [PATCH 1/4] add onConnectionSetup event handler --- src/guest.ts | 8 +++++--- src/host.ts | 1 + src/types.ts | 4 ++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/guest.ts b/src/guest.ts index 3b95cce..194b7ef 100644 --- a/src/guest.ts +++ b/src/guest.ts @@ -1,6 +1,6 @@ import { extractMethods, isWorker } from "./helpers"; import { registerLocalMethods, registerRemoteMethods } from "./rpc"; -import { actions, events, IConnection, ISchema } from "./types"; +import { actions, EventHandlers, events, IConnection, ISchema } from "./types"; const REQUEST_INTERVAL = 10; const TIMEOUT_INTERVAL = 3000; @@ -8,12 +8,12 @@ const TIMEOUT_INTERVAL = 3000; let interval: any = null; let connected = false; -function connect(schema: ISchema = {}): Promise { +function connect(schema: ISchema = {}, eventHandlers?: EventHandlers): Promise { return new Promise((resolve, reject) => { const localMethods = extractMethods(schema); // on handshake response - function handleHandshakeResponse(event: any) { + async function handleHandshakeResponse(event: any) { if (event.data.action !== actions.HANDSHAKE_REPLY) return; // register local methods @@ -27,6 +27,8 @@ function connect(schema: ISchema = {}): Promise { event ); + await eventHandlers?.onConnectionSetup?.(remote); + // close the connection and all listeners when called const close = () => { self.removeEventListener(events.MESSAGE, handleHandshakeResponse); diff --git a/src/host.ts b/src/host.ts index 0102a6c..fcd69bd 100644 --- a/src/host.ts +++ b/src/host.ts @@ -70,6 +70,7 @@ function connect(guest: HTMLIFrameElement | Worker, schema: ISchema = {}): Promi listeners.removeEventListener(events.MESSAGE, handleHandshake); unregisterRemote(); unregisterLocal(); + if (guestIsWorker) (guest as Worker).terminate(); }; // resolve connection object diff --git a/src/types.ts b/src/types.ts index fab3433..ee4bb73 100644 --- a/src/types.ts +++ b/src/types.ts @@ -59,3 +59,7 @@ export interface IRPCResolvePayload { callName: string; connectionID: string; } + +export interface EventHandlers { + onConnectionSetup: (remote: ISchema) => Promise; +} From 0b6a3b9ed9dc30be8e5b8858e11b780277131663 Mon Sep 17 00:00:00 2001 From: Aurelien Franky Date: Sat, 26 Oct 2024 16:35:53 +0200 Subject: [PATCH 2/4] 0.3.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 72250ff..ffc424f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "rimless", - "version": "0.2.3", + "version": "0.3.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "rimless", - "version": "0.2.3", + "version": "0.3.0", "license": "MIT", "devDependencies": { "@chromatic-com/storybook": "^1.7.0", diff --git a/package.json b/package.json index e83d6ed..8bafa8e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "rimless", "author": "Aurélien Franky", - "version": "0.2.3", + "version": "0.3.0", "license": "MIT", "homepage": "https://github.com/au-re/rimless", "description": "event base communication made easy with a promise-based API wrapping `postMessage`", From 7566bf59ce1f09238ef6b3376eaef85b03448e35 Mon Sep 17 00:00:00 2001 From: Aurelien Franky Date: Sun, 27 Oct 2024 22:43:10 +0100 Subject: [PATCH 3/4] await a connection confirmed before resolving the connection in the host --- docs/GettingStarted.mdx | 7 +++- docs/examples/WorkerExample.tsx | 34 +++++++++++++++++++ docs/examples/iframe.html | 2 +- docs/examples/worker.ts | 35 +++++++++++++++++++ docs/typings.d.ts | 11 ++++++ src/guest.ts | 33 +++++++----------- src/helpers.ts | 60 ++++++++++++++++++++++++++------- src/host.ts | 13 ++++--- src/rpc.ts | 6 ++-- src/types.ts | 2 +- src/typings.d.ts | 1 - src/utils.ts | 45 ------------------------- src/vite-env.d.ts | 5 --- 13 files changed, 159 insertions(+), 95 deletions(-) create mode 100644 docs/examples/WorkerExample.tsx create mode 100644 docs/examples/worker.ts create mode 100644 docs/typings.d.ts delete mode 100644 src/typings.d.ts delete mode 100644 src/utils.ts diff --git a/docs/GettingStarted.mdx b/docs/GettingStarted.mdx index 5aebe25..0fa72ab 100644 --- a/docs/GettingStarted.mdx +++ b/docs/GettingStarted.mdx @@ -1,11 +1,12 @@ import { Meta } from "@storybook/blocks"; import SingleIframeExample from "./examples/SingleIframeExample"; +import WorkerExample from "./examples/WorkerExample"; # Rimless -Rimless makes event based communication easy with a promise-based API wrapping [postMessage](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage). +Rimless makes event based communication easy with a promise-based API wrapping [postMessage](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage). Rimless works with both **iframes** and **webworkers**. @@ -18,3 +19,7 @@ In the example below you can invoke remote procedures from an iframe to the host versa. + +## RPCs on Web webworkers + + diff --git a/docs/examples/WorkerExample.tsx b/docs/examples/WorkerExample.tsx new file mode 100644 index 0000000..843373a --- /dev/null +++ b/docs/examples/WorkerExample.tsx @@ -0,0 +1,34 @@ +import React from "react"; + +import { host } from "../../src/index"; +import Worker from "./worker?worker"; + +function WorkerExample() { + const [color, setColor] = React.useState("#fff"); + const [message, setMessage] = React.useState(""); + + const onClick = async () => { + const options = { initialValue: "initial value from host" }; + const connection = await host.connect(new Worker(), options); + + const messageRes = await connection?.remote.getMessage(); + setMessage(messageRes); + + const colorRes = await connection?.remote.createColor(); + setColor(colorRes); + }; + + return ( +
+
+

HOST

+ +

{message}

+
+
+ ); +} + +export default WorkerExample; diff --git a/docs/examples/iframe.html b/docs/examples/iframe.html index 5a92fba..09d0787 100644 --- a/docs/examples/iframe.html +++ b/docs/examples/iframe.html @@ -4,7 +4,7 @@ Rimless Guest - +