Skip to content

Commit

Permalink
dispatch message to view
Browse files Browse the repository at this point in the history
  • Loading branch information
spearwolf committed Jun 26, 2024
1 parent 2de9f97 commit 165400f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
8 changes: 8 additions & 0 deletions packages/shadow-ents-e2e/src/remote-worker-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ async function main() {
const foo = new ViewComponent('foo');
foo.setProperty('xyz', 123);

foo.on('helloFromFoo', (...args) => {
console.log('HELLO', ...args);
});

const bar = new ViewComponent('bar', {parent: foo});
bar.setProperty('plah', 666);

await testAsyncAction('shadow-env-1st-sync', async () => {
await shadowEnv.sync();
});

await testAsyncAction('shadow-env-hello', async () => {
await foo.onceAsync('helloFromFoo');
});
}
13 changes: 11 additions & 2 deletions packages/shadow-ents-e2e/src/testAsyncAction.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import {createTestNode} from './createTestNode.js';

export async function testAsyncAction(name, action) {
export async function testAsyncAction(name, action, timeout = 5000) {
const waitForAction = new Promise((resolve, reject) => {
const timeoutId = setTimeout(reject, timeout);
action()
.then(() => {
clearTimeout(timeoutId);
resolve();
})
.catch(reject);
});
try {
await Promise.resolve(action());
await waitForAction;
createTestNode(name, 'ok', 'success');
} catch (error) {
createTestNode(name, 'fail', `${error}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/shadow-ents-e2e/tests/remote-worker-env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ test.describe('remote-worker-env', () => {
await page.goto('/pages/remote-worker-env.html');
});

lookupTests(['shadow-env-ready', 'shadow-env-importScript', 'shadow-env-isReady', 'shadow-env-1st-sync']);
lookupTests(['shadow-env-ready', 'shadow-env-importScript', 'shadow-env-isReady', 'shadow-env-1st-sync', 'shadow-env-hello']);
});
7 changes: 7 additions & 0 deletions packages/shadow-ents/src/view/ComponentContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ export class ComponentContext {
}
}

/**
* Dispatch an event (usually from the shadow-objects from the worker env) to a specific view component
*/
dispatchMessage(uuid: string, type: string, data: unknown = undefined, traverseChildren = false) {
this.#components.get(uuid)?.component.dispatchEvent(type, data, traverseChildren);
}

/**
* Create the component change trails at this point in time.
* The next call will only return the differences from the previous call.
Expand Down
1 change: 1 addition & 0 deletions packages/shadow-ents/src/view/ShadowEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,6 @@ export class ShadowEnv {

#onMessageToView(event: Omit<MessageToViewEvent, 'transferables'>) {
console.log('ShadowEnv: onMessageToView', event.type, event.data);
this.view?.dispatchMessage(event.uuid, event.type, event.data);
}
}
8 changes: 5 additions & 3 deletions packages/shadow-ents/src/view/ViewComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,12 @@ export class ViewComponent {
this.#context.dispatchShadowObjectsEvent(this, type, data, transferables);
}

dispatchEvent(type: string, data: unknown) {
dispatchEvent(type: string, data: unknown, traverseChildren = true) {
this.emit(type, data);
for (const child of this.#context!.getChildren(this)) {
child.dispatchEvent(type, data);
if (traverseChildren) {
for (const child of this.#context!.getChildren(this)) {
child.dispatchEvent(type, data);
}
}
}

Expand Down

0 comments on commit 165400f

Please sign in to comment.