Add event emitter subsystem.

This commit is contained in:
SillyLossy
2023-05-16 16:28:38 +03:00
parent 07bc7c434a
commit 5bfd3787fa
5 changed files with 91 additions and 4 deletions

View File

@ -41,6 +41,7 @@
<script src="scripts/moment.min.js"></script> <script src="scripts/moment.min.js"></script>
<script src="scripts/cropper.min.js"></script> <script src="scripts/cropper.min.js"></script>
<script src="scripts/jquery-cropper.min.js"></script> <script src="scripts/jquery-cropper.min.js"></script>
<script type="module" src="scripts/eventemitter.js"></script>
<script type="module" src="scripts/power-user.js"></script> <script type="module" src="scripts/power-user.js"></script>
<script type="module" src="scripts/swiped-events.js"></script> <script type="module" src="scripts/swiped-events.js"></script>
<link rel="stylesheet" type="text/css" href="style.css"> <link rel="stylesheet" type="text/css" href="style.css">

View File

@ -107,7 +107,7 @@ import {
} from "./scripts/poe.js"; } from "./scripts/poe.js";
import { debounce, delay, restoreCaretPosition, saveCaretPosition, end_trim_to_sentence } from "./scripts/utils.js"; import { debounce, delay, restoreCaretPosition, saveCaretPosition, end_trim_to_sentence } from "./scripts/utils.js";
import { extension_settings, getContext, loadExtensionSettings } from "./scripts/extensions.js"; import { extension_settings, loadExtensionSettings } from "./scripts/extensions.js";
import { executeSlashCommands, getSlashCommandsHelp, registerSlashCommand } from "./scripts/slash-commands.js"; import { executeSlashCommands, getSlashCommandsHelp, registerSlashCommand } from "./scripts/slash-commands.js";
import { import {
tag_map, tag_map,
@ -126,6 +126,7 @@ import {
writeSecret writeSecret
} from "./scripts/secrets.js"; } from "./scripts/secrets.js";
import uniqolor from "./scripts/uniqolor.js"; import uniqolor from "./scripts/uniqolor.js";
import { EventEmitter } from './scripts/eventemitter.js';
//exporting functions and vars for mods //exporting functions and vars for mods
export { export {
@ -383,6 +384,12 @@ const system_messages = {
}, },
}; };
export const event_types = {
EXTRAS_CONNECTED: 'extras_connected',
}
export const eventSource = new EventEmitter();
// refresh token // refresh token
$(document).ajaxError(function myErrorHandler(_, xhr) { $(document).ajaxError(function myErrorHandler(_, xhr) {
if (xhr.status == 403) { if (xhr.status == 403) {
@ -4489,6 +4496,8 @@ window["SillyTavern"].getContext = function () {
maxContext: Number(max_context), maxContext: Number(max_context),
chatMetadata: chat_metadata, chatMetadata: chat_metadata,
streamingProcessor, streamingProcessor,
eventSource: eventSource,
event_types: event_types,
addOneMessage: addOneMessage, addOneMessage: addOneMessage,
generate: Generate, generate: Generate,
getTokenCount: getTokenCount, getTokenCount: getTokenCount,

View File

@ -0,0 +1,71 @@
/* Polyfill indexOf. */
var indexOf;
if (typeof Array.prototype.indexOf === 'function') {
indexOf = function (haystack, needle) {
return haystack.indexOf(needle);
};
} else {
indexOf = function (haystack, needle) {
var i = 0, length = haystack.length, idx = -1, found = false;
while (i < length && !found) {
if (haystack[i] === needle) {
idx = i;
found = true;
}
i++;
}
return idx;
};
};
/* Polyfill EventEmitter. */
var EventEmitter = function () {
this.events = {};
};
EventEmitter.prototype.on = function (event, listener) {
if (typeof this.events[event] !== 'object') {
this.events[event] = [];
}
this.events[event].push(listener);
};
EventEmitter.prototype.removeListener = function (event, listener) {
var idx;
if (typeof this.events[event] === 'object') {
idx = indexOf(this.events[event], listener);
if (idx > -1) {
this.events[event].splice(idx, 1);
}
}
};
EventEmitter.prototype.emit = function (event) {
var i, listeners, length, args = [].slice.call(arguments, 1);
if (typeof this.events[event] === 'object') {
listeners = this.events[event].slice();
length = listeners.length;
for (i = 0; i < length; i++) {
listeners[i].apply(this, args);
}
}
};
EventEmitter.prototype.once = function (event, listener) {
this.on(event, function g () {
this.removeListener(event, g);
listener.apply(this, arguments);
});
};
export { EventEmitter }

View File

@ -1,4 +1,4 @@
import { callPopup, saveSettings, saveSettingsDebounced } from "../script.js"; import { callPopup, eventSource, event_types, saveSettings, saveSettingsDebounced } from "../script.js";
import { isSubsetOf } from "./utils.js"; import { isSubsetOf } from "./utils.js";
export { export {
getContext, getContext,
@ -162,6 +162,7 @@ async function connectToApi(baseUrl) {
const data = await getExtensionsResult.json(); const data = await getExtensionsResult.json();
modules = data.modules; modules = data.modules;
await activateExtensions(); await activateExtensions();
eventSource.emit(event_types.EXTRAS_CONNECTED, modules);
} }
updateStatus(getExtensionsResult.ok); updateStatus(getExtensionsResult.ok);

View File

@ -5,7 +5,9 @@ import {
hideSwipeButtons, hideSwipeButtons,
showSwipeButtons, showSwipeButtons,
callPopup, callPopup,
getRequestHeaders getRequestHeaders,
event_types,
eventSource
} from "../../../script.js"; } from "../../../script.js";
import { getApiUrl, getContext, extension_settings, defaultRequestArgs, modules } from "../../extensions.js"; import { getApiUrl, getContext, extension_settings, defaultRequestArgs, modules } from "../../extensions.js";
import { stringFormat, initScrollHeight, resetScrollHeight } from "../../utils.js"; import { stringFormat, initScrollHeight, resetScrollHeight } from "../../utils.js";
@ -611,6 +613,9 @@ jQuery(async () => {
initScrollHeight($("#sd_negative_prompt")); initScrollHeight($("#sd_negative_prompt"));
}) })
await loadSettings(); eventSource.on(event_types.EXTRAS_CONNECTED, async () => {
await Promise.all([loadSamplers(), loadModels()]);
});
await loadSettings();
}); });