Skip to content

Commit 47ae6f8

Browse files
committed
Fix issue #30: Make Unarchiver derive from EventTarget and the UnarchiveEvents derive from Event.
1 parent 2431240 commit 47ae6f8

File tree

1 file changed

+9
-56
lines changed

1 file changed

+9
-56
lines changed

archive/decompress-internal.js

Lines changed: 9 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,12 @@ export const UnarchiveEventType = {
3333
/**
3434
* An unarchive event.
3535
*/
36-
export class UnarchiveEvent {
36+
export class UnarchiveEvent extends Event {
3737
/**
3838
* @param {string} type The event type.
3939
*/
4040
constructor(type) {
41-
/**
42-
* The event type.
43-
* @type {string}
44-
*/
45-
this.type = type;
41+
super(type);
4642
}
4743
}
4844

@@ -169,11 +165,8 @@ export class UnarchiveExtractEvent extends UnarchiveEvent {
169165

170166
/**
171167
* Base class for all Unarchivers.
172-
* TODO: When EventTarget constructors are broadly supported, make this extend
173-
* EventTarget and remove event listener code.
174-
* https://caniuse.com/#feat=mdn-api_eventtarget_eventtarget
175168
*/
176-
export class Unarchiver {
169+
export class Unarchiver extends EventTarget {
177170
/**
178171
* @param {ArrayBuffer} arrayBuffer The Array Buffer. Note that this ArrayBuffer must not be
179172
* referenced once it is sent to the Unarchiver, since it is marked as Transferable and sent
@@ -186,6 +179,8 @@ export class UnarchiveExtractEvent extends UnarchiveEvent {
186179
* 'debug': A boolean where true indicates that the archivers should log debug output.
187180
*/
188181
constructor(arrayBuffer, createWorkerFn, options = {}) {
182+
super();
183+
189184
if (typeof options === 'string') {
190185
console.warn(`Deprecated: Don't send a raw string to Unarchiver()`);
191186
console.warn(` send {'pathToBitJS':'${options}'} instead`);
@@ -219,16 +214,6 @@ export class UnarchiveExtractEvent extends UnarchiveEvent {
219214
*/
220215
this.debugMode_ = !!(options.debug);
221216

222-
/**
223-
* A map from event type to an array of listeners.
224-
* @private
225-
* @type {Map.<string, Array>}
226-
*/
227-
this.listeners_ = {};
228-
for (let type in UnarchiveEventType) {
229-
this.listeners_[UnarchiveEventType[type]] = [];
230-
}
231-
232217
/**
233218
* Private web worker initialized during start().
234219
* @private
@@ -255,35 +240,6 @@ export class UnarchiveExtractEvent extends UnarchiveEvent {
255240
throw 'Subclasses of Unarchiver must overload getScriptFileName()';
256241
}
257242

258-
/**
259-
* Adds an event listener for UnarchiveEvents.
260-
*
261-
* @param {string} Event type.
262-
* @param {function} An event handler function.
263-
*/
264-
addEventListener(type, listener) {
265-
if (type in this.listeners_) {
266-
if (this.listeners_[type].indexOf(listener) == -1) {
267-
this.listeners_[type].push(listener);
268-
}
269-
}
270-
}
271-
272-
/**
273-
* Removes an event listener.
274-
*
275-
* @param {string} Event type.
276-
* @param {EventListener|function} An event listener or handler function.
277-
*/
278-
removeEventListener(type, listener) {
279-
if (type in this.listeners_) {
280-
const index = this.listeners_[type].indexOf(listener);
281-
if (index != -1) {
282-
this.listeners_[type].splice(index, 1);
283-
}
284-
}
285-
}
286-
287243
/**
288244
* Create an UnarchiveEvent out of the object sent back from the Worker.
289245
* @param {Object} obj
@@ -322,10 +278,9 @@ export class UnarchiveExtractEvent extends UnarchiveEvent {
322278
*/
323279
handleWorkerEvent_(obj) {
324280
const type = obj.type;
325-
if (type && Object.values(UnarchiveEventType).includes(type) &&
326-
this.listeners_[obj.type] instanceof Array) {
281+
if (type && Object.values(UnarchiveEventType).includes(type)) {
327282
const evt = this.createUnarchiveEvent_(obj);
328-
this.listeners_[evt.type].forEach(function (listener) { listener(evt) });
283+
this.dispatchEvent(evt);
329284
if (evt.type == UnarchiveEventType.FINISH) {
330285
this.worker_.terminate();
331286
}
@@ -387,10 +342,8 @@ export class UnarchiveExtractEvent extends UnarchiveEvent {
387342
this.worker_.postMessage({ bytes: ab });
388343
}
389344
}
390-
if (this.listeners_[UnarchiveEventType.APPEND]) {
391-
const evt = new UnarchiveAppendEvent(numBytes);
392-
this.listeners_[UnarchiveEventType.APPEND].forEach(listener => listener(evt));
393-
}
345+
346+
this.dispatchEvent(new UnarchiveAppendEvent(numBytes));
394347
}
395348

396349
/**

0 commit comments

Comments
 (0)