generated from napi-rs/package-template
-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement EventTarget interface #61
Merged
Merged
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
3e435b9
feat: event emitter new try
b-ma b8f62b8
typo
b-ma e7dce73
fix conflicts
b-ma e91dd1d
feat: implenent sink change event + cleaning
b-ma e736138
wip: implement onended event
b-ma 848c156
feat: onended event + refactor js monkey patching
b-ma a79beb1
prevent wpt to crash, options parsing should be consolidated
b-ma fe90271
fix: improve error handling in AudioNode constructor + implement some…
b-ma d94f136
fix: set invalid enum value should not throw
b-ma 9f96de5
Update wpt remote ref
orottier 202e54e
wpt: pass XMLHttpRequest mock to wpt runner
b-ma fb58d13
chore: reinstate proper upstream crate
b-ma 32b0457
fix: remove async facade for startRendering, is now natively async
b-ma 456624e
ci: remove lint for now, necessitates some precommit hook to be useful
b-ma 22d3330
lint
b-ma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,59 @@ | ||
const { isFunction } = require('./utils.js'); | ||
|
||
const kEventListeners = Symbol('eventListeners'); | ||
const kDispatchEvent = Symbol.for('napiDispatchEvent'); | ||
|
||
module.exports = (superclass, eventTypes) => class extends superclass { | ||
[kEventListeners] = new Map(); | ||
|
||
constructor(...args) { | ||
super(...args); | ||
|
||
eventTypes.forEach((eventType) => { | ||
this[`on${eventType}`] = null; | ||
}); | ||
|
||
// we need to bind because calling [kDispatchEvent] loose `this` | ||
this[kDispatchEvent] = this[kDispatchEvent].bind(this); | ||
} | ||
|
||
// instance might | ||
addEventListener(eventType, callback) { | ||
if (!this[kEventListeners].has(eventType)) { | ||
this[kEventListeners].set(eventType, new Set()); | ||
} | ||
|
||
const callbacks = this[kEventListeners].get(eventType); | ||
callbacks.add(callback); | ||
} | ||
|
||
removeEventListener(eventType, callback) { | ||
// this is valid event eventType, otherwaise just ignore | ||
if (this[kEventListeners].has(eventType)) { | ||
const callbacks = this[kEventListeners].get(eventType); | ||
callbacks.delete(callback); | ||
} | ||
} | ||
|
||
dispatchEvent(event) { | ||
if (isFunction(this[`on${event.type}`])) { | ||
this[`on${event.type}`](event); | ||
} | ||
|
||
if (this[kEventListeners].has(event.type)) { | ||
const callbacks = this[kEventListeners].get(event.type); | ||
callbacks.forEach(callback => callback(event)); | ||
} | ||
} | ||
|
||
// called from rust | ||
[kDispatchEvent](err, eventType) { | ||
const event = new Event(eventType); | ||
// cannot override, this would need to derive EventTarget | ||
// cf. https://www.nearform.com/blog/node-js-and-the-struggles-of-being-an-eventtarget/ | ||
// event.target = this; | ||
// event.currentTarget = this; | ||
// event.srcElement = this; | ||
this.dispatchEvent(event); | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope this hack is only needed for the onStateChange event? Or also for other types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hum no it seems to be required for other types as well, need to investigate further