From 2044e1bbe6f4e0c06eedd0fbeb8d96236659c99d Mon Sep 17 00:00:00 2001 From: Zhang Minghan Date: Tue, 24 Oct 2023 20:02:53 +0800 Subject: [PATCH] fix message dispatch error --- app/src/conf.ts | 2 +- app/src/conversation/conversation.ts | 8 ++++++-- app/src/conversation/manager.ts | 7 ++++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/src/conf.ts b/app/src/conf.ts index aecb8dde..206897f2 100644 --- a/app/src/conf.ts +++ b/app/src/conf.ts @@ -1,7 +1,7 @@ import axios from "axios"; import { Model } from "./conversation/types.ts"; -export const version = "3.5.3"; +export const version = "3.5.4"; export const dev: boolean = window.location.hostname === "localhost"; export const deploy: boolean = true; export let rest_api: string = "http://localhost:8094"; diff --git a/app/src/conversation/conversation.ts b/app/src/conversation/conversation.ts index dc16ce34..8e76e730 100644 --- a/app/src/conversation/conversation.ts +++ b/app/src/conversation/conversation.ts @@ -3,7 +3,7 @@ import { Message } from "./types.ts"; import { sharingEvent } from "../events/sharing.ts"; import { connectionEvent } from "../events/connection.ts"; -type ConversationCallback = (idx: number, message: Message[]) => void; +type ConversationCallback = (idx: number, message: Message[]) => boolean; export class Conversation { protected connection?: Connection; @@ -109,7 +109,11 @@ export class Conversation { } public triggerCallback() { - this.callback && this.callback(this.id, this.copyMessages()); + if (!this.callback) return; + const interval = setInterval(() => { + const state = this.callback && this.callback(this.id, this.copyMessages()); + if (state) clearInterval(interval); + }, 100); } public addMessage(message: Message): number { diff --git a/app/src/conversation/manager.ts b/app/src/conversation/manager.ts index ecce429e..d56afdab 100644 --- a/app/src/conversation/manager.ts +++ b/app/src/conversation/manager.ts @@ -38,11 +38,12 @@ export class Manager { this.dispatch = dispatch; } - public callback(idx: number, message: Message[]): void { + public callback(idx: number, message: Message[]): boolean { console.debug( `[manager] conversation receive message (id: ${idx}, length: ${message.length})`, ); if (idx === this.current) this.dispatch?.(setMessages(message)); + return !!this.dispatch; } public getCurrent(): number { @@ -56,8 +57,8 @@ export class Manager { public createConversation(id: number): Conversation { console.debug(`[manager] create conversation instance (id: ${id})`); const _this = this; - return new Conversation(id, function (idx: number, message: Message[]) { - _this.callback(idx, message); + return new Conversation(id, function (idx: number, message: Message[]): boolean { + return _this.callback(idx, message); }); }