mirror of
https://github.com/codedread/bitjs
synced 2025-10-03 09:39:16 +02:00
Add explicit type-safe methods to bind for GIF events.
This commit is contained in:
parent
18f4ceb316
commit
5aad62cc49
3 changed files with 86 additions and 16 deletions
|
@ -29,7 +29,7 @@ export const GifParseEventType = {
|
||||||
* @property {string} version
|
* @property {string} version
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export class GifHeaderParseEvent extends Event {
|
export class GifHeaderEvent extends Event {
|
||||||
/** @param {GifHeader} header */
|
/** @param {GifHeader} header */
|
||||||
constructor(header) {
|
constructor(header) {
|
||||||
super(GifParseEventType.HEADER);
|
super(GifParseEventType.HEADER);
|
||||||
|
@ -58,7 +58,7 @@ export class GifHeaderParseEvent extends Event {
|
||||||
* @property {GifColor[]=} globalColorTable Only if globalColorTableFlag is true.
|
* @property {GifColor[]=} globalColorTable Only if globalColorTableFlag is true.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export class GifLogicalScreenParseEvent extends Event {
|
export class GifLogicalScreenEvent extends Event {
|
||||||
/** @param {GifLogicalScreen} */
|
/** @param {GifLogicalScreen} */
|
||||||
constructor(logicalScreen) {
|
constructor(logicalScreen) {
|
||||||
super(GifParseEventType.LOGICAL_SCREEN);
|
super(GifParseEventType.LOGICAL_SCREEN);
|
||||||
|
@ -204,13 +204,83 @@ export class GifParser extends EventTarget {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Overridden so that the type hints for eventType are specific.
|
* Type-safe way to bind a listener for a GifApplicationExtensionEvent.
|
||||||
* @param {'application_extension'|'comment_extension'|'graphical_control_extension'|'header'|'logical_screen'|'plain_text_extension'|'table_based_image'|'trailer'} eventType
|
* @param {function(GifApplicationExtensionEvent): void} listener
|
||||||
* @param {EventListenerOrEventListenerObject} listener
|
* @returns {GifParser} for chaining
|
||||||
* @override
|
|
||||||
*/
|
*/
|
||||||
addEventListener(eventType, listener) {
|
onApplicationExtension(listener) {
|
||||||
super.addEventListener(eventType, listener);
|
super.addEventListener(GifParseEventType.APPLICATION_EXTENSION, listener);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type-safe way to bind a listener for a GifCommentExtensionEvent.
|
||||||
|
* @param {function(GifCommentExtensionEvent): void} listener
|
||||||
|
* @returns {GifParser} for chaining
|
||||||
|
*/
|
||||||
|
onCommentExtension(listener) {
|
||||||
|
super.addEventListener(GifParseEventType.COMMENT_EXTENSION, listener);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type-safe way to bind a listener for a GifGraphicControlExtensionEvent.
|
||||||
|
* @param {function(GifGraphicControlExtensionEvent): void} listener
|
||||||
|
* @returns {GifParser} for chaining
|
||||||
|
*/
|
||||||
|
onGraphicControlExtension(listener) {
|
||||||
|
super.addEventListener(GifParseEventType.GRAPHIC_CONTROL_EXTENSION, listener);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type-safe way to bind a listener for a GifHeaderEvent.
|
||||||
|
* @param {function(GifHeaderEvent): void} listener
|
||||||
|
* @returns {GifParser} for chaining
|
||||||
|
*/
|
||||||
|
onHeader(listener) {
|
||||||
|
super.addEventListener(GifParseEventType.HEADER, listener);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type-safe way to bind a listener for a GifLogicalScreenEvent.
|
||||||
|
* @param {function(GifLogicalScreenEvent): void} listener
|
||||||
|
* @returns {GifParser} for chaining
|
||||||
|
*/
|
||||||
|
onLogicalScreen(listener) {
|
||||||
|
super.addEventListener(GifParseEventType.LOGICAL_SCREEN, listener);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type-safe way to bind a listener for a GifPlainTextExtensionEvent.
|
||||||
|
* @param {function(GifPlainTextExtensionEvent): void} listener
|
||||||
|
* @returns {GifParser} for chaining
|
||||||
|
*/
|
||||||
|
onPlainTextExtension(listener) {
|
||||||
|
super.addEventListener(GifParseEventType.PLAIN_TEXT_EXTENSION, listener);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type-safe way to bind a listener for a GifTableBasedImageEvent.
|
||||||
|
* @param {function(GifTableBasedImageEvent): void} listener
|
||||||
|
* @returns {GifParser} for chaining
|
||||||
|
*/
|
||||||
|
onTableBasedImage(listener) {
|
||||||
|
super.addEventListener(GifParseEventType.TABLE_BASED_IMAGE, listener);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type-safe way to bind a listener for a GifTrailerEvent.
|
||||||
|
* @param {function(GifTrailerEvent): void} listener
|
||||||
|
* @returns {GifParser} for chaining
|
||||||
|
*/
|
||||||
|
onTrailer(listener) {
|
||||||
|
super.addEventListener(GifParseEventType.TRAILER, listener);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -224,7 +294,7 @@ export class GifParser extends EventTarget {
|
||||||
const version = this.version = this.bstream.readString(3); // "87a" or "89a"
|
const version = this.version = this.bstream.readString(3); // "87a" or "89a"
|
||||||
if (!["87a", "89a"].includes(version)) throw `Bad version: ${version}`;
|
if (!["87a", "89a"].includes(version)) throw `Bad version: ${version}`;
|
||||||
|
|
||||||
this.dispatchEvent(new GifHeaderParseEvent(
|
this.dispatchEvent(new GifHeaderEvent(
|
||||||
/** @type {GifHeader} */ ({ version })
|
/** @type {GifHeader} */ ({ version })
|
||||||
));
|
));
|
||||||
|
|
||||||
|
@ -253,7 +323,7 @@ export class GifParser extends EventTarget {
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.dispatchEvent(new GifLogicalScreenParseEvent(
|
this.dispatchEvent(new GifLogicalScreenEvent(
|
||||||
/** @type {GifLogicalScreen} */ ({
|
/** @type {GifLogicalScreen} */ ({
|
||||||
logicalScreenWidth,
|
logicalScreenWidth,
|
||||||
logicalScreenHeight,
|
logicalScreenHeight,
|
||||||
|
|
2
index.js
2
index.js
|
@ -24,7 +24,7 @@ export {
|
||||||
export { getFullMIMEString, getShortMIMEString } from './codecs/codecs.js';
|
export { getFullMIMEString, getShortMIMEString } from './codecs/codecs.js';
|
||||||
export { findMimeType } from './file/sniffer.js';
|
export { findMimeType } from './file/sniffer.js';
|
||||||
export { GifApplicationExtensionEvent, GifCommentExtensionEvent, GifGraphicControlExtensionEvent,
|
export { GifApplicationExtensionEvent, GifCommentExtensionEvent, GifGraphicControlExtensionEvent,
|
||||||
GifHeaderParseEvent, GifLogicalScreenParseEvent, GifParseEventType, GifParser,
|
GifHeaderEvent, GifLogicalScreenEvent, GifParseEventType, GifParser,
|
||||||
GifPlainTextExtensionEvent, GifTableBasedImageEvent } from './image/parsers/gif.js';
|
GifPlainTextExtensionEvent, GifTableBasedImageEvent } from './image/parsers/gif.js';
|
||||||
export { convertWebPtoPNG, convertWebPtoJPG } from './image/webp-shim/webp-shim.js';
|
export { convertWebPtoPNG, convertWebPtoJPG } from './image/webp-shim/webp-shim.js';
|
||||||
export { BitBuffer } from './io/bitbuffer.js';
|
export { BitBuffer } from './io/bitbuffer.js';
|
||||||
|
|
|
@ -14,18 +14,18 @@ describe('bitjs.image.parsers.GifParser', () => {
|
||||||
const parser = new GifParser(ab);
|
const parser = new GifParser(ab);
|
||||||
let trailerFound = false;
|
let trailerFound = false;
|
||||||
let comment;
|
let comment;
|
||||||
parser.addEventListener('logical_screen', evt => {
|
parser.onLogicalScreen(evt => {
|
||||||
const {logicalScreenWidth, logicalScreenHeight} = evt.logicalScreen;
|
const {logicalScreenWidth, logicalScreenHeight} = evt.logicalScreen;
|
||||||
expect(logicalScreenWidth).equals(32);
|
expect(logicalScreenWidth).equals(32);
|
||||||
expect(logicalScreenHeight).equals(52);
|
expect(logicalScreenHeight).equals(52);
|
||||||
});
|
});
|
||||||
parser.addEventListener('table_based_image', evt => {
|
parser.onTableBasedImage(evt => {
|
||||||
const {imageWidth, imageHeight} = evt.tableBasedImage;
|
const {imageWidth, imageHeight} = evt.tableBasedImage;
|
||||||
expect(imageWidth).equals(32);
|
expect(imageWidth).equals(32);
|
||||||
expect(imageHeight).equals(52);
|
expect(imageHeight).equals(52);
|
||||||
});
|
});
|
||||||
parser.addEventListener('comment_extension', evt => comment = evt.comment);
|
parser.onCommentExtension(evt => comment = evt.comment);
|
||||||
parser.addEventListener('trailer', evt => trailerFound = true);
|
parser.onTrailer(evt => trailerFound = true);
|
||||||
|
|
||||||
await parser.start();
|
await parser.start();
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ describe('bitjs.image.parsers.GifParser', () => {
|
||||||
let appId;
|
let appId;
|
||||||
let appAuthCode;
|
let appAuthCode;
|
||||||
let hasAppData = false;
|
let hasAppData = false;
|
||||||
parser.addEventListener('application_extension', evt => {
|
parser.onApplicationExtension(evt => {
|
||||||
appId = evt.applicationExtension.applicationIdentifier
|
appId = evt.applicationExtension.applicationIdentifier
|
||||||
appAuthCode = new TextDecoder().decode(
|
appAuthCode = new TextDecoder().decode(
|
||||||
evt.applicationExtension.applicationAuthenticationCode);
|
evt.applicationExtension.applicationAuthenticationCode);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue