12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
-
-
-
-
- import Log from '../utils/log.js'
-
- const log = new Log('EventTarget')
-
- log.disable()
-
- export default class EventTarget {
-
- listeners : any;
-
- constructor() {
- log.info('constructor called')
- this.listeners = {}
- }
-
-
-
- addEventListener(type:string, cb : () => void) {
- log.info('add event listener', type, cb)
- if(!(type in this.listeners)) {
- this.listeners[type] = []
- }
- this.listeners[type].push(cb)
- }
-
-
-
- removeEventListener(type:string, cb:() => void) {
- log.info('remove event listener', type, cb)
- if(!(type in this.listeners))
- return
- let handlers = this.listeners[type]
- for(let i in handlers) {
- if(cb === handlers[i]) {
- handlers.splice(i,1)
- return this.removeEventListener(type, cb)
- }
- }
- }
-
-
-
- dispatchEvent(type:string,event:Event) {
- log.info('dispatch event', event)
- if(!(type in this.listeners))
- return
- let handlers = this.listeners[type]
- for(let i in handlers) {
- handlers[i].call(this, event)
- }
-
- }
-
-
-
- clearEventListeners() {
- for(let i in this.listeners) {
- delete this.listeners[i]
- }
- }
-
- }
|