Skip to content

Commit

Permalink
fix message dispatch error
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Oct 24, 2023
1 parent 0064408 commit 2044e1b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/src/conf.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
8 changes: 6 additions & 2 deletions app/src/conversation/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions app/src/conversation/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
});
}

Expand Down

0 comments on commit 2044e1b

Please sign in to comment.