Need to make a copy of listener array before iterating

If a listener calls RemoveEventListener from it's handler, some listeners won't be called because the array changes out from under it
This commit is contained in:
Blair MacIntyre 2018-03-18 14:53:42 -04:00
Родитель ff07dcaebb
Коммит 19b37f9f97
1 изменённых файлов: 5 добавлений и 1 удалений

Просмотреть файл

@ -31,7 +31,11 @@ export default class EventHandlerBase {
dispatchEvent(event){
let listeners = this._listeners.get(event.type)
if(Array.isArray(listeners) === false) return
for(let listener of listeners){
// need a copy, since removeEventListener is often called inside listeners to create one-shots and it modifies the array, causing
// listeners not to be called!
var array = listeners.slice( 0 );
for(let listener of array){
listener(event)
}
}