From 383b953763b4b852fb0713239174c2c96482a1e4 Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 19 Oct 2024 17:42:22 +0200 Subject: [PATCH 1/3] add onBeforeUnload handler to weave API, add logic to emit onBeforeUnload event when refreshing applet main view, fix priviledged protocol creation --- example/ui/src/applet-main.ts | 47 ++++++++++ iframes/applet-iframe/src/index.ts | 71 +++++++++++++- libs/api/src/api.ts | 16 ++++ libs/api/src/types.ts | 3 + package.json | 3 +- src/main/index.ts | 12 +-- src/renderer/src/applets/applet-host.ts | 3 + src/renderer/src/elements/main-dashboard.ts | 45 ++++++++- src/renderer/src/layout/views/applet-main.ts | 4 + src/renderer/src/layout/views/applet-view.ts | 6 +- src/renderer/src/layout/views/view-frame.ts | 98 +++++++++++++++----- src/renderer/src/moss-store.ts | 24 ++++- src/renderer/src/utils.ts | 84 ++++++++++++++++- 13 files changed, 369 insertions(+), 47 deletions(-) diff --git a/example/ui/src/applet-main.ts b/example/ui/src/applet-main.ts index 168e9333..1c783ea3 100644 --- a/example/ui/src/applet-main.ts +++ b/example/ui/src/applet-main.ts @@ -13,6 +13,7 @@ import { weaveUrlToLocation, ReadonlyPeerStatusStore, GroupPermissionType, + UnsubscribeFunction, } from '@theweave/api'; import { AgentPubKey, AppClient } from '@holochain/client'; import '@theweave/elements/dist/elements/wal-embed.js'; @@ -78,11 +79,35 @@ export class AppletMain extends LitElement { // @state() // unsubscribe: undefined | (() => void); + @query('#failUnbeforeUnloadCheckmark') + failUnbeforeUnloadCheckmark!: HTMLInputElement; + + onBeforeUnloadUnsubscribe: UnsubscribeFunction | undefined; + onBeforeUnloadUnsubscribe2: UnsubscribeFunction | undefined; + async firstUpdated() { + this.onBeforeUnloadUnsubscribe = this.weaveClient.onBeforeUnload(async () => { + console.log('Unloading in 20 seconds'); + await new Promise((resolve) => setTimeout(resolve, 20000)); + console.log('Unloading now.'); + }); + this.onBeforeUnloadUnsubscribe2 = this.weaveClient.onBeforeUnload(() => { + if (this.failUnbeforeUnloadCheckmark.checked) + throw new Error( + 'The onbeforeunload callback failed (intentionally for testing purposes) in the example applet :(.' + ); + console.log('@example-applet: Running second unbeforeunload callback.'); + }); + this.groupPermissionType = await this.weaveClient.myGroupPermissionType(); this.appletParticipants = await this.weaveClient.appletParticipants(); } + disconnectedCallback(): void { + if (this.onBeforeUnloadUnsubscribe) this.onBeforeUnloadUnsubscribe(); + if (this.onBeforeUnloadUnsubscribe2) this.onBeforeUnloadUnsubscribe2(); + } + // disconnectedCallback(): void { // if (this.unsubscribe) this.unsubscribe(); // } @@ -247,6 +272,7 @@ export class AppletMain extends LitElement {
+

Notifications

@@ -256,6 +282,17 @@ export class AppletMain extends LitElement { + +

on-before-unload behavior

+ +
+ + Make the on-before-unload callback fail when reloading the applet to test how Moss handles this case. +
+ + +

Activity Notification

+ @@ -265,6 +302,9 @@ export class AppletMain extends LitElement { }}> Send Activity Notification + +

Links

+
Enter WAL: