Skip to content

Commit ede9c6a

Browse files
committed
For issue #47, add semantic event handlers to Unarchiver (onProgress, onExtract)
1 parent 675512c commit ede9c6a

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [1.2.3] - 2024-02-??
6+
7+
### Added
8+
9+
- archive: Support semantic methods for subscribing to unarchive events (onExtract), [Issue #47](https://github.com/codedread/bitjs/issues/47).
10+
11+
512
## [1.2.2] - 2024-01-26
613

714
### Added

archive/decompress.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { getConnectedPort } from './common.js';
1515
import { findMimeType } from '../file/sniffer.js';
1616

1717
// Exported as a convenience (and also because this module used to contain these).
18-
// TODO(2.0): Remove this export, since they have moved to events.js.
18+
// TODO(2.0): Remove this export, since they have moved to events.js?
1919
export {
2020
UnarchiveAppendEvent,
2121
UnarchiveErrorEvent,
@@ -31,6 +31,7 @@ export {
3131
/**
3232
* All extracted files returned by an Unarchiver will implement
3333
* the following interface:
34+
* TODO: Move this interface into common.js?
3435
*/
3536

3637
/**
@@ -49,7 +50,7 @@ export {
4950
*/
5051
export class Unarchiver extends EventTarget {
5152
/**
52-
* The client-side port that sends messages to, and receives messages from the
53+
* The client-side port that sends messages to, and receives messages from, the
5354
* decompressor implementation.
5455
* @type {MessagePort}
5556
* @private
@@ -74,6 +75,7 @@ export class Unarchiver extends EventTarget {
7475
constructor(arrayBuffer, options = {}) {
7576
super();
7677

78+
// TODO(2.0): Remove this.
7779
if (typeof options === 'string') {
7880
console.warn(`Deprecated: Don't send a raw string to Unarchiver()`);
7981
console.warn(` send UnarchiverOptions instead.`);
@@ -95,7 +97,7 @@ export class Unarchiver extends EventTarget {
9597
}
9698

9799
/**
98-
* Overridden so that the type hints for eventType are specific.
100+
* Overridden so that the type hints for eventType are specific. Prefer onExtract(), etc.
99101
* @param {'progress'|'extract'|'finish'} eventType
100102
* @param {EventListenerOrEventListenerObject} listener
101103
* @override
@@ -104,6 +106,36 @@ export class Unarchiver extends EventTarget {
104106
super.addEventListener(eventType, listener);
105107
}
106108

109+
/**
110+
* Type-safe way to subscribe to an UnarchiveExtractEvent.
111+
* @param {function(UnarchiveExtractEvent)} listener
112+
* @returns {Unarchiver} for chaining.
113+
*/
114+
onExtract(listener) {
115+
super.addEventListener(UnarchiveEventType.EXTRACT, listener);
116+
return this;
117+
}
118+
119+
/**
120+
* Type-safe way to subscribe to an UnarchiveFinishEvent.
121+
* @param {function(UnarchiveFinishEvent)} listener
122+
* @returns {Unarchiver} for chaining.
123+
*/
124+
onFinish(listener) {
125+
super.addEventListener(UnarchiveEventType.FINISH, listener);
126+
return this;
127+
}
128+
129+
/**
130+
* Type-safe way to subscribe to an UnarchiveProgressEvent.
131+
* @param {function(UnarchiveProgressEvent)} listener
132+
* @returns {Unarchiver} for chaining.
133+
*/
134+
onProgress(listener) {
135+
super.addEventListener(UnarchiveEventType.PROGRESS, listener);
136+
return this;
137+
}
138+
107139
/**
108140
* This method must be overridden by the subclass to return the script filename.
109141
* @returns {string} The MIME type of the archive.
@@ -154,7 +186,6 @@ export class Unarchiver extends EventTarget {
154186

155187
/**
156188
* Receive an event and pass it to the listener functions.
157-
*
158189
* @param {Object} obj
159190
* @returns {boolean} Returns true if the decompression is finished.
160191
* @private

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codedread/bitjs",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "Binary Tools for JavaScript",
55
"homepage": "https://github.com/codedread/bitjs",
66
"author": "Jeff Schiller",

0 commit comments

Comments
 (0)