Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@nextcloud/event-bus

nextcloud56.1kGPL-3.0-or-later3.3.2TypeScript support: included

A simple event bus to communicate between Nextcloud components.

nextcloud

readme

@nextcloud/event-bus

REUSE status Build Status Code coverage npm Documentation

A simple event bus to communicate between Nextcloud components.

Installation

npm install @nextcloud/event-bus --save
yarn add @nextcloud/event-bus

Usage

import { emit, subscribe, unsubscribe } from '@nextcloud/event-bus'

const h = (e) => console.info(e)

subscribe('a', h)
subscribe('b', h)

emit('a', {
    data: 123,
})

unsubscribe('a', h)
unsubscribe('b', h)

Typed events

It is also possible to type events, which allows type infering on the event-bus methods like emit, subscribe and unsubscribe. To register new events, simply extend the NextcloudEvents interface:

  1. Create a file like event-bus.d.ts:
declare module '@nextcloud/event-bus' {
    interface NextcloudEvents {
        'example-app:awesomeness:increased': { level: number }
    }
}

export {}
  1. Now if you use any of the event bus functions, the parameters will automatically be typed correctly:
import { subscribe } from '@nextcloud/event-bus'

subscribe('example-app:awesomeness:increased', (event) => {
    // "event" automatically infers type { level: number}
    console.log(event.level)
})

Naming convention

To stay consistent, we encourage you to use the following syntax when declaring events

app-id:object:verb

Examples:

  • nextcloud:unified-search:closed
  • files:node:uploading
  • files:node:uploaded
  • files:node:deleted
  • contacts:contact:deleted
  • calendar:event:created
  • forms:answer:updated

Development

npm install

npm run build
npm run test

Requirements

changelog

Changelog

All notable changes to this project will be documented in this file.

v3.3.2 (2025-02-28)

Fixed

  • fix: Allow events without parameters #792 (susnux)
  • fix: Resolve typos in doc strings #862 (susnux)

Changed

v3.3.1 (2024-05-28)

Fixed

  • fix: Do not include declarations for private globals in distribution #772 (susnux)

Changed

  • chore: Add license information to files and add REUSE workflow #773 (susnux)

v3.3.0 (2024-05-07)

Added

  • feat: Allow to fully type events by extending the NextcloudEvents interface #755 (susnux)

Fixed

  • fix(readme): update engine requirements according to package.json #754 (AaronActu)
  • fix(docs): Adjust invalid example for typed events #763 (susnux)

Changed

  • feat: Add ESLint for linting and prettier for formatting #759 (susnux)
  • feat: Migrate to vite for building and vitest for testing #758 (susnux)

v3.2.0 (2024-04-21)

Changed

Dependencies

  • chore(deps-dev): Bump @rollup/plugin-replace to 5.0.3
  • chore(deps-dev): Bump @rollup/plugin-typescript to 11.1.5
  • chore(deps-dev): Bump @types/node to 20.4.1
  • chore(deps-dev): Bump @types/semver to 7.5.3
  • chore(deps-dev): Bump jest to 29.7.0
  • chore(deps-dev): Bump jest-environment-jsdom to 29.7.0
  • chore(deps-dev): Bump rollup to 4.0.2
  • chore(deps-dev): Bump semver to 7.5.4
  • chore(deps-dev): Bump tslib to 2.6.2
  • chore(deps-dev): Bump typedoc to 0.25.2
  • chore(deps-dev): Bump typescript to 5.2.2
  • chore(deps-dev): Bump word-wrap to 1.2.4
  • chore(deps): Bump @types/node to 20.8.6
  • chore(deps): Bump semver to 7.5.3
  • chore(deps): Bump tough-cookie to 4.1.3

v3.1.0 (2023-05-17)

Full Changelog

Changed

  • Updated dependencies
  • Initialize bus on first usage not on import #587 (susnux)

Fixed

  • Allow package to be imported by native Node ES module projects #586 (susnux)

v3.0.2 (2022-09-05)

Full Changelog

Closed pull requests

3.0.1 - 2022-08-24

⚠️ deprecated

Changed

  • Update dependencies
  • Improve native ESM Support

3.0.0 - 2022-05-02

Changed

  • Update dependencies
  • Fix rollup build
  • Remove babel
    • Typescript already is able to generate code that is in compliance with es5
  • Remove package.json from generated code.
    • Use @rollup/plugin-replace to handle this.
  • Add ESM support
    • Improve native usage support
    • Improve bundle size
  • Fix invalid typescript typings export
  • Remove unnecessary files from npm package

2.1.1 – 2021-11-02

Changed

  • Dependency updates

2.1.0 – 2021-09-28

Changed

  • Dependency updates

Fixed

  • Readme examples
  • Dependency updates

2.0.0 - 2021-05-05

Breaking

Changed

  • Move a single to transpiled library bundle
  • Dependency updates

1.3.0 - 2021-05-05

Changed

  • Move a single to transpiled library bundle
  • Dependency updates

1.2.0 - 2020-06-03

Changed

  • Dependency updates

1.1.4 - 2020-04-06

Changed

  • Dependency updates

Fixed

  • Update vulnerable packages

1.1.3 - 2020-03-19

Changed

  • Dependency updates

Fixed

  • Update vulnerable packages

1.1.2 - 2020-01-10

Fixed

  • Packaging of corejs

1.1.1 - 2020-01-10

Fixed

  • Module error with semver package

1.0.1 - 2020-01-07

Fixed

  • Misleading warning if ProxyBus is used for a compatible bus