import valid from "semver/functions/valid.js"; import major from "semver/functions/major.js"; class ProxyBus { bus; constructor(bus2) { if (typeof bus2.getVersion !== "function" || !valid(bus2.getVersion())) { console.warn("Proxying an event bus with an unknown or invalid version"); } else if (major(bus2.getVersion()) !== major(this.getVersion())) { console.warn( "Proxying an event bus of version " + bus2.getVersion() + " with " + this.getVersion() ); } this.bus = bus2; } getVersion() { return "3.3.2"; } subscribe(name, handler) { this.bus.subscribe(name, handler); } unsubscribe(name, handler) { this.bus.unsubscribe(name, handler); } emit(name, ...event) { this.bus.emit(name, ...event); } } class SimpleBus { handlers = /* @__PURE__ */ new Map(); getVersion() { return "3.3.2"; } subscribe(name, handler) { this.handlers.set( name, (this.handlers.get(name) || []).concat( handler ) ); } unsubscribe(name, handler) { this.handlers.set( name, (this.handlers.get(name) || []).filter((h) => h !== handler) ); } emit(name, ...event) { const handlers = this.handlers.get(name) || []; handlers.forEach((h) => { try { ; h(event[0]); } catch (e) { console.error("could not invoke event listener", e); } }); } } let bus = null; function getBus() { if (bus !== null) { return bus; } if (typeof window === "undefined") { return new Proxy({}, { get: () => { return () => console.error( "Window not available, EventBus can not be established!" ); } }); } if (window.OC?._eventBus && typeof window._nc_event_bus === "undefined") { console.warn( "found old event bus instance at OC._eventBus. Update your version!" ); window._nc_event_bus = window.OC._eventBus; } if (typeof window?._nc_event_bus !== "undefined") { bus = new ProxyBus(window._nc_event_bus); } else { bus = window._nc_event_bus = new SimpleBus(); } return bus; } function subscribe(name, handler) { getBus().subscribe(name, handler); } function unsubscribe(name, handler) { getBus().unsubscribe(name, handler); } function emit(name, ...event) { getBus().emit(name, ...event); } export { ProxyBus, SimpleBus, emit, subscribe, unsubscribe };