Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
turbocrime committed Sep 5, 2024
1 parent a0172f3 commit 5d625aa
Show file tree
Hide file tree
Showing 5 changed files with 362 additions and 261 deletions.
38 changes: 38 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,43 @@
"turbo": "^2.0.12",
"typescript": "^5.5.3",
"vitest": "^1.6.0"
},
"pnpm": {
"overrides": {
"@penumbra-zone/bech32m": "file:///Users/yet/Developer/github.com/penumbra-zone/web/packages/bech32m/penumbra-zone-bech32m-7.0.0.tgz",
"@penumbra-zone/client": "file:///Users/yet/Developer/github.com/penumbra-zone/web/packages/client/penumbra-zone-client-18.1.0.tgz",
"@penumbra-zone/crypto-web": "file:///Users/yet/Developer/github.com/penumbra-zone/web/packages/crypto/penumbra-zone-crypto-web-23.0.0.tgz",
"@penumbra-zone/getters": "file:///Users/yet/Developer/github.com/penumbra-zone/web/packages/getters/penumbra-zone-getters-16.0.0.tgz",
"@penumbra-zone/keys": "file:///Users/yet/Developer/github.com/penumbra-zone/web/packages/keys/penumbra-zone-keys-4.2.1.tgz",
"@penumbra-zone/perspective": "file:///Users/yet/Developer/github.com/penumbra-zone/web/packages/perspective/penumbra-zone-perspective-29.0.0.tgz",
"@penumbra-zone/protobuf": "file:///Users/yet/Developer/github.com/penumbra-zone/web/packages/protobuf/penumbra-zone-protobuf-6.0.0.tgz",
"@penumbra-zone/query": "file:///Users/yet/Developer/github.com/penumbra-zone/web/packages/query/penumbra-zone-query-30.0.0.tgz",
"@penumbra-zone/services": "file:///Users/yet/Developer/github.com/penumbra-zone/web/packages/services/penumbra-zone-services-33.0.0.tgz",
"@penumbra-zone/storage": "file:///Users/yet/Developer/github.com/penumbra-zone/web/packages/storage/penumbra-zone-storage-29.0.0.tgz",
"@penumbra-zone/transport-chrome": "file:///Users/yet/Developer/github.com/penumbra-zone/web/packages/transport-chrome/penumbra-zone-transport-chrome-8.0.1.tgz",
"@penumbra-zone/transport-dom": "file:///Users/yet/Developer/github.com/penumbra-zone/web/packages/transport-dom/penumbra-zone-transport-dom-7.5.0.tgz",
"@penumbra-zone/types": "file:///Users/yet/Developer/github.com/penumbra-zone/web/packages/types/penumbra-zone-types-22.0.0.tgz",
"@penumbra-zone/ui": "file:///Users/yet/Developer/github.com/penumbra-zone/web/packages/ui/penumbra-zone-ui-9.1.0.tgz",
"@penumbra-zone/wasm": "file:///Users/yet/Developer/github.com/penumbra-zone/web/packages/wasm/penumbra-zone-wasm-27.0.0.tgz"
},
"peerDependencyRules": {
"allowAny": [
"@penumbra-zone/bech32m",
"@penumbra-zone/client",
"@penumbra-zone/crypto-web",
"@penumbra-zone/getters",
"@penumbra-zone/keys",
"@penumbra-zone/perspective",
"@penumbra-zone/protobuf",
"@penumbra-zone/query",
"@penumbra-zone/services",
"@penumbra-zone/storage",
"@penumbra-zone/transport-chrome",
"@penumbra-zone/transport-dom",
"@penumbra-zone/types",
"@penumbra-zone/ui",
"@penumbra-zone/wasm"
]
}
}
}
32 changes: 17 additions & 15 deletions packages/chrome-offscreen-worker/src/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@ const persist = () => {
const sessionId = location.hash.slice(1);

chrome.runtime.onConnect.addListener(newSessionPort => {
console.log(
'offscreen entry onConnect with hash',
location.hash,
'handling port',
newSessionPort.name,
);
if (newSessionPort.name === sessionId) {
console.log('offscreen accepting session', sessionId);
console.log('entry accepting', sessionId);
persist();
attachSession(newSessionPort);
}
Expand All @@ -47,9 +41,19 @@ const attachSession = (sessionPort: chrome.runtime.Port) => {
});
const workerPort = chrome.runtime.connect({ name: workerId }) as OffscreenWorkerPort;

worker.addEventListener('error', event =>
workerPort.postMessage({ type: 'error', init: event }),
);
worker.addEventListener('messageerror', event =>
workerPort.postMessage({ type: 'messageerror', init: { ...event, ports: undefined } }),
);
worker.addEventListener('message', event =>
workerPort.postMessage({ type: 'message', init: { ...event, ports: undefined } }),
);

// setup disconnect handler
workerPort.onDisconnect.addListener(() => {
console.log('workerPort onDisconnect', workerId);
console.log('entry workerPort onDisconnect', workerId);
workers.delete(workerId);
if (!workers.size) {
contemplate('no workers');
Expand All @@ -61,18 +65,16 @@ const attachSession = (sessionPort: chrome.runtime.Port) => {
workers.set(workerId, { worker, workerPort });

const workerListener = (json: unknown) => {
console.log('workerListener', json, workerId);
console.log('entry workerListener', json, workerId);
if (isOffscreenWorkerEventMessage(json)) {
if (isOffscreenWorkerEvent(json.init, json.type)) {
persist();
switch (json.type) {
case 'error':
worker.dispatchEvent(new ErrorEvent(json.type, json.init));
return;
case 'message':
worker.postMessage((json.init as MessageEventInit)?.data);
break;
case 'error':
case 'messageerror':
worker.dispatchEvent(new MessageEvent(json.type, json.init));
return;
default:
throw new Error('Unknown message in worker input', {
cause: { message: json, workerId },
Expand All @@ -87,7 +89,7 @@ const attachSession = (sessionPort: chrome.runtime.Port) => {
};

sessionPort.onMessage.addListener((json: unknown) => {
console.log('offscreen control message', json, sessionId);
console.log('entry control', json, sessionId);
if (isOffscreenRootControlMessage(json)) {
switch (json.type) {
case 'new': {
Expand Down
6 changes: 3 additions & 3 deletions packages/chrome-offscreen-worker/src/messages/worker-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ export const isOffscreenWorkerEventMessage = (message: unknown): message is Offs
message != null &&
'type' in message &&
typeof message.type === 'string' &&
'event' in message &&
typeof message.event === 'object' &&
message.event != null;
'init' in message &&
typeof message.init === 'object' &&
message.init != null;

export const isOffscreenWorkerEvent = <T extends OffscreenWorkerEventType>(
init: unknown,
Expand Down
44 changes: 35 additions & 9 deletions packages/chrome-offscreen-worker/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { OffscreenControl } from './control';
import {
isOffscreenWorkerEvent,
isOffscreenWorkerEventMessage,
type OffscreenWorkerEvent,
type OffscreenWorkerPort,
} from './messages/worker-event';
import type { WorkerConstructorParamsPrimitive } from './messages/root-control';
Expand Down Expand Up @@ -72,16 +71,19 @@ export class OffscreenWorker implements Worker {
}

private workerListener = (json: unknown, port: chrome.runtime.Port) => {
console.debug(this.params[1].name, 'workerListener', json, port.name);
console.debug('worker workerListener', json, port.name);
if (isOffscreenWorkerEventMessage(json) && isOffscreenWorkerEvent(json.init, json.type)) {
switch (json.type) {
case 'error':
this.internalTarget.dispatchEvent(new ErrorEvent(json.type, json.init));
this.externalTarget.dispatchEvent(new ErrorEvent(json.type, { ...json.init }));
return;
case 'message':
case 'messageerror':
this.internalTarget.dispatchEvent(new MessageEvent(json.type, json.init));
this.internalTarget.postMessage(
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
new MessageEvent(json.type, { data: (json.init as MessageEventInit).data }),
);
return;
case 'messageerror':
default:
//console.warn('Dispatching unknown event', json);
//this.internalTarget.dispatchEvent(new Event(json.type, json.init));
Expand All @@ -93,14 +95,38 @@ export class OffscreenWorker implements Worker {
};

private parentListener = (evt: Event) => {
console.debug(this.params[1].name, 'parentListener', [evt]);
const unknownMessage = { type: evt.type, init: { ...evt } };
console.debug('worker parentListener', [evt]);
switch (evt.type) {
case 'error':
void this.workerPort.then(port =>
port.postMessage({
type: 'error',
init: {
message: (evt as ErrorEvent).message,
filename: (evt as ErrorEvent).filename,
lineno: (evt as ErrorEvent).lineno,
colno: (evt as ErrorEvent).colno,
//error: (evt as ErrorEvent).error,
},
}),
);
break;
case 'message':
void this.workerPort.then(port =>
port.postMessage({
type: 'message',
init: { data: (evt as MessageEvent).data as unknown },
}),
);
break;
case 'messageerror':
void this.workerPort.then(port => port.postMessage(unknownMessage as OffscreenWorkerEvent));
return;
void this.workerPort.then(port =>
port.postMessage({
type: 'messageerror',
init: { data: (evt as MessageEvent).data as unknown },
}),
);
break;
default:
throw new Error('Unknown event from parent', { cause: evt });
}
Expand Down
Loading

0 comments on commit 5d625aa

Please sign in to comment.