mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-12 00:37:19 +00:00
Create a SideEvent
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import App from './index';
|
import App from './index';
|
||||||
|
import { SideEvent, SideEventType } from '../compress/index';
|
||||||
|
|
||||||
import { expose } from 'comlink';
|
import { expose } from 'comlink';
|
||||||
|
|
||||||
@@ -22,7 +23,7 @@ class API {
|
|||||||
setFile(blob: Blob, name: string) {
|
setFile(blob: Blob, name: string) {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
document.addEventListener(
|
document.addEventListener(
|
||||||
'squoosh:processingstart',
|
SideEventType.START,
|
||||||
() => resolve(),
|
() => resolve(),
|
||||||
{ once: true },
|
{ once: true },
|
||||||
);
|
);
|
||||||
@@ -43,30 +44,30 @@ class API {
|
|||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
document.addEventListener(
|
document.addEventListener(
|
||||||
'squoosh:processingdone',
|
SideEventType.DONE,
|
||||||
(event) => {
|
(event: Event) => {
|
||||||
if ((event as CustomEvent).detail.side !== side) {
|
if ((event as SideEvent).side !== side) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
resolve(this._app.compressInstance!.state.sides[side].file);
|
resolve(this._app.compressInstance!.state.sides[side].file);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
document.addEventListener(
|
document.addEventListener(
|
||||||
'squoosh:processingabort',
|
SideEventType.ABORT,
|
||||||
(event) => {
|
(event) => {
|
||||||
if ((event as CustomEvent).detail.side !== side) {
|
if ((event as SideEvent).side !== side) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
reject(new DOMException('Aborted', 'AbortError'));
|
reject(new DOMException('Aborted', 'AbortError'));
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
document.addEventListener(
|
document.addEventListener(
|
||||||
'squoosh:processingerror',
|
SideEventType.ERROR,
|
||||||
(event) => {
|
(event) => {
|
||||||
if ((event as CustomEvent).detail.side !== side) {
|
if ((event as SideEvent).side !== side) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
reject(new Error((event as CustomEvent).detail.msg));
|
reject((event as SideEvent).error);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -32,6 +32,26 @@ import { ExpandIcon, CopyAcrossIconProps } from '../../lib/icons';
|
|||||||
import SnackBarElement from '../../lib/SnackBar';
|
import SnackBarElement from '../../lib/SnackBar';
|
||||||
import { InputProcessorState, defaultInputProcessorState } from '../../codecs/input-processors';
|
import { InputProcessorState, defaultInputProcessorState } from '../../codecs/input-processors';
|
||||||
|
|
||||||
|
export enum SideEventType {
|
||||||
|
START = 'squoosh:START',
|
||||||
|
DONE = 'squoosh:done',
|
||||||
|
ABORT = 'squoosh:abort',
|
||||||
|
ERROR = 'squoosh:error',
|
||||||
|
}
|
||||||
|
export interface SideEventInit extends EventInit {
|
||||||
|
side?: 0|1;
|
||||||
|
error?: Error;
|
||||||
|
}
|
||||||
|
export class SideEvent extends Event {
|
||||||
|
public side?: 0|1;
|
||||||
|
public error?: Error;
|
||||||
|
constructor(name: SideEventType, init: SideEventInit) {
|
||||||
|
super(name, init);
|
||||||
|
this.side = init.side;
|
||||||
|
this.error = init.error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export interface SourceImage {
|
export interface SourceImage {
|
||||||
file: File | Fileish;
|
file: File | Fileish;
|
||||||
decoded: ImageData;
|
decoded: ImageData;
|
||||||
@@ -474,28 +494,30 @@ export default class Compress extends Component<Props, State> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@bind
|
@bind
|
||||||
private dispatchCustomEvent(name: string, detail?: {}) {
|
private dispatchSideEvent(type: SideEventType, init: SideEventInit = {}) {
|
||||||
(this.base || document).dispatchEvent(new CustomEvent(name, { detail, bubbles: true }));
|
document.dispatchEvent(
|
||||||
|
new SideEvent(type, init),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@bind
|
@bind
|
||||||
private signalProcessingStart() {
|
private signalProcessingStart() {
|
||||||
this.dispatchCustomEvent('squoosh:processingstart');
|
this.dispatchSideEvent(SideEventType.START);
|
||||||
}
|
}
|
||||||
|
|
||||||
@bind
|
@bind
|
||||||
private signalProcessingDone(side: 0|1) {
|
private signalProcessingDone(side: 0|1) {
|
||||||
this.dispatchCustomEvent('squoosh:processingdone', { side });
|
this.dispatchSideEvent(SideEventType.DONE, { side });
|
||||||
}
|
}
|
||||||
|
|
||||||
@bind
|
@bind
|
||||||
private signalProcessingAbort(side: 0|1) {
|
private signalProcessingAbort(side: 0|1) {
|
||||||
this.dispatchCustomEvent('squoosh:processingabort', { side });
|
this.dispatchSideEvent(SideEventType.ABORT, { side });
|
||||||
}
|
}
|
||||||
|
|
||||||
@bind
|
@bind
|
||||||
private signalProcessingError(side: 0|1, msg: string) {
|
private signalProcessingError(side: 0|1, msg: string) {
|
||||||
this.dispatchCustomEvent('squoosh:processingerror', { side, msg });
|
this.dispatchSideEvent(SideEventType.ERROR, { side, error: new Error(msg) });
|
||||||
}
|
}
|
||||||
|
|
||||||
private async updateImage(index: number, options: UpdateImageOptions = {}): Promise<void> {
|
private async updateImage(index: number, options: UpdateImageOptions = {}): Promise<void> {
|
||||||
|
|||||||
Reference in New Issue
Block a user