From 47ae6f8965d891986298cacd7ce4d7a6e3719b20 Mon Sep 17 00:00:00 2001 From: Jeff Schiller Date: Wed, 8 Feb 2023 21:18:51 -0800 Subject: [PATCH] Fix issue #30: Make Unarchiver derive from EventTarget and the UnarchiveEvents derive from Event. --- archive/decompress-internal.js | 65 +++++----------------------------- 1 file changed, 9 insertions(+), 56 deletions(-) diff --git a/archive/decompress-internal.js b/archive/decompress-internal.js index adf46db..f8422c0 100644 --- a/archive/decompress-internal.js +++ b/archive/decompress-internal.js @@ -33,16 +33,12 @@ export const UnarchiveEventType = { /** * An unarchive event. */ - export class UnarchiveEvent { + export class UnarchiveEvent extends Event { /** * @param {string} type The event type. */ constructor(type) { - /** - * The event type. - * @type {string} - */ - this.type = type; + super(type); } } @@ -169,11 +165,8 @@ export class UnarchiveExtractEvent extends UnarchiveEvent { /** * Base class for all Unarchivers. - * TODO: When EventTarget constructors are broadly supported, make this extend - * EventTarget and remove event listener code. - * https://caniuse.com/#feat=mdn-api_eventtarget_eventtarget */ - export class Unarchiver { + export class Unarchiver extends EventTarget { /** * @param {ArrayBuffer} arrayBuffer The Array Buffer. Note that this ArrayBuffer must not be * 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 { * 'debug': A boolean where true indicates that the archivers should log debug output. */ constructor(arrayBuffer, createWorkerFn, options = {}) { + super(); + if (typeof options === 'string') { console.warn(`Deprecated: Don't send a raw string to Unarchiver()`); console.warn(` send {'pathToBitJS':'${options}'} instead`); @@ -219,16 +214,6 @@ export class UnarchiveExtractEvent extends UnarchiveEvent { */ this.debugMode_ = !!(options.debug); - /** - * A map from event type to an array of listeners. - * @private - * @type {Map.} - */ - this.listeners_ = {}; - for (let type in UnarchiveEventType) { - this.listeners_[UnarchiveEventType[type]] = []; - } - /** * Private web worker initialized during start(). * @private @@ -255,35 +240,6 @@ export class UnarchiveExtractEvent extends UnarchiveEvent { throw 'Subclasses of Unarchiver must overload getScriptFileName()'; } - /** - * Adds an event listener for UnarchiveEvents. - * - * @param {string} Event type. - * @param {function} An event handler function. - */ - addEventListener(type, listener) { - if (type in this.listeners_) { - if (this.listeners_[type].indexOf(listener) == -1) { - this.listeners_[type].push(listener); - } - } - } - - /** - * Removes an event listener. - * - * @param {string} Event type. - * @param {EventListener|function} An event listener or handler function. - */ - removeEventListener(type, listener) { - if (type in this.listeners_) { - const index = this.listeners_[type].indexOf(listener); - if (index != -1) { - this.listeners_[type].splice(index, 1); - } - } - } - /** * Create an UnarchiveEvent out of the object sent back from the Worker. * @param {Object} obj @@ -322,10 +278,9 @@ export class UnarchiveExtractEvent extends UnarchiveEvent { */ handleWorkerEvent_(obj) { const type = obj.type; - if (type && Object.values(UnarchiveEventType).includes(type) && - this.listeners_[obj.type] instanceof Array) { + if (type && Object.values(UnarchiveEventType).includes(type)) { const evt = this.createUnarchiveEvent_(obj); - this.listeners_[evt.type].forEach(function (listener) { listener(evt) }); + this.dispatchEvent(evt); if (evt.type == UnarchiveEventType.FINISH) { this.worker_.terminate(); } @@ -387,10 +342,8 @@ export class UnarchiveExtractEvent extends UnarchiveEvent { this.worker_.postMessage({ bytes: ab }); } } - if (this.listeners_[UnarchiveEventType.APPEND]) { - const evt = new UnarchiveAppendEvent(numBytes); - this.listeners_[UnarchiveEventType.APPEND].forEach(listener => listener(evt)); - } + + this.dispatchEvent(new UnarchiveAppendEvent(numBytes)); } /**