Skip to content

Commit

Permalink
Merge pull request #587 from nextcloud/fix/init-later
Browse files Browse the repository at this point in the history
  • Loading branch information
skjnldsv committed Mar 16, 2023
2 parents 2aef7e6 + 7fcd3a1 commit aefc866
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,44 @@ declare global {
}
}

let bus: EventBus | null = null

function getBus(): EventBus {
if ((typeof window.OC !== 'undefined') && window.OC._eventBus && typeof window._nc_event_bus === 'undefined') {
if (bus !== null) {
return bus
}

if (typeof window === 'undefined') {
// testing or SSR
return new Proxy({} as EventBus, {
get: () => {
return () => console.error('Window not available, EventBus can not be established!')
}
})
}

if (typeof window.OC !== 'undefined' && 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
}

// Either use an existing event bus instance or create one
if (typeof window._nc_event_bus !== 'undefined') {
return new ProxyBus(window._nc_event_bus)
if (typeof window?._nc_event_bus !== 'undefined') {
bus = new ProxyBus(window._nc_event_bus)
} else {
return window._nc_event_bus = new SimpleBus()
bus = window._nc_event_bus = new SimpleBus()
}
return bus
}

const bus = getBus()

/**
* Register an event listener
*
* @param name name of the event
* @param handler callback invoked for every matching event emitted on the bus
*/
export function subscribe(name: string, handler: (string) => void): void {
bus.subscribe(name, handler)
getBus().subscribe(name, handler)
}

/**
Expand All @@ -51,7 +65,7 @@ export function subscribe(name: string, handler: (string) => void): void {
* @param handler callback passed to `subscribed`
*/
export function unsubscribe(name: string, handler: (string) => void): void {
bus.unsubscribe(name, handler)
getBus().unsubscribe(name, handler)
}

/**
Expand All @@ -61,5 +75,5 @@ export function unsubscribe(name: string, handler: (string) => void): void {
* @param event event payload
*/
export function emit(name: string, event: object): void {
bus.emit(name, event)
getBus().emit(name, event)
}

0 comments on commit aefc866

Please sign in to comment.