Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,4 @@ export default defineConfig([
},

...recommendedLibrary,

{
files: ['lib/**/*.ts'],
rules: {
// requires the logger to work in no-window environments
'no-console': 'off',
},
},
])
8 changes: 3 additions & 5 deletions lib/ProxyBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@ import type { IsUndefined } from './types.ts'

import major from 'semver/functions/major.js'
import valid from 'semver/functions/valid.js'
import { logger } from './logger.ts'

export class ProxyBus<E extends GenericEvents = NextcloudEvents>
implements EventBus<E> {
private bus: EventBus<E>

constructor(bus: EventBus<E>) {
if (typeof bus.getVersion !== 'function' || !valid(bus.getVersion())) {
console.warn('Proxying an event bus with an unknown or invalid version')
logger.warn('Proxying an event bus with an unknown or invalid version')
} else if (major(bus.getVersion()) !== major(this.getVersion())) {
console.warn('Proxying an event bus of version '
+ bus.getVersion()
+ ' with '
+ this.getVersion())
logger.warn(`Proxying an event bus of version ${bus.getVersion()} with ${this.getVersion()}`)
}

this.bus = bus
Expand Down
6 changes: 4 additions & 2 deletions lib/SimpleBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type { EventBus } from './EventBus.js'
import type { EventHandler } from './EventHandler.js'
import type { IsUndefined } from './types.ts'

import { logger } from './logger.ts'

export class SimpleBus<E extends GenericEvents = NextcloudEvents>
implements EventBus<E> {
private handlers = new Map<keyof E, EventHandler<E[keyof E]>[]>()
Expand Down Expand Up @@ -44,8 +46,8 @@ implements EventBus<E> {
handlers.forEach((h) => {
try {
(h as EventHandler<(typeof event)[0]>)(event[0])
} catch (e) {
console.error('could not invoke event listener', e)
} catch (error) {
logger.error('SimpleBus: Could not invoke event listener', { error })
}
})
}
Expand Down
5 changes: 3 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { EventBus } from './EventBus.ts'
import type { EventHandler } from './EventHandler.ts'
import type { IsUndefined } from './types.ts'

import { logger } from './logger.ts'
import { ProxyBus } from './ProxyBus.ts'
import { SimpleBus } from './SimpleBus.ts'

Expand All @@ -34,13 +35,13 @@ function getBus(): EventBus {
// testing or SSR
return new Proxy({} as EventBus, {
get: () => {
return () => console.error('Window not available, EventBus can not be established!')
return () => logger.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!')
logger.warn('Found old event bus instance at OC._eventBus. Update your version!')
window._nc_event_bus = window.OC._eventBus
}

Expand Down
10 changes: 10 additions & 0 deletions lib/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*!
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/

import { getLoggerBuilder } from '@nextcloud/logger'

export const logger = getLoggerBuilder()

Check failure on line 8 in lib/logger.ts

View workflow job for this annotation

GitHub Actions / node-tests

test/index.test.ts

ReferenceError: OC is not defined ❯ new LoggerBuilder node_modules/@nextcloud/logger/lib/LoggerBuilder.ts:18:30 ❯ getLoggerBuilder node_modules/@nextcloud/logger/lib/index.ts:9:12 ❯ lib/logger.ts:8:23 ❯ lib/index.ts:11:1

Check failure on line 8 in lib/logger.ts

View workflow job for this annotation

GitHub Actions / node-tests

test/SimpleBus.test.ts

ReferenceError: OC is not defined ❯ new LoggerBuilder node_modules/@nextcloud/logger/lib/LoggerBuilder.ts:18:30 ❯ getLoggerBuilder node_modules/@nextcloud/logger/lib/index.ts:9:12 ❯ lib/logger.ts:8:23 ❯ lib/SimpleBus.ts:11:1

Check failure on line 8 in lib/logger.ts

View workflow job for this annotation

GitHub Actions / node-tests

test/ProxyBus.test.ts

ReferenceError: OC is not defined ❯ new LoggerBuilder node_modules/@nextcloud/logger/lib/LoggerBuilder.ts:18:30 ❯ getLoggerBuilder node_modules/@nextcloud/logger/lib/index.ts:9:12 ❯ lib/logger.ts:8:23 ❯ lib/ProxyBus.ts:13:1
.setApp('@nextcloud/event-bus')
.build()
59 changes: 59 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"watch": "npm run dev -- --watch"
},
"dependencies": {
"@nextcloud/logger": "^2.2.1",
"@types/semver": "^7.7.0",
"semver": "^7.7.2"
},
Expand Down
4 changes: 2 additions & 2 deletions test/SimpleBus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('SimpleBus', () => {
bus.emit('test', 'the message')

expect(consoleError).toBeCalledTimes(1)
expect(consoleError.mock.calls[0]?.[0]).toMatch(/could not invoke event listener/)
expect(consoleError.mock.calls[0]?.[1]).toBe(error)
expect(consoleError.mock.calls[0]?.[0]).toMatchInlineSnapshot('"SimpleBus: Could not invoke event listener"')
expect(consoleError.mock.calls[0]?.[1]).toEqual({ error })
})
})
Loading