Use EventEmitter instead of native event handling

This commit is contained in:
maver
2023-06-05 13:21:07 +02:00
parent 4476cbdc81
commit a5e2f11e61
4 changed files with 17 additions and 5 deletions

View File

@@ -7522,6 +7522,7 @@ $(document).ready(function () {
if (popup_type == "del_ch") {
const deleteChats = !!$("#del_char_checkbox").prop("checked");
await handleDeleteCharacter(popup_type, this_chid, deleteChats);
eventSource.emit('characterDeleted', {id: this_chid, character: characters[this_chid]});
}
if (popup_type == "alternate_greeting" && menu_type !== "create") {
createOrEditCharacter();

View File

@@ -1,6 +1,6 @@
import {countTokens} from "./openai.js";
import {DraggablePromptListModule as DraggableList} from "./DraggableList.js";
import {substituteParams} from "../script.js";
import {eventSource, substituteParams} from "../script.js";
// Thrown by ChatCompletion when a requested prompt couldn't be found.
class IdentifierNotFoundError extends Error {
@@ -227,19 +227,19 @@ PromptManagerModule.prototype.init = function (moduleConfiguration, serviceSetti
}
// Re-render when the character changes.
document.addEventListener('characterSelected', (event) => {
eventSource.on('characterSelected', (event) => {
this.handleCharacterSelected(event)
this.saveServiceSettings().then(() => this.render());
});
// Re-render when the group changes.
document.addEventListener('groupSelected', (event) => {
eventSource.on('groupSelected', (event) => {
this.handleGroupSelected(event)
this.saveServiceSettings().then(() => this.render());
});
// Sanitize settings after character has been deleted.
document.addEventListener('characterDeleted', (event) => {
eventSource.on('characterDeleted', (event) => {
this.handleCharacterDeleted(event)
this.saveServiceSettings().then(() => this.render());
});

View File

@@ -13,6 +13,7 @@ import {
menu_type,
max_context,
saveSettingsDebounced,
eventSource,
active_group,
active_character,
setActiveGroup,
@@ -924,6 +925,16 @@ $("document").ready(function () {
$(document).on("click", ".character_select", function () {
setActiveCharacter($(this).find('.avatar').attr('title'));
setActiveGroup(null);
const chid = $(this).attr('chid');
eventSource.emit(
'characterSelected',
{detail: {id: chid, character: characters[chid]}})
.then(r => {
SaveLocal('ActiveChar', chid);
SaveLocal('ActiveGroup', null);
});
saveSettingsDebounced();
});

View File

@@ -1167,7 +1167,7 @@ function select_group_chats(groupId, skipAnimation) {
await eventSource.emit(event_types.GROUP_UPDATED);
});
document.dispatchEvent(new CustomEvent('groupSelected', { detail: {id: groupId, group: group}}));
eventSource.emit('groupSelected', {detail: {id: groupId, group: group}});
}
function updateFavButtonState(state) {