duu
This commit is contained in:
parent
88f618cadf
commit
8f9f5b5100
108 changed files with 2139 additions and 1500 deletions
|
|
@ -1,5 +1,7 @@
|
|||
"use strict";
|
||||
|
||||
const TARINAI_PUBLIC_EVENT_TYPES = new Set(["audio:play", "selection:changed", "tool:selected", "world:phase", "log:entry", "tool:placed"]);
|
||||
|
||||
class TarinaiEventBus {
|
||||
constructor() {
|
||||
this.handlers = new Map();
|
||||
|
|
@ -15,12 +17,28 @@ class TarinaiEventBus {
|
|||
list.add(handler);
|
||||
}
|
||||
|
||||
hasDirectListeners(type) {
|
||||
if (!type) return false;
|
||||
return Boolean(this.handlers.get(type)?.size || 0);
|
||||
}
|
||||
|
||||
hasWildcardListeners(type) {
|
||||
if (!type || !TARINAI_PUBLIC_EVENT_TYPES.has(String(type))) return false;
|
||||
return Boolean(this.handlers.get("*")?.size || 0);
|
||||
}
|
||||
|
||||
hasListeners(type) {
|
||||
if (!type) return false;
|
||||
return Boolean(this.hasDirectListeners(type) || this.hasWildcardListeners(type));
|
||||
}
|
||||
|
||||
emit(type, detail = {}) {
|
||||
if (!type) return null;
|
||||
const event = { type, detail: detail || {}, at: performance?.now?.() ?? Date.now() };
|
||||
const direct = this.handlers.get(type);
|
||||
const wildcard = TARINAI_PUBLIC_EVENT_TYPES.has(String(type)) ? this.handlers.get("*") : null;
|
||||
if (!(direct?.size || 0) && !(wildcard?.size || 0)) return null;
|
||||
const event = { type, detail: detail || {}, at: performance?.now?.() ?? Date.now() };
|
||||
if (direct) for (const handler of [...direct]) this.safeCall(handler, event);
|
||||
const wildcard = this.handlers.get("*");
|
||||
if (wildcard) for (const handler of [...wildcard]) this.safeCall(handler, event);
|
||||
return event;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue