From f13e718dc7297db24cb5b61e0f98cbdc10f07be0 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Tue, 2 Apr 2024 20:25:37 +0300 Subject: [PATCH] Compatibility with extensions --- public/lib/eventemitter.js | 6 ++++++ public/script.js | 1 + public/scripts/sse-stream.js | 2 ++ 3 files changed, 9 insertions(+) diff --git a/public/lib/eventemitter.js b/public/lib/eventemitter.js index 046991a05..a1b40e38c 100644 --- a/public/lib/eventemitter.js +++ b/public/lib/eventemitter.js @@ -29,6 +29,12 @@ var EventEmitter = function () { }; EventEmitter.prototype.on = function (event, listener) { + // Unknown event used by external libraries? + if (event === undefined) { + console.trace('EventEmitter: Cannot listen to undefined event'); + return; + } + if (typeof this.events[event] !== 'object') { this.events[event] = []; } diff --git a/public/script.js b/public/script.js index 3078d8a88..805f54dff 100644 --- a/public/script.js +++ b/public/script.js @@ -416,6 +416,7 @@ export const event_types = { // TODO: Naming convention is inconsistent with other events CHARACTER_DELETED: 'characterDeleted', CHARACTER_DUPLICATED: 'character_duplicated', + SMOOTH_STREAM_TOKEN_RECEIVED: 'smooth_stream_token_received', }; export const eventSource = new EventEmitter(); diff --git a/public/scripts/sse-stream.js b/public/scripts/sse-stream.js index ad50798d4..86caf38d7 100644 --- a/public/scripts/sse-stream.js +++ b/public/scripts/sse-stream.js @@ -1,3 +1,4 @@ +import { eventSource, event_types } from '../script.js'; import { power_user } from './power-user.js'; import { delay } from './utils.js'; @@ -257,6 +258,7 @@ export class SmoothEventSourceStream extends EventSourceStream { hasFocus && await delay(getDelay(lastStr)); controller.enqueue(new MessageEvent(event.type, { data: JSON.stringify(parsed.data) })); lastStr = parsed.chunk; + hasFocus && await eventSource.emit(event_types.SMOOTH_STREAM_TOKEN_RECEIVED, parsed.chunk); } } catch { controller.enqueue(event);