2018-07-21 21:51:20 +00:00
|
|
|
/* global window, Event, textsecure */
|
|
|
|
|
2016-01-14 22:07:00 +00:00
|
|
|
/*
|
|
|
|
* Implements EventTarget
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
|
|
|
|
*/
|
2018-07-21 21:51:20 +00:00
|
|
|
// eslint-disable-next-line func-names
|
2018-05-02 01:54:43 +00:00
|
|
|
(function() {
|
|
|
|
window.textsecure = window.textsecure || {};
|
2016-01-14 22:07:00 +00:00
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
function EventTarget() {}
|
2016-01-14 22:07:00 +00:00
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
EventTarget.prototype = {
|
|
|
|
constructor: EventTarget,
|
2018-07-21 21:51:20 +00:00
|
|
|
dispatchEvent(ev) {
|
2018-05-02 01:54:43 +00:00
|
|
|
if (!(ev instanceof Event)) {
|
|
|
|
throw new Error('Expects an event');
|
|
|
|
}
|
|
|
|
if (this.listeners === null || typeof this.listeners !== 'object') {
|
|
|
|
this.listeners = {};
|
|
|
|
}
|
2018-07-21 21:51:20 +00:00
|
|
|
const listeners = this.listeners[ev.type];
|
|
|
|
const results = [];
|
2018-05-02 01:54:43 +00:00
|
|
|
if (typeof listeners === 'object') {
|
2018-07-21 21:51:20 +00:00
|
|
|
for (let i = 0, max = listeners.length; i < max; i += 1) {
|
|
|
|
const listener = listeners[i];
|
2018-05-02 01:54:43 +00:00
|
|
|
if (typeof listener === 'function') {
|
|
|
|
results.push(listener.call(null, ev));
|
2016-01-14 22:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-02 01:54:43 +00:00
|
|
|
}
|
|
|
|
return results;
|
|
|
|
},
|
2018-07-21 21:51:20 +00:00
|
|
|
addEventListener(eventName, callback) {
|
2018-05-02 01:54:43 +00:00
|
|
|
if (typeof eventName !== 'string') {
|
|
|
|
throw new Error('First argument expects a string');
|
|
|
|
}
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
throw new Error('Second argument expects a function');
|
|
|
|
}
|
|
|
|
if (this.listeners === null || typeof this.listeners !== 'object') {
|
|
|
|
this.listeners = {};
|
|
|
|
}
|
2018-07-21 21:51:20 +00:00
|
|
|
let listeners = this.listeners[eventName];
|
2018-05-02 01:54:43 +00:00
|
|
|
if (typeof listeners !== 'object') {
|
|
|
|
listeners = [];
|
|
|
|
}
|
|
|
|
listeners.push(callback);
|
|
|
|
this.listeners[eventName] = listeners;
|
|
|
|
},
|
2018-07-21 21:51:20 +00:00
|
|
|
removeEventListener(eventName, callback) {
|
2018-05-02 01:54:43 +00:00
|
|
|
if (typeof eventName !== 'string') {
|
|
|
|
throw new Error('First argument expects a string');
|
|
|
|
}
|
|
|
|
if (typeof callback !== 'function') {
|
|
|
|
throw new Error('Second argument expects a function');
|
|
|
|
}
|
|
|
|
if (this.listeners === null || typeof this.listeners !== 'object') {
|
|
|
|
this.listeners = {};
|
|
|
|
}
|
2018-07-21 21:51:20 +00:00
|
|
|
const listeners = this.listeners[eventName];
|
2018-05-02 01:54:43 +00:00
|
|
|
if (typeof listeners === 'object') {
|
2018-07-21 21:51:20 +00:00
|
|
|
for (let i = 0; i < listeners.length; i += 1) {
|
2018-05-02 01:54:43 +00:00
|
|
|
if (listeners[i] === callback) {
|
|
|
|
listeners.splice(i, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.listeners[eventName] = listeners;
|
|
|
|
},
|
2018-07-21 21:51:20 +00:00
|
|
|
extend(obj) {
|
|
|
|
// eslint-disable-next-line no-restricted-syntax, guard-for-in
|
|
|
|
for (const prop in obj) {
|
2018-05-02 01:54:43 +00:00
|
|
|
this[prop] = obj[prop];
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
};
|
2016-01-14 22:07:00 +00:00
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
textsecure.EventTarget = EventTarget;
|
|
|
|
})();
|