-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
events: Add helpers for event listeners (#1051)
* Simplify code * Support EventTarget and EventEmitter
- Loading branch information
Showing
6 changed files
with
103 additions
and
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import onoff from "./onoff.js"; | ||
|
||
export default function listeners(events) { | ||
return { | ||
subscribe(target) { | ||
const { on } = onoff(target); | ||
for (const [event, handler] of Object.entries(events)) { | ||
on(event, handler); | ||
} | ||
}, | ||
unsubscribe(target) { | ||
const { off } = onoff(target); | ||
for (const [event, handler] of Object.entries(events)) { | ||
off(event, handler); | ||
} | ||
}, | ||
}; | ||
} |
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,21 @@ | ||
const map = new WeakMap(); | ||
|
||
export default function onoff(target) { | ||
let m = map.get(target); | ||
|
||
if (!m) { | ||
const on = (target.addEventListener ?? target.addListener).bind(target); | ||
const off = (target.removeEventListener ?? target.removeListener).bind( | ||
target, | ||
); | ||
const once = ( | ||
target.once ?? | ||
((event, handler) => | ||
target.addEventListener(event, handler, { once: true })) | ||
).bind(target); | ||
m = { on, off, once }; | ||
map.set(target, m); | ||
} | ||
|
||
return m; | ||
} |
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