-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
71 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
const { EventEmitter } = require("events"); | ||
|
||
module.exports = class Events extends EventEmitter { | ||
|
||
static emitted = Symbol("emitted"); | ||
static registered = Symbol("registerd"); | ||
static emitter = new EventEmitter(); | ||
|
||
constructor(name) { | ||
super(); | ||
this.name = name; | ||
this._registeredEvents = new Set(); | ||
} | ||
|
||
emit(event, ...args) { | ||
|
||
// the idea behin this was that it is used for /api/events ws route | ||
// but the static "side chain" emitter, can also be used | ||
// the purpuse of it was to transfer events between workers | ||
// see #6 when implmented | ||
// also, the "scenes" component can use it to detect state changes, e.g. for triggers | ||
if (!this._registeredEvents.has(event)) { | ||
|
||
this._registeredEvents.add(event); | ||
|
||
process.nextTick(() => { | ||
super.emit(Events.registered, ...args); | ||
}); | ||
|
||
} | ||
|
||
Events.emitter.emit(Events.emitted, { | ||
component: this.name, | ||
event, | ||
args | ||
}); | ||
|
||
return super.emit(event, ...args); | ||
|
||
} | ||
|
||
}; |