Address minor nits

This commit is contained in:
Surma
2018-12-19 13:58:03 +00:00
parent 98f61ba60c
commit 2fa2e567a6
2 changed files with 24 additions and 24 deletions

View File

@@ -6,67 +6,67 @@ const API_VERSION = 1;
export function exposeAPI(app: App) {
self.parent.postMessage({ type: 'READY', version: API_VERSION }, '*');
self.addEventListener('message', (ev: MessageEvent) => {
if (ev.data !== 'READY?') {
self.addEventListener('message', (event: MessageEvent) => {
if (event.data !== 'READY?') {
return;
}
ev.stopPropagation();
event.stopImmediatePropagation();
self.parent.postMessage({ type: 'READY', version: API_VERSION }, '*');
});
expose(new API(app), self.parent);
}
class API {
constructor(private app: App) { }
constructor(private _app: App) { }
async setFile(blob: Blob, name: string) {
return new Promise(async (resolve) => {
setFile(blob: Blob, name: string) {
return new Promise((resolve) => {
document.addEventListener(
'squoosh:processingstart',
() => resolve(),
{ once: true },
);
this.app.openFile(new File([blob], name));
this._app.openFile(new File([blob], name));
});
}
async getBlob(side: 0 | 1) {
if (!this.app.state.file || !this.app.compressInstance) {
getBlob(side: 0 | 1) {
if (!this._app.state.file || !this._app.compressInstance) {
throw new Error('No file has been loaded');
}
if (
!this.app.compressInstance!.state.loading &&
!this.app.compressInstance!.state.sides[side].loading
!this._app.compressInstance!.state.loading &&
!this._app.compressInstance!.state.sides[side].loading
) {
return this.app.compressInstance!.state.sides[side].file;
return this._app.compressInstance!.state.sides[side].file;
}
return new Promise((resolve, reject) => {
document.addEventListener(
'squoosh:processingdone',
(ev) => {
if ((ev as CustomEvent).detail.side !== side) {
(event) => {
if ((event as CustomEvent).detail.side !== side) {
return;
}
resolve(this.app.compressInstance!.state.sides[side].file);
resolve(this._app.compressInstance!.state.sides[side].file);
},
);
document.addEventListener(
'squoosh:processingabort',
(ev) => {
if ((ev as CustomEvent).detail.side !== side) {
(event) => {
if ((event as CustomEvent).detail.side !== side) {
return;
}
reject('aborted');
reject(new DOMException('Aborted', 'AbortError'));
},
);
document.addEventListener(
'squoosh:processingerroor',
(ev) => {
if ((ev as CustomEvent).detail.side !== side) {
'squoosh:processingerror',
(event) => {
if ((event as CustomEvent).detail.side !== side) {
return;
}
reject((ev as CustomEvent).detail.msg);
reject(new Error((event as CustomEvent).detail.msg));
},
);
});