Skip to content

Commit

Permalink
Using shared web worker to maintain state
Browse files Browse the repository at this point in the history
  • Loading branch information
robmoffat committed Dec 12, 2023
1 parent 79f1f39 commit 8a81e5c
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 49 deletions.
25 changes: 25 additions & 0 deletions packages/demo/src/SimpleServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

/**
* This is a very basic implementation of the server side of the desktop agent.
* You can register message ports with it and plug in extra functionality to handle new message types.
*/

class SimpleServer {

constructor() {
}

register(client: MessagePort) {
console.log("Added new listener")
client.addEventListener("message", e => {
console.log("Received: "+e.data)
})
}
}

const theServer = new SimpleServer();

onconnect = function (event) {
const port = event.ports[0]
theServer.register(port)
}
2 changes: 2 additions & 0 deletions packages/demo/src/app1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ function createContext(i: number) {
* Can be called any number of times.
*/
async function startBroadcasting() {
console.log("starting...")
const fdc3 = await getClientAPI();
console.log("got api...")
for (let index = 0; index < 50; index++) {
setTimeout(() => fdc3.broadcast(createContext(index)), index*1000);
}
Expand Down
26 changes: 17 additions & 9 deletions packages/demo/src/dummy-desktop-agent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AppIdentifier } from "@finos/fdc3";
import { AppChecker, DesktopAgentDetailResolver, } from "fdc3-common";
import { AppChecker, DesktopAgentDetailResolver, FDC3_PORT_TRANSFER_REQUEST_TYPE, } from "fdc3-common";
import { supply } from "server";

enum Approach { Tab, Frame, Nested }
Expand Down Expand Up @@ -68,25 +68,33 @@ window.addEventListener("load", () => {
}

// for a given window, allows us to determine which app it is (if any)
const appChecker : AppChecker = o => instances.find(i => i.window ==o) != undefined;
const appChecker : AppChecker = o => instances.find(i => i.window == o)

const detailsResolver : DesktopAgentDetailResolver = (o) => {
const appIdentifier = instances.find(i => i.window ==o)!!
return {
url: "/src/demo/implementation.ts",
apiId : currentApiInstance++,
apikey: "Abc",
appId: appIdentifier.appId,
instanceId: appIdentifier.instanceId!!
apikey: "Abc"
}
}

// set up desktop agent handler here using FDC3 Web Loader (or whatever we call it)
supply(appChecker, detailsResolver);
supply(appChecker, detailsResolver, {
uri: "http://localhost:8080/static/embed/index.html"
})

// hook up the buttons
document.getElementById("app1")?.addEventListener("click", () => launch("/static/app1/index.html", "1"));
document.getElementById("app2")?.addEventListener("click", () => launch("http://robs-pro:8080/static/app2/index.html", "2"));
document.getElementById("app3")?.addEventListener("click", () => launch("http://localhost:8080/static/app3/index.html", "3"));

// // listen for desktop agent ports being sent to connect
// bc.addEventListener(
// "message",
// (event) => {
// console.log("Received "+event)
// if (event.data.type == FDC3_PORT_TRANSFER_REQUEST_TYPE) {
// const port = event.data.payload
// server.register(port)
// }
// });
})

16 changes: 16 additions & 0 deletions packages/demo/src/embed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { FDC3_PORT_TRANSFER_REQUEST_TYPE, FDC3_PORT_TRANSFER_RESPONSE_TYPE, exchange, exchangePostMessage } from "fdc3-common";

const appWindow = window.parent;

window.addEventListener("load", () => {

const sw = new SharedWorker('http://localhost:8080/src/SimpleServer.ts')
sw.port.start()

// the other end to the app
appWindow.postMessage({
type: FDC3_PORT_TRANSFER_RESPONSE_TYPE,
},"*", [sw.port])


})
24 changes: 0 additions & 24 deletions packages/demo/src/nested.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/demo/static/app1/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<title>App 1</title>
<meta charset="UTF-8" />
<script type="module" src="/src/app1.ts"></script>
<script type="module" src="../../src/app1.ts"></script>
</head>

<body>
Expand Down
18 changes: 3 additions & 15 deletions packages/demo/static/embed/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,12 @@
<head>
<title>Desktop Agent - Embedded Communications Agent</title>
<meta charset="UTF-8" />
<script type="module" src="/src/demo/nested.ts"></script>
<script type="module" src="../../src/embed.ts"></script>
</head>

<body>
<p>Dummy Desktop Agent</p>

<select name="opener" id="opener">
<option value="Tab" default>New Tab</option>
<option value="Frame">Frame</option>
<option value="Nested">Nested Frame</option>
</select>

<a type="button" href="#" id="app1" >Open App 1</a>

<a type="button" href="#" id="app2" >Open App 2</a>

<a type="button" href="#" id="app3" >Open App 3</a>

<p>
Nothing to see here
</p>

</body>
Expand Down

0 comments on commit 8a81e5c

Please sign in to comment.