-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2887 from mainmatter/implement-our-event-target
feat(ember-simple-auth): implement internal EventTarget
- Loading branch information
Showing
9 changed files
with
78 additions
and
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import EmberObject from '@ember/object'; | ||
import Evented from '@ember/object/evented'; | ||
|
||
export type EventListener<Events, Event extends keyof Events> = (event: Events[Event]) => void; | ||
|
||
type ValueIsEvent<T> = { | ||
[key in keyof T]: CustomEvent; | ||
}; | ||
|
||
/** | ||
* This module exists to act as a 'shim' between Native EventTarget API, which unfortunately can't be used due to Fastboot's node environment not providing it. | ||
* | ||
* It more-or-less implements the same API as an EventTarget would have but on top of Evented mixin instead. | ||
*/ | ||
export default class EsaEventTarget<Events extends ValueIsEvent<Events>> extends EmberObject.extend( | ||
Evented | ||
) { | ||
addEventListener<Event extends keyof Events & string>( | ||
event: Event, | ||
cb: EventListener<Events, Event> | ||
) { | ||
(this as any).on(event, cb); | ||
} | ||
|
||
removeEventListener<Event extends keyof Events & string>( | ||
event: Event, | ||
cb: EventListener<Events, Event> | ||
) { | ||
(this as any).off(event, cb); | ||
} | ||
|
||
dispatchEvent<Event extends keyof Events & string>(event: Event, value: Events[Event]['detail']) { | ||
// let customEvent: CustomEvent; | ||
// if (value) { | ||
// customEvent = new CustomEvent(event, { detail: value }); | ||
// } else { | ||
// customEvent = new CustomEvent(event); | ||
// } | ||
|
||
(this as any).trigger(event, { detail: value }); | ||
} | ||
} |
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
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.