Skip to content

Commit

Permalink
feat: begin adding messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Sep 17, 2024
1 parent 90ba773 commit ad5e2b4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
5 changes: 3 additions & 2 deletions apps/namadillo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@
"release:dry-run": "release-it --verbose --dry-run --ci",
"release:no-npm": "release-it --verbose --no-npm.publish --ci",
"start:proxy": "node ./scripts/startProxies.js",
"dev": "tsc --watch --project tsconfig.sw.json& vite",
"dev": "yarn dev:sw& vite",
"dev:sw": "tsc --watch --project tsconfig.sw.json",
"preview": "vite preview",
"dev:local": "NODE_ENV=development NAMADA_INTERFACE_LOCAL=\"true\" yarn dev",
"dev:proxy": "NAMADA_INTERFACE_PROXY=true && ./scripts/start-proxies.sh && yarn dev:local",
"dev:proxy": "NAMADA_INTERFACE_PROXY=true && ./scripts/start-proxies.sh && yarn dev",
"build": "NODE_ENV=production && yarn wasm:build && vite build",
"build:only": "NODE_ENV=production && vite build",
"lint": "eslint src --ext .ts,.tsx",
Expand Down
31 changes: 30 additions & 1 deletion apps/namadillo/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,38 @@ if ("serviceWorker" in navigator) {
})
.then((registration) => {
console.log("Service Worker registered: ", registration);

const msgChannel = new MessageChannel();
registration.active?.postMessage(
{
type: "INIT_PORT",
},
[msgChannel.port2]
);

msgChannel.port1.onmessage = (event) => {
switch (event.type) {
case "namadillo:hasMaspParamsResponse":
console.warn(`${event.data.param}: ${event.data.hasMaspParam}`);
break;
}
};

registration.active?.postMessage({
type: "namadillo:hasMaspParams",
param: "masp-output.params",
});
registration.active?.postMessage({
type: "namadillo:hasMaspParams",
param: "masp-spend.params",
});
registration.active?.postMessage({
type: "namadillo:hasMaspParams",
param: "masp-convert.params",
});
})
.catch((error) => {
console.log("Service Worker registration failed: ", error);
console.warn("Service Worker registration failed: ", error);
});
});
}
46 changes: 45 additions & 1 deletion apps/namadillo/sw/sw.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// <reference lib="webworker" />
importScripts("constants.js");
importScripts("crypto.js");
importScripts("fetch.js");
Expand Down Expand Up @@ -27,14 +28,57 @@ const logSuccess = ({ param, bytes }: MaspParamBytes): void =>

const logError = (e: any) => console.error(e);

const params = [MaspParam.Output, MaspParam.Spend, MaspParam.Convert];

(async () => {
[MaspParam.Output, MaspParam.Spend, MaspParam.Convert].map(async (param) => {
params.forEach(async (param) => {
if (!(await store.get(param))) {
await fetchMaspParam(param)
.then(validateMaspParamBytes)
.then(storeMaspParam)
.then(logSuccess)
.catch(logError);
} else {
console.log(`Found ${param}`);
}
});
})();

/**
* EVENT HANDLERS
*/
const EVENT_PREFIX = "namadillo";

enum EventMessage {
FetchMaspParams = `${EVENT_PREFIX}:fetchMaspParams`,
HasMaspParams = `${EVENT_PREFIX}:hasMaspParams`,
HasMaspParamsResponse = `${EVENT_PREFIX}:hasMaspParamsResponse`,
}

// PORT
let port: MessagePort;

self.addEventListener("message", (event: MessageEvent) => {
console.log({ event });
if (event.data && event.data.type == "INIT_PORT") {
port = event.ports[0];
}
const { data } = event;

switch (data.type as EventMessage) {
case EventMessage.FetchMaspParams:
console.log(`${EventMessage.FetchMaspParams}`);
break;
case EventMessage.HasMaspParams: {
const { param } = data;
store.get(param).then((record) => {
port.postMessage({
type: EventMessage.HasMaspParamsResponse,
param,
hasMaspParam: record ? true : false,
});
});
break;
}
}
});
4 changes: 2 additions & 2 deletions apps/namadillo/tsconfig.sw.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"baseUrl": "./sw",
"lib": ["ESNext", "WebWorker"],
"lib": ["esnext", "webworker"],
"noEmit": false,
"strict": false,
"outDir": "public/sw",
"target": "ESNext",
"target": "esnext",
"isolatedModules": false
},
"include": ["sw"]
Expand Down

0 comments on commit ad5e2b4

Please sign in to comment.