diff --git a/lul/index.html b/lul/index.html index 1ce33f04..c170e562 100644 --- a/lul/index.html +++ b/lul/index.html @@ -12,13 +12,12 @@ const squoosh = Comlink.proxy(ifr.contentWindow); async function load() { const blob = await fetch("/profile.jpg").then(resp => resp.blob()); await squoosh.setFile(blob); - console.log('done') + extract(); } document.all.load.onclick = load; async function extract() { const file = await squoosh.getBlob(1); - console.log('asdf', file) const url = URL.createObjectURL(file); const img = document.createElement('img'); img.src = url; diff --git a/src/components/App/index.tsx b/src/components/App/index.tsx index 52d9c7c4..ca4fa144 100644 --- a/src/components/App/index.tsx +++ b/src/components/App/index.tsx @@ -108,12 +108,16 @@ export default class App extends Component { private exposeAPI() { const api = { setFile: (blob: Blob, name: string) => { - this.setState({ file: new File([blob], name) }); + return new Promise((resolve) => { + this.setState({ file: new File([blob], name) }); + document.addEventListener('squooshingdone', () => resolve(), { once: true }); + }); }, getBlob: async (side: 0 | 1) => { if (!this.state.file || !this.compressInstance) { throw new Error('No file has been loaded'); } + await this.compressInstance.compressionJobs[side]; return this.compressInstance.state.images[side].file; }, }; diff --git a/src/components/compress/index.tsx b/src/components/compress/index.tsx index fd727dc6..c84fe7d7 100644 --- a/src/components/compress/index.tsx +++ b/src/components/compress/index.tsx @@ -205,6 +205,7 @@ const originalDocumentTitle = document.title; export default class Compress extends Component { widthQuery = window.matchMedia('(max-width: 599px)'); + compressionJobs: [Promise, Promise] = [Promise.resolve(), Promise.resolve()]; state: State = { source: undefined, @@ -305,7 +306,7 @@ export default class Compress extends Component { // The image only needs updated if the encoder/preprocessor settings have changed, or the // source has changed. if (sourceDataChanged || encoderChanged || preprocessorChanged) { - this.queueUpdateImage(i, { + this.compressionJobs[i] = this.updateImage(i, { skipPreprocessing: !sourceDataChanged && !preprocessorChanged, }); } @@ -489,7 +490,9 @@ export default class Compress extends Component { loading: true, }); - this.setState({ sides }); + this.setState({ sides }, () => this.base!.dispatchEvent( + new CustomEvent('squooshingdone', { bubbles: true }), + )); const side = sides[index]; const settings = side.latestSettings;