Installations
npm install @d-fischer/typed-event-emitter
Developer Guide
Typescript
Yes
Module System
CommonJS, ESM
Node Version
18.17.1
NPM Version
9.6.7
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (73.37%)
Shell (15.95%)
JavaScript (10.68%)
Developer
d-fischer
Download Statistics
Total Downloads
633,187
Last Day
296
Last Week
1,771
Last Month
11,691
Last Year
156,680
GitHub Statistics
4 Stars
57 Commits
2 Forks
3 Watching
2 Branches
1 Contributors
Bundle Size
1.89 kB
Minified
643.00 B
Minified + Gzipped
Sponsor this package
Package Meta Information
Latest Version
3.3.3
Package Id
@d-fischer/typed-event-emitter@3.3.3
Unpacked Size
16.43 kB
Size
4.16 kB
File Count
12
NPM Version
9.6.7
Node Version
18.17.1
Publised On
10 Dec 2023
Total Downloads
Cumulative downloads
Total Downloads
633,187
Last day
-4.8%
296
Compared to previous day
Last week
-28%
1,771
Compared to previous week
Last month
-55%
11,691
Compared to previous month
Last year
12.8%
156,680
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
@d-fischer/typed-event-emitter
This module provides an alternative API to the well known event emitting interfaces used in the browser (DOM) or node.js. Instead of accepting arbitrary strings as the event name, this module forces you to register your events in your class. Consequently, the style of binding and emitting events differs a little, ensuring already at binding time that the events actually exist.
Install
1yarn add @d-fischer/typed-event-emitter 2# or 3npm install @d-fischer/typed-event-emitter
Usage
1import { EventEmitter } from '@d-fischer/typed-event-emitter'; 2 3class MyClass extends EventEmitter { 4 // register an event 5 onValueChanged = this.registerEvent<[newValue: number]>(); 6 7 private _value: number; 8 9 constructor(value: number) { 10 // initialize EventEmitter 11 super(); 12 13 this._value = value; 14 } 15 16 get value() { 17 return this._value; 18 } 19 20 set value(value: number) { 21 this._value = value; 22 // emit the event to all listeners 23 this.emit(this.onValueChanged, this._value); 24 } 25} 26 27const instance = new MyClass(); 28 29// listen to event 30instance.onValueChanged(newValue => { 31 console.log('Value changed', newValue); 32}); 33 34// invoke setter 35instance.value = 27;
First, the EventEmitter
is loaded from the module. Any class that shall emit
events, must extend that EventEmitter
. If your class has its own constructor,
make sure to call super()
.
Any events your class shall be able to emit must be registered in the form:
1onFooBar = this.registerEvent<[...argTypes]>();
Where onFooBar
can be any name (it doesn't need to begin with on) and
argTypes
must be a tuple representing the arguments of the function the
listeners must have. With this, you can see the signature your function must
have when you're about to bind a listener to that event.
To emit an event (only possible from within your event emitter), you have
to call this.emit(this.onFooBar, ...)
, where this.onFooBar
is the event to
emit and ...
any number of parameters, that will be passed to the listeners.
Internal events
You can also declare internal events as well as internal listeners.
The main advantage over just using regular listeners appears when you
write a library for other people to use. If an end user decides to clear
all events using instance.removeListener()
without any parameter
or with only the event binder to clear out a specific event,
internal listeners will survive that.
1import { EventEmitter } from '@d-fischer/typed-event-emitter'; 2 3class MyClass extends EventEmitter { 4 // register an event 5 onValueChanged = this.registerEvent<[newValue: number]>(); 6 7 private _value: number; 8 9 constructor(value: number) { 10 // initialize EventEmitter 11 super(); 12 13 this._value = value; 14 15 this.addInternalListener(this.onValueChanged, newValue => { 16 console.log('Value changed (internal)', newValue); 17 }); 18 } 19 20 get value() { 21 return this._value; 22 } 23 24 set value(value: number) { 25 this._value = value; 26 // emit the event to all listeners 27 this.emit(this.onValueChanged, this._value); 28 } 29} 30 31const instance = new MyClass(); 32 33// listen to event 34instance.onValueChanged(newValue => { 35 console.log('Value changed (external)', newValue); 36}); 37 38// invoke setter, shows internal & external logs 39instance.value = 27; 40 41// clear listeners for event 42instance.removeListener(instance.onValueChanged); 43 44// invoke setter, still shows internal log 45instance.value = 42;
JavaScript
Your JavaScript host (i.e., your browser, node.js, etc.) should support classes and inheritance in order to work correctly. The code shown above can also be written in JavaScript (node.js):
1const { EventEmitter } = require('@d-fischer/typed-event-emitter'); 2 3class MyClass extends EventEmitter { 4 constructor(value) { 5 // initialize EventEmitter 6 super(); 7 8 /* newValue: number */ 9 this.onValueChanged = this.registerEvent(); 10 11 this._value = value; 12 } 13 14 get value() { 15 return this._value; 16 } 17 18 set value(value) { 19 this._value = value; 20 this.emit(this.onValueChanged, this._value); 21 } 22} 23 24let instance = new MyClass(); 25instance.onValueChanged(newValue => { 26 console.log(`Value changed: ${newValue}`); 27}); 28 29instance.value = 27;
Node that the events are registered explicitly within the constructor. Make sure
to initialize them after calling super()
.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
SAST tool detected: CodeQL
Details
- Info: SAST configuration detected: CodeQL
- Warn: no pull requests merged into dev branch
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Warn: project license file does not contain an FSF or OSI license.
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/codeql-analysis.yml:1
- Warn: no topLevel permission defined: .github/workflows/node.yml:1
- Info: no jobLevel write permissions found
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/d-fischer/typed-event-emitter/codeql-analysis.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:26: update your workflow using https://app.stepsecurity.io/secureworkflow/d-fischer/typed-event-emitter/codeql-analysis.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/codeql-analysis.yml:34: update your workflow using https://app.stepsecurity.io/secureworkflow/d-fischer/typed-event-emitter/codeql-analysis.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/d-fischer/typed-event-emitter/node.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/d-fischer/typed-event-emitter/node.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.yml:32: update your workflow using https://app.stepsecurity.io/secureworkflow/d-fischer/typed-event-emitter/node.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.yml:34: update your workflow using https://app.stepsecurity.io/secureworkflow/d-fischer/typed-event-emitter/node.yml/main?enable=pin
- Info: 0 out of 7 GitHub-owned GitHubAction dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Score
4
/10
Last Scanned on 2025-01-06
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More