snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
This commit is contained in:
113
packages/emitter/CHANGELOG.md
Normal file
113
packages/emitter/CHANGELOG.md
Normal file
@@ -0,0 +1,113 @@
|
||||
# Change Log
|
||||
|
||||
## 0.33.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- [#2072](https://github.com/RocketChat/fuselage/pull/2072) [`46e9cc3`](https://github.com/RocketChat/fuselage/commit/46e9cc36b03bcdd8668f6146dd6e326857416a39) Thanks [@tassoevan](https://github.com/tassoevan)! - feat: Remove unused UMD builds from all packages
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- [#2060](https://github.com/RocketChat/fuselage/pull/2060) [`ee381bb`](https://github.com/RocketChat/fuselage/commit/ee381bb32982d0f4ed9ef26385b5016e0a2e4023) Thanks [@tassoevan](https://github.com/tassoevan)! - feat: Remove deprecations and keep documentation comments
|
||||
|
||||
## 0.32.0
|
||||
|
||||
### Minor Changes
|
||||
|
||||
- [#1717](https://github.com/RocketChat/fuselage/pull/1717) [`1179d3f`](https://github.com/RocketChat/fuselage/commit/1179d3f3fb954f00741fd99ddd979b00f39c937e) Thanks [@tassoevan](https://github.com/tassoevan)! - feat: ES2024 standard requirement
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||
|
||||
# [0.31.0](https://github.com/RocketChat/fuselage/compare/v0.30.1...v0.31.0) (2021-12-28)
|
||||
|
||||
### Features
|
||||
|
||||
- New hooks for element size tracking ([#413](https://github.com/RocketChat/fuselage/issues/413)) ([8ca682c](https://github.com/RocketChat/fuselage/commit/8ca682c636d2e4813f7d346cb881513382be63cf))
|
||||
|
||||
# [0.30.0](https://github.com/RocketChat/fuselage/compare/v0.29.0...v0.30.0) (2021-10-06)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- **jest:** Adjust jest and ts-jest dependencies ([#547](https://github.com/RocketChat/fuselage/issues/547)) ([91a4fa1](https://github.com/RocketChat/fuselage/commit/91a4fa1365394001afe1bd46480bda3bafed5505))
|
||||
|
||||
# [0.29.0](https://github.com/RocketChat/fuselage/compare/v0.28.0...v0.29.0) (2021-08-31)
|
||||
|
||||
**Note:** Version bump only for package @rocket.chat/emitter
|
||||
|
||||
# [0.28.0](https://github.com/RocketChat/fuselage/compare/v0.27.0...v0.28.0) (2021-07-30)
|
||||
|
||||
### Features
|
||||
|
||||
- **onboarding-ui:** Administrator information form and Organization information form ([#489](https://github.com/RocketChat/fuselage/issues/489)) ([b289f68](https://github.com/RocketChat/fuselage/commit/b289f68676954b91c792d8d97680314178bf2c60))
|
||||
- styled API; monorepo grooming ([#482](https://github.com/RocketChat/fuselage/issues/482)) ([1b6b70c](https://github.com/RocketChat/fuselage/commit/1b6b70cf67ec16927b1566adc2350295a8927223))
|
||||
|
||||
# [0.27.0](https://github.com/RocketChat/fuselage/compare/v0.26.0...v0.27.0) (2021-06-28)
|
||||
|
||||
**Note:** Version bump only for package @rocket.chat/emitter
|
||||
|
||||
# [0.26.0](https://github.com/RocketChat/fuselage/compare/v0.25.0...v0.26.0) (2021-05-28)
|
||||
|
||||
**Note:** Version bump only for package @rocket.chat/emitter
|
||||
|
||||
# [0.25.0](https://github.com/RocketChat/fuselage/compare/v0.24.0...v0.25.0) (2021-05-19)
|
||||
|
||||
### Features
|
||||
|
||||
- [@rocket](https://github.com/rocket).chat/message-parser ([#443](https://github.com/RocketChat/fuselage/issues/443)) ([4722cdf](https://github.com/RocketChat/fuselage/commit/4722cdff46f5987f335d989be59649c7652bb12a))
|
||||
|
||||
# [0.24.0](https://github.com/RocketChat/fuselage/compare/v0.23.0...v0.24.0) (2021-04-28)
|
||||
|
||||
### Features
|
||||
|
||||
- [@rocket](https://github.com/rocket).chat/string-helpers ([#431](https://github.com/RocketChat/fuselage/issues/431)) ([2509d6a](https://github.com/RocketChat/fuselage/commit/2509d6acdbe5ec8b216e8d4430373797c5f5dfe2))
|
||||
|
||||
# [0.23.0](https://github.com/RocketChat/fuselage/compare/v0.22.0...v0.23.0) (2021-04-01)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- **npm:** Wrong paths in "files" field of package.json ([6d3c811](https://github.com/RocketChat/fuselage/commit/6d3c811f6fd747de7f47aff145902d88476272ee))
|
||||
|
||||
# [0.22.0](https://github.com/RocketChat/fuselage/compare/v0.21.0...v0.22.0) (2021-02-26)
|
||||
|
||||
### Features
|
||||
|
||||
- Type Emitter Implementation ([#399](https://github.com/RocketChat/fuselage/issues/399)) ([cd0d3fc](https://github.com/RocketChat/fuselage/commit/cd0d3fc7b50d911dbacbc122d09745fa9fb4d921))
|
||||
|
||||
# [0.21.0](https://github.com/RocketChat/fuselage/compare/v0.20.3...v0.21.0) (2021-01-31)
|
||||
|
||||
### Features
|
||||
|
||||
- Built modules for design tokens ([#356](https://github.com/RocketChat/fuselage/issues/356)) ([f9c3449](https://github.com/RocketChat/fuselage/commit/f9c344953b8161a4385cab3a3dcc8b6a7210446f))
|
||||
|
||||
## [0.20.1](https://github.com/RocketChat/fuselage/compare/v0.20.0...v0.20.1) (2020-12-22)
|
||||
|
||||
**Note:** Version bump only for package @rocket.chat/emitter
|
||||
|
||||
# [0.20.0](https://github.com/RocketChat/fuselage/compare/v0.19.0...v0.20.0) (2020-12-21)
|
||||
|
||||
**Note:** Version bump only for package @rocket.chat/emitter
|
||||
|
||||
# [0.19.0](https://github.com/RocketChat/fuselage/compare/v0.18.0...v0.19.0) (2020-11-28)
|
||||
|
||||
**Note:** Version bump only for package @rocket.chat/emitter
|
||||
|
||||
# [0.18.0](https://github.com/RocketChat/fuselage/compare/v0.17.3...v0.18.0) (2020-11-16)
|
||||
|
||||
**Note:** Version bump only for package @rocket.chat/emitter
|
||||
|
||||
## [0.17.2](https://github.com/RocketChat/fuselage/compare/v0.17.1...v0.17.2) (2020-10-28)
|
||||
|
||||
**Note:** Version bump only for package @rocket.chat/emitter
|
||||
|
||||
## [0.17.1](https://github.com/RocketChat/fuselage/compare/v0.17.0...v0.17.1) (2020-10-26)
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
- Emitter: Created events method ([#311](https://github.com/RocketChat/fuselage/issues/311)) ([e36240f](https://github.com/RocketChat/fuselage/commit/e36240f396a33bd68a2d71c6b00e845e6e22604d))
|
||||
|
||||
# [0.17.0](https://github.com/RocketChat/fuselage/compare/v0.16.0...v0.17.0) (2020-10-25)
|
||||
|
||||
### Features
|
||||
|
||||
- Event Emitter ([#300](https://github.com/RocketChat/fuselage/issues/300)) ([4ec074c](https://github.com/RocketChat/fuselage/commit/4ec074c17b311b2ac3caaa4fa01360cb2b65cefe))
|
||||
6
packages/emitter/jest.config.ts
Normal file
6
packages/emitter/jest.config.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import server from '@rocket.chat/jest-presets/server';
|
||||
import type { Config } from 'jest';
|
||||
|
||||
export default {
|
||||
preset: server.preset,
|
||||
} satisfies Config;
|
||||
50
packages/emitter/package.json
Normal file
50
packages/emitter/package.json
Normal file
@@ -0,0 +1,50 @@
|
||||
{
|
||||
"name": "@rocket.chat/emitter",
|
||||
"version": "0.33.0",
|
||||
"description": "Event Emitter by Rocket.Chat",
|
||||
"keywords": [
|
||||
"rocket.chat"
|
||||
],
|
||||
"bugs": {
|
||||
"url": "https://github.com/RocketChat/Rocket.Chat/issues"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/RocketChat/Rocket.Chat.git",
|
||||
"directory": "packages/emitter"
|
||||
},
|
||||
"license": "MIT",
|
||||
"author": {
|
||||
"name": "Rocket.Chat",
|
||||
"url": "https://rocket.chat/"
|
||||
},
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.module.js",
|
||||
"source": "src/index.ts",
|
||||
"types": "dist/index.d.ts",
|
||||
"files": [
|
||||
"/dist"
|
||||
],
|
||||
"scripts": {
|
||||
"build": "rollup -c",
|
||||
"lint": "eslint .",
|
||||
"lint:fix": "eslint --fix .",
|
||||
"test": "jest",
|
||||
"testunit": "jest",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rocket.chat/jest-presets": "workspace:~",
|
||||
"@rocket.chat/tsconfig": "workspace:~",
|
||||
"@rollup/plugin-terser": "~1.0.0",
|
||||
"@rollup/plugin-typescript": "~12.3.0",
|
||||
"eslint": "~9.39.5",
|
||||
"jest": "~30.2.0",
|
||||
"rollup": "~4.62.5",
|
||||
"ts-jest": "~29.4.12",
|
||||
"typescript": "~5.9.3"
|
||||
},
|
||||
"volta": {
|
||||
"extends": "../../package.json"
|
||||
}
|
||||
}
|
||||
32
packages/emitter/rollup.config.mjs
Normal file
32
packages/emitter/rollup.config.mjs
Normal file
@@ -0,0 +1,32 @@
|
||||
import terser from '@rollup/plugin-terser';
|
||||
import typescript from '@rollup/plugin-typescript';
|
||||
import { defineConfig } from 'rollup';
|
||||
|
||||
import pkg from './package.json' with { type: 'json' };
|
||||
|
||||
export default defineConfig({
|
||||
input: 'src/index.ts',
|
||||
output: [
|
||||
{
|
||||
file: pkg.main,
|
||||
format: 'cjs',
|
||||
sourcemap: true,
|
||||
},
|
||||
{
|
||||
file: pkg.module,
|
||||
format: 'es',
|
||||
sourcemap: true,
|
||||
},
|
||||
],
|
||||
plugins: [
|
||||
typescript({
|
||||
tsconfig: './tsconfig.build.json',
|
||||
}),
|
||||
terser({
|
||||
module: true,
|
||||
output: {
|
||||
comments: false,
|
||||
},
|
||||
}),
|
||||
],
|
||||
});
|
||||
102
packages/emitter/src/emitter.spec.ts
Normal file
102
packages/emitter/src/emitter.spec.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
import { Emitter } from './index';
|
||||
|
||||
const times = (n: number, fn: () => void): number => {
|
||||
Array.from({ length: n }, () => fn());
|
||||
return n;
|
||||
};
|
||||
|
||||
let handler: () => void;
|
||||
let emitter: Emitter;
|
||||
|
||||
beforeEach(() => {
|
||||
handler = jest.fn();
|
||||
emitter = new Emitter();
|
||||
});
|
||||
|
||||
describe('`on` method', () => {
|
||||
it('should call `test` handler 5 times - with only one listener', () => {
|
||||
emitter.on('test', handler);
|
||||
times(5, () => emitter.emit('test'));
|
||||
expect(handler).toHaveBeenCalledTimes(5);
|
||||
});
|
||||
|
||||
it('should call `test2` handler 0 times - with multiple listeners', () => {
|
||||
emitter.on('test', () => null);
|
||||
emitter.on('test2', handler);
|
||||
times(5, () => emitter.emit('test'));
|
||||
expect(handler).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('`once` method', () => {
|
||||
it('should call `test` handler only once', () => {
|
||||
emitter.once('test', handler);
|
||||
times(5, () => emitter.emit('test'));
|
||||
expect(handler).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should call `test` handler only once - with multiple events and same handler', () => {
|
||||
emitter.once('test', handler);
|
||||
emitter.once('test2', handler);
|
||||
times(5, () => emitter.emit('test'));
|
||||
expect(handler).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should call `test` handler 5 times afert `once + remove + on` same event', () => {
|
||||
emitter.once('test', handler);
|
||||
emitter.off('test', handler);
|
||||
emitter.on('test', handler);
|
||||
times(5, () => emitter.emit('test'));
|
||||
expect(handler).toHaveBeenCalledTimes(5);
|
||||
});
|
||||
|
||||
it('should call `test` handler once after add multiple `once` and remove n-1', () => {
|
||||
emitter.once('test', handler);
|
||||
emitter.once('test', handler)();
|
||||
emitter.once('test', handler)();
|
||||
emitter.once('test', handler)();
|
||||
emitter.once('test', handler)();
|
||||
emitter.once('test', handler)();
|
||||
times(5, () => emitter.emit('test'));
|
||||
expect(handler).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should call `test` handler once after add same handler with different `once` events and remove n-1', () => {
|
||||
emitter.once('test', handler);
|
||||
emitter.once('test2', handler)();
|
||||
times(5, () => emitter.emit('test'));
|
||||
expect(handler).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('`off` method', () => {
|
||||
it('should have no `test` handler after removal', () => {
|
||||
emitter.on('test', handler);
|
||||
emitter.off('test', handler);
|
||||
expect(emitter.has('test')).toBe(false);
|
||||
});
|
||||
|
||||
it('should have no `test` handler after use stop callback', () => {
|
||||
emitter.on('test', handler)();
|
||||
expect(emitter.has('test')).toBe(false);
|
||||
});
|
||||
|
||||
it('should have no `test` handler after emit once', () => {
|
||||
emitter.once('test', handler);
|
||||
emitter.emit('test');
|
||||
expect(emitter.has('test')).toBe(false);
|
||||
});
|
||||
|
||||
it('should remove only the specified handler', () => {
|
||||
const handler = jest.fn();
|
||||
const unusedHandler = jest.fn();
|
||||
|
||||
emitter.on('test', handler);
|
||||
emitter.off('test', unusedHandler);
|
||||
|
||||
emitter.emit('test');
|
||||
|
||||
expect(handler).toHaveBeenCalledTimes(1);
|
||||
expect(unusedHandler).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
});
|
||||
161
packages/emitter/src/index.ts
Normal file
161
packages/emitter/src/index.ts
Normal file
@@ -0,0 +1,161 @@
|
||||
/** @public */
|
||||
export type DefaultEventMap = Record<string | symbol, any>;
|
||||
|
||||
/** @public */
|
||||
export type AnyEventTypeOf<EventMap extends DefaultEventMap> = keyof EventMap;
|
||||
|
||||
/** @public */
|
||||
export type AnyEventOf<EventMap extends DefaultEventMap> = EventMap[keyof EventMap];
|
||||
|
||||
/** @public */
|
||||
export type AnyEventHandlerOf<EventMap extends DefaultEventMap> = {
|
||||
[EventType in keyof EventMap]: EventMap[EventType] extends void ? () => void : (event: EventMap[EventType]) => void;
|
||||
}[keyof EventMap];
|
||||
|
||||
/** @public */
|
||||
export type EventTypeOf<EventMap extends DefaultEventMap, EventValue extends EventMap[keyof EventMap]> = {
|
||||
[EventType in keyof EventMap]: EventMap[EventType] extends EventValue ? EventType : never;
|
||||
}[keyof EventMap];
|
||||
|
||||
/** @public */
|
||||
export type EventOf<EventMap extends DefaultEventMap, EventType extends AnyEventTypeOf<EventMap>> = EventMap[EventType] extends void
|
||||
? never
|
||||
: EventMap[EventType];
|
||||
|
||||
/** @public */
|
||||
export type EventHandlerOf<EventMap extends DefaultEventMap, EventType extends AnyEventTypeOf<EventMap>> = EventMap[EventType] extends void
|
||||
? () => void
|
||||
: (event: EventMap[EventType]) => void;
|
||||
|
||||
/** @public */
|
||||
export type OffCallbackHandler = () => void;
|
||||
|
||||
/** @public */
|
||||
export interface IEmitter<EventMap extends DefaultEventMap = DefaultEventMap> {
|
||||
on<T extends AnyEventOf<EventMap>, EventType extends AnyEventTypeOf<EventMap> = EventTypeOf<EventMap, T>>(
|
||||
type: EventType,
|
||||
handler: EventHandlerOf<EventMap, EventType>,
|
||||
): OffCallbackHandler;
|
||||
once<T extends AnyEventOf<EventMap>, EventType extends AnyEventTypeOf<EventMap> = EventTypeOf<EventMap, T>>(
|
||||
type: EventType,
|
||||
handler: EventHandlerOf<EventMap, EventType>,
|
||||
): OffCallbackHandler;
|
||||
off<T extends AnyEventOf<EventMap>, EventType extends AnyEventTypeOf<EventMap> = EventTypeOf<EventMap, T>>(
|
||||
type: EventType,
|
||||
handler: EventHandlerOf<EventMap, EventType>,
|
||||
): void;
|
||||
emit<T extends AnyEventOf<EventMap>, EventType extends AnyEventTypeOf<EventMap> = EventTypeOf<EventMap, T>>(
|
||||
type: EventType,
|
||||
...[event]: EventOf<EventMap, EventType> extends void ? [undefined?] : [EventOf<EventMap, EventType>]
|
||||
): void;
|
||||
has(key: AnyEventTypeOf<EventMap>): boolean;
|
||||
events(): AnyEventTypeOf<EventMap>[];
|
||||
}
|
||||
|
||||
const once = Symbol('once');
|
||||
const evts = Symbol('evts');
|
||||
|
||||
/**
|
||||
* The event emitter class.
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export class Emitter<EventMap extends DefaultEventMap = DefaultEventMap> implements IEmitter<EventMap> {
|
||||
private [evts] = new Map<AnyEventTypeOf<EventMap>, AnyEventHandlerOf<EventMap>[]>();
|
||||
|
||||
private [once] = new WeakMap<AnyEventHandlerOf<EventMap>, number>();
|
||||
|
||||
/**
|
||||
* Returns the whole EventType list
|
||||
*/
|
||||
events(): AnyEventTypeOf<EventMap>[] {
|
||||
return Array.from(this[evts].keys());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if this emmiter has a listener attached to the `key` event type
|
||||
*/
|
||||
has(key: AnyEventTypeOf<EventMap>): boolean {
|
||||
return this[evts].has(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the `handler` function to listen events of the `type` type.
|
||||
*
|
||||
* @returns a function to unsubscribe the handler invoking `this.off(type, handler)`
|
||||
*/
|
||||
on<T extends AnyEventOf<EventMap>, TType extends AnyEventTypeOf<EventMap> = EventTypeOf<EventMap, T>>(
|
||||
type: TType,
|
||||
handler: EventHandlerOf<EventMap, TType>,
|
||||
): OffCallbackHandler;
|
||||
|
||||
on(type: keyof EventMap, handler: (...args: any[]) => void) {
|
||||
const handlers = this[evts].get(type) ?? [];
|
||||
handlers.push(handler);
|
||||
this[evts].set(type, handlers);
|
||||
return () => this.off(type, handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a *one-time* `handler` function for the event of the `type` type.
|
||||
*
|
||||
* @returns a function to unsubscribe the handler invoking `this.off(type, handler)`
|
||||
*/
|
||||
once<T extends AnyEventOf<EventMap>, EventType extends AnyEventTypeOf<EventMap> = EventTypeOf<EventMap, T>>(
|
||||
type: EventType,
|
||||
handler: EventHandlerOf<EventMap, EventType>,
|
||||
): OffCallbackHandler;
|
||||
|
||||
once(type: keyof EventMap, handler: (...args: any[]) => void) {
|
||||
const counter = this[once].get(handler) || 0;
|
||||
this[once].set(handler, counter + 1);
|
||||
return this.on(type, handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the specified `handler` from the list of handlers of the event of the `type` type
|
||||
*/
|
||||
off<T extends AnyEventOf<EventMap>, EventType extends AnyEventTypeOf<EventMap> = EventTypeOf<EventMap, T>>(
|
||||
type: EventType,
|
||||
handler: EventHandlerOf<EventMap, EventType>,
|
||||
): void;
|
||||
|
||||
off(type: keyof EventMap, handler: (...args: any[]) => void) {
|
||||
const handlers = this[evts].get(type);
|
||||
if (!handlers) {
|
||||
return;
|
||||
}
|
||||
|
||||
const counter = this[once].get(handler) ?? 0;
|
||||
if (counter > 1) {
|
||||
this[once].set(handler, counter - 1);
|
||||
} else {
|
||||
this[once].delete(handler);
|
||||
}
|
||||
|
||||
handlers.splice(handlers.findIndex((callback) => callback === handler) >>> 0, 1);
|
||||
|
||||
if (handlers.length === 0) {
|
||||
this[evts].delete(type);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls each of the handlers registered for the event of `type` type, in the
|
||||
* order they were registered, passing the supplied argument `e` to each.
|
||||
*/
|
||||
emit<T extends AnyEventOf<EventMap>, EventType extends AnyEventTypeOf<EventMap> = EventTypeOf<EventMap, T>>(
|
||||
type: EventType,
|
||||
...[event]: EventOf<EventMap, EventType> extends void ? [undefined?] : [EventOf<EventMap, EventType>]
|
||||
): void;
|
||||
|
||||
emit(type: keyof EventMap, ...[event]: any[]) {
|
||||
[...(this[evts].get(type) ?? [])].forEach((handler) => {
|
||||
handler(event);
|
||||
|
||||
if (this[once].get(handler)) {
|
||||
this.off(type, handler);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
8
packages/emitter/tsconfig.build.json
Normal file
8
packages/emitter/tsconfig.build.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"declaration": true,
|
||||
"declarationMap": true
|
||||
},
|
||||
"exclude": ["src/**/*.spec.*", "jest.config.*"]
|
||||
}
|
||||
10
packages/emitter/tsconfig.json
Normal file
10
packages/emitter/tsconfig.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"extends": "@rocket.chat/tsconfig/base.json",
|
||||
"compilerOptions": {
|
||||
"target": "es2024",
|
||||
"lib": ["ES2024"],
|
||||
|
||||
"rootDirs": ["./src", "./"],
|
||||
"outDir": "./dist"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user