js/modules/events.js
/**
     * Dispatches a given event, calling all the attached handlers for it.
     * @param {string} name - Event to dispatch.
     */
Dispatches a given event, calling all the attached handlers for it.
@param {string} name - Event to dispatch.
Dispatches a given event calling all the attached handlers for it . @param { string } name - Event to dispatch .
dispatch(name) {
        console.log('dispatching event "' + name + '" -', this.handlers[name] === undefined ? 0 : this.handlers[name].length, 'handler(s)')
        this.pendingEvents.push(name)
        if (!this.workingOnEvents) {
            this.workingOnEvents = true
            while (true) {
                var newEvent = this.pendingEvents.shift()
                if (newEvent === undefined) {
                    break
                }
                var handlers = this.handlers[newEvent]
                if (handlers === undefined) {
                    continue
                }
                for (const handler of handlers) {
                    var returnVal = handler(newEvent)
                    if (returnVal === true) {
                        break
                    }
                }
                var allEventHandlers = this.handlers['*']
                if (allEventHandlers === undefined) {
                    continue
                }
                for (const handler of allEventHandlers) {
                    if (handlers.includes(handler)) {
                        continue
                    }
                    var returnVal = handler(newEvent)
                    if (returnVal === true) {
                        break
                    }
                }
            }
            this.workingOnEvents = false
        }
    }
dispatch ( name ) { console . log ( 'dispatching event "' + name + '" -' , this . handlers [ name ] === undefined ? 0 : this . handlers [ name ] . length , 'handler(s)' ) this . pendingEvents . push ( name ) if ( ! this . workingOnEvents ) { this . workingOnEvents = true while ( true ) { var newEvent = this . pendingEvents . shift ( ) if ( newEvent === undefined ) { break } var handlers = this . handlers [ newEvent ] if ( handlers === undefined ) { continue } for ( const handler of handlers ) { var returnVal = handler ( newEvent ) if ( returnVal === true ) { break } } var allEventHandlers = this . handlers [ '*' ] if ( allEventHandlers === undefined ) { continue } for ( const handler of allEventHandlers ) { if ( handlers . includes ( handler ) ) { continue } var returnVal = handler ( newEvent ) if ( returnVal === true ) { break } } } this . workingOnEvents = false } }
Dispatches a given event, calling all the attached handlers for it.
Dispatches a given event calling all the attached handlers for it .
// call handler for new event // console.log('calling handler', handler, 'with event', newEvent) // call handlers for * event // check if handler's already been called // console.log('calling handler', handler, 'with event', newEvent)