From b6f85c57bac343bb07bd5a2e3288fcabc6924bb0 Mon Sep 17 00:00:00 2001 From: 0xZensh Date: Thu, 26 Sep 2024 13:20:36 +0800 Subject: [PATCH] chore: improve loading messages --- src/ic_panda_frontend/package.json | 2 +- .../src/lib/canisters/actors.ts | 3 +++ .../messages/ChannelMessages.svelte | 9 ++++++-- .../components/messages/ChannelSetting.svelte | 3 ++- src/ic_panda_frontend/src/lib/constants.ts | 2 +- .../src/lib/stores/message.ts | 21 +++++++++++-------- .../src/lib/stores/message_agent.ts | 19 +++++++++++------ 7 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/ic_panda_frontend/package.json b/src/ic_panda_frontend/package.json index 04f8633..ad78060 100644 --- a/src/ic_panda_frontend/package.json +++ b/src/ic_panda_frontend/package.json @@ -80,5 +80,5 @@ "test": "vitest run" }, "type": "module", - "version": "2.2.2" + "version": "2.2.3" } \ No newline at end of file diff --git a/src/ic_panda_frontend/src/lib/canisters/actors.ts b/src/ic_panda_frontend/src/lib/canisters/actors.ts index b388c65..b209e4d 100644 --- a/src/ic_panda_frontend/src/lib/canisters/actors.ts +++ b/src/ic_panda_frontend/src/lib/canisters/actors.ts @@ -27,6 +27,9 @@ export const createActor = async >({ return Actor.createActor(idlFactory, { agent, canisterId + // queryTransform: (methodName: string, args: unknown[], callConfig: any) => { + // console.log('queryTransform', methodName, args, callConfig) + // } }) } diff --git a/src/ic_panda_frontend/src/lib/components/messages/ChannelMessages.svelte b/src/ic_panda_frontend/src/lib/components/messages/ChannelMessages.svelte index b6d0f47..9cdc13d 100644 --- a/src/ic_panda_frontend/src/lib/components/messages/ChannelMessages.svelte +++ b/src/ic_panda_frontend/src/lib/components/messages/ChannelMessages.svelte @@ -204,7 +204,7 @@ id, dek, start, - start + 20 + Math.min(start + 20, latestMessageId) ) if (messages.length > 0) { @@ -266,7 +266,12 @@ async (signal: AbortSignal, abortingQue: (() => void)[]) => { abortingQue.push(popupDestroy) - channelInfo = await myState.refreshChannel(channelInfo) + if ( + !channelInfo._kek || + channelInfo._sync_at < Date.now() - 5 * 60 * 1000 + ) { + channelInfo = await myState.refreshChannel(channelInfo) + } messageStart = channelInfo.message_start latestMessageId = channelInfo.latest_message_id lastRead = Math.min(channelInfo.my_setting.last_read, MaybeMaxMessageId) diff --git a/src/ic_panda_frontend/src/lib/components/messages/ChannelSetting.svelte b/src/ic_panda_frontend/src/lib/components/messages/ChannelSetting.svelte index d79179d..d1ca4de 100644 --- a/src/ic_panda_frontend/src/lib/components/messages/ChannelSetting.svelte +++ b/src/ic_panda_frontend/src/lib/components/messages/ChannelSetting.svelte @@ -10,6 +10,7 @@ import IconCircleSpin from '$lib/components/icons/IconCircleSpin.svelte' import IconEditLine from '$lib/components/icons/IconEditLine.svelte' import IconExchange2Line from '$lib/components/icons/IconExchange2Line.svelte' + import IconLogout from '$lib/components/icons/IconLogout.svelte' import { toastRun } from '$lib/stores/toast' import { errMessage } from '$lib/types/result' import { sleep } from '$lib/utils/helper' @@ -351,7 +352,7 @@ class="variant-filled-warning disabled:variant-filled-surface" on:click={onClickMyLeaving} disabled={myLeavingSubmitting || leavingWord != channelInfo.name} - >Leave 0) { + const users = await this.api.batch_get_users(todo) + for (const info of users) { + rt.push(info) + await setUser(now, info) + } } return rt } diff --git a/src/ic_panda_frontend/src/lib/stores/message_agent.ts b/src/ic_panda_frontend/src/lib/stores/message_agent.ts index 4b2d275..7eb0568 100644 --- a/src/ic_panda_frontend/src/lib/stores/message_agent.ts +++ b/src/ic_panda_frontend/src/lib/stores/message_agent.ts @@ -22,6 +22,10 @@ import { KVS } from './kvstore' export type CachedMessage = Message & { canister: Principal; channel: number } +export interface SyncAt { + _sync_at: number +} + // should be running in Web Workers. export class MessageAgent extends EventTarget { readonly id: string @@ -328,8 +332,11 @@ export class MessageAgent extends EventTarget { // ------------ Channels ------------ - async getChannel(canister: Principal, id: number): Promise { - let val = await this._db.get('Channels', [ + async getChannel( + canister: Principal, + id: number + ): Promise { + let val = await this._db.get('Channels', [ canister.toUint8Array(), id ]) @@ -343,7 +350,7 @@ export class MessageAgent extends EventTarget { canister: Principal, id: number, signal?: AbortSignal - ): Promise> { + ): Promise> { const channel = await this.getChannel(canister, id) const eventType = `Channel:${canister.toText()}/${id}` return readable(channel, (set) => { @@ -467,7 +474,7 @@ export class MessageAgent extends EventTarget { canister: Principal, id: number, updated_at: bigint = 0n - ): Promise { + ): Promise { const api = await this.api.channelAPI(canister) const channel = await api.get_channel_if_update(id, updated_at) if (!channel) { @@ -490,7 +497,7 @@ export class MessageAgent extends EventTarget { for (const mid of channel.deleted_messages) { await this.updateDeletedMessage(canisterId, id, mid) } - return (await this.setChannel(channel)) as ChannelInfo + return (await this.setChannel(channel)) as ChannelInfo & SyncAt } async setChannel( @@ -500,7 +507,7 @@ export class MessageAgent extends EventTarget { channel.canister.toUint8Array(), channel.id ]) - val = { ...val, ...channel } as ChannelInfo + val = { ...val, ...channel, _sync_at: Date.now() } as ChannelInfo & SyncAt await this._db.set('Channels', val) this.dispatchEvent( new CustomEvent(`Channel:${val.canister.toText()}/${val.id}`, {