Skip to content

Commit

Permalink
Merge branch 'soeren/dev-core'
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Dec 11, 2024
2 parents 0c7c8fe + 7f9bc49 commit 8419e1c
Show file tree
Hide file tree
Showing 25 changed files with 8,286 additions and 9 deletions.
Empty file.
63 changes: 63 additions & 0 deletions public/albatross/launcher/browser/client-proxy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// launcher/client-proxy.ts
function clientFactory(workerFactory, comlinkWrapper) {
return {
async create(config) {
const worker = workerFactory();
await new Promise((resolve) => {
const readyListener = (event) => {
removeEventListener(worker, "message", readyListener);
if (getEventData(event) === "NIMIQ_ONLOAD") resolve();
};
addEventListener(worker, "message", readyListener);
});
console.debug("Client WASM worker loaded");
const client = comlinkWrapper(worker);
if (typeof window !== "undefined") {
window.addEventListener("offline", () => worker.postMessage("offline"));
window.addEventListener("online", () => worker.postMessage("online"));
}
if (typeof document !== "undefined") {
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "visible") {
worker.postMessage("visible");
}
});
}
console.debug("Sending NIMIQ_INIT message to client worker");
worker.postMessage({
type: "NIMIQ_INIT",
config
});
await new Promise((resolve, reject) => {
addEventListener(worker, "message", (event) => {
const eventData = getEventData(event);
if (!("ok" in eventData)) return;
if (eventData.ok === true) resolve();
if (eventData.ok === false && "error" in eventData && typeof eventData.error === "string") {
const error = new Error(eventData.error);
if ("stack" in eventData && typeof eventData.stack === "string") {
error.stack = eventData.stack;
}
reject(error);
}
});
});
console.debug("Have client worker remote");
return client;
}
};
}
function addEventListener(worker, type, listener) {
const method = "addListener" in worker ? "addListener" : "addEventListener";
worker[method](type, listener);
}
function removeEventListener(worker, type, listener) {
const method = "removeListener" in worker ? "removeListener" : "removeEventListener";
worker[method](type, listener);
}
function getEventData(event) {
return typeof event === "object" && "data" in event ? event.data : event;
}
export {
clientFactory
};
38 changes: 38 additions & 0 deletions public/albatross/launcher/browser/cryptoutils-worker-proxy.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// launcher/cryptoutils-worker-proxy.ts
var remote;
function cryptoUtilsWorkerFactory(workerFactory, comlinkWrapper) {
const proxy = {};
["otpKdf"].forEach((method) => {
proxy[method] = async function() {
remote = remote || await startWorker(workerFactory, comlinkWrapper);
return remote[method](...arguments);
};
});
return proxy;
}
async function startWorker(workerFactory, comlinkWrapper) {
const worker = workerFactory();
await new Promise((resolve) => {
const readyListener = (event) => {
removeEventListener(worker, "message", readyListener);
if (getEventData(event) === "NIMIQ_ONLOAD") resolve();
};
addEventListener(worker, "message", readyListener);
});
console.debug("Have crypto worker remote");
return comlinkWrapper(worker);
}
function addEventListener(worker, type, listener) {
const method = "addListener" in worker ? "addListener" : "addEventListener";
worker[method](type, listener);
}
function removeEventListener(worker, type, listener) {
const method = "removeListener" in worker ? "removeListener" : "removeEventListener";
worker[method](type, listener);
}
function getEventData(event) {
return typeof event === "object" && "data" in event ? event.data : event;
}
export {
cryptoUtilsWorkerFactory
};
27 changes: 27 additions & 0 deletions public/albatross/launcher/browser/transfer-handlers.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// launcher/transfer-handlers.ts
function setupMainThreadTransferHandlers(Comlink, classes) {
Comlink.transferHandlers.set("function", {
canHandle: (obj) => typeof obj === "function",
serialize(obj) {
return Comlink.transferHandlers.get("proxy").serialize(obj);
}
// deserialize(plain) {}, // Cannot receive functions from worker
});
function canBeSerialized(obj) {
return obj instanceof classes.Address || obj instanceof classes.Transaction;
}
Comlink.transferHandlers.set("plain", {
canHandle: (obj) => canBeSerialized(obj) || Array.isArray(obj) && obj.some((item) => canBeSerialized(item)),
serialize(obj) {
if (Array.isArray(obj)) {
return [obj.map((item) => canBeSerialized(item) ? item.serialize() : item), []];
} else {
return [obj.serialize(), []];
}
}
// deserialize(plain) {}, // Cannot receive class instances from worker
});
}
export {
setupMainThreadTransferHandlers
};
Loading

0 comments on commit 8419e1c

Please sign in to comment.