Skip to content

Commit

Permalink
Merge pull request webrtcHacks#1036 from webrtcHacks/fix-eventtarget
Browse files Browse the repository at this point in the history
util: fix wrapPeerConnection
  • Loading branch information
fippo authored Jun 14, 2020
2 parents cd88213 + dfcc726 commit d74a0c5
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,39 @@ export function wrapPeerConnectionEvent(window, eventNameToWrap, wrapper) {
const wrappedCallback = (e) => {
const modifiedEvent = wrapper(e);
if (modifiedEvent) {
cb(modifiedEvent);
if (cb.handleEvent) {
cb.handleEvent(modifiedEvent);
} else {
cb(modifiedEvent);
}
}
};
this._eventMap = this._eventMap || {};
this._eventMap[cb] = wrappedCallback;
if (!this._eventMap[eventNameToWrap]) {
this._eventMap[eventNameToWrap] = new Map();
}
this._eventMap[eventNameToWrap].set(cb, wrappedCallback);
return nativeAddEventListener.apply(this, [nativeEventName,
wrappedCallback]);
};

const nativeRemoveEventListener = proto.removeEventListener;
proto.removeEventListener = function(nativeEventName, cb) {
if (nativeEventName !== eventNameToWrap || !this._eventMap
|| !this._eventMap[cb]) {
|| !this._eventMap[eventNameToWrap]) {
return nativeRemoveEventListener.apply(this, arguments);
}
if (!this._eventMap[eventNameToWrap].has(cb)) {
return nativeRemoveEventListener.apply(this, arguments);
}
const unwrappedCb = this._eventMap[cb];
delete this._eventMap[cb];
const unwrappedCb = this._eventMap[eventNameToWrap].get(cb);
this._eventMap[eventNameToWrap].delete(cb);
if (this._eventMap[eventNameToWrap].size === 0) {
delete this._eventMap[eventNameToWrap];
}
if (Object.keys(this._eventMap).length === 0) {
delete this._eventMap;
}
return nativeRemoveEventListener.apply(this, [nativeEventName,
unwrappedCb]);
};
Expand Down

0 comments on commit d74a0c5

Please sign in to comment.