mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add server events emitter
This commit is contained in:
parent
c36607be6f
commit
6b821409e0
34
index.d.ts
vendored
34
index.d.ts
vendored
@ -1,8 +1,36 @@
|
||||
import { UserDirectoryList, User } from "./src/users";
|
||||
import { CommandLineArguments } from "./src/command-line";
|
||||
import { CsrfSyncedToken } from "csrf-sync";
|
||||
import { EventEmitter } from 'node:events';
|
||||
import { CsrfSyncedToken } from 'csrf-sync';
|
||||
import { UserDirectoryList, User } from './src/users.js';
|
||||
import { CommandLineArguments } from './src/command-line.js';
|
||||
import { EVENT_NAMES } from './src/server-events.js';
|
||||
|
||||
/**
|
||||
* Event payload for SERVER_STARTED event.
|
||||
*/
|
||||
export interface ServerStartedEvent {
|
||||
/**
|
||||
* The URL the server is listening on.
|
||||
*/
|
||||
url: URL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map of all server events to their payload types.
|
||||
*/
|
||||
export interface ServerEventMap {
|
||||
[EVENT_NAMES.SERVER_STARTED]: [ServerStartedEvent];
|
||||
}
|
||||
|
||||
declare global {
|
||||
declare namespace NodeJS {
|
||||
export interface Process {
|
||||
/**
|
||||
* A global instance of the server events emitter.
|
||||
*/
|
||||
serverEvents: EventEmitter<ServerEventMap>;
|
||||
}
|
||||
}
|
||||
|
||||
declare namespace CookieSessionInterfaces {
|
||||
export interface CookieSessionObject {
|
||||
/**
|
||||
|
@ -20,6 +20,7 @@ import bodyParser from 'body-parser';
|
||||
import open from 'open';
|
||||
|
||||
// local library imports
|
||||
import { serverEvents, EVENT_NAMES } from './src/server-events.js';
|
||||
import { CommandLineParser } from './src/command-line.js';
|
||||
import { loadPlugins } from './src/plugin-loader.js';
|
||||
import {
|
||||
@ -348,6 +349,7 @@ async function postSetupTasks(result) {
|
||||
console.log('\n' + getSeparator(plainGoToLog.length) + '\n');
|
||||
|
||||
setupLogLevel();
|
||||
serverEvents.emit(EVENT_NAMES.SERVER_STARTED, { url: autorunUrl });
|
||||
}
|
||||
|
||||
/**
|
||||
|
21
src/server-events.js
Normal file
21
src/server-events.js
Normal file
@ -0,0 +1,21 @@
|
||||
import EventEmitter from 'node:events';
|
||||
import process from 'node:process';
|
||||
|
||||
/**
|
||||
* @typedef {import('../index').ServerEventMap} ServerEventMap
|
||||
* @type {EventEmitter<ServerEventMap>} The default event source.
|
||||
*/
|
||||
export const serverEvents = new EventEmitter();
|
||||
process.serverEvents = serverEvents;
|
||||
export default serverEvents;
|
||||
|
||||
/**
|
||||
* @enum {string}
|
||||
* @readonly
|
||||
*/
|
||||
export const EVENT_NAMES = Object.freeze({
|
||||
/**
|
||||
* Emitted when the server has started.
|
||||
*/
|
||||
SERVER_STARTED: 'server-started',
|
||||
});
|
Loading…
x
Reference in New Issue
Block a user