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 14, 2024
1 parent 30a3a49 commit 1ff533c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
20 changes: 20 additions & 0 deletions apps/namadillo/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,23 @@ if ("serviceWorker" in navigator) {
});
});
}

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

msgChannel.port1.onmessage = (event) => {
console.warn(event);
};

navigator.serviceWorker.ready.then((registration) => {
console.log("registration?", registration);
registration.active?.postMessage({
type: "namadillo:hasMaspParams",
param: "masp-output.params",
});
});
51 changes: 48 additions & 3 deletions apps/namadillo/sw/sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const { isProxy } = Object.fromEntries(urlParams);
const MASP_MPC_URL =
isProxy === "true" ?
"http://localhost:8010/proxy"
: "https://github.com/anoma/masp-mpc/releases/download/namada-trusted-setup";
: "https://github.com/anoma/masp-mpc/releases/download/namada-trusted-setup";

const store = new IndexedDBKVStore(STORAGE_PREFIX);

Expand All @@ -20,15 +20,18 @@ const resetMaspParamStore = async (): Promise<void> => {
store.set(MaspParam.Convert, null);
};

// resetMaspParamStore();
resetMaspParamStore();

const logSuccess = ({ param, bytes }: MaspParamBytes): void =>
console.info(`Fetched and stored ${param}`, bytes);

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

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

// TODO: Refactor into handler
(async () => {
[MaspParam.Output, MaspParam.Spend, MaspParam.Convert].map(async (param) => {
params.forEach(async (param) => {
if (!(await store.get(param))) {
await fetchMaspParam(param)
.then(validateMaspParamBytes)
Expand All @@ -38,3 +41,45 @@ const logError = (e: any) => console.error(e);
}
});
})();

/**
* 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: any) => {
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,
},
event.origin
);
});
break;
}
}
});

0 comments on commit 1ff533c

Please sign in to comment.