Update encodedWith to contain resolved values

We already await the promises that we set on the `encodedWith` instead of setting already
resolved promises to `encodedWith` we can set the resolved values

So, the users can use like
`const { mozjpeg: { binary } } = await image.encode({ mozjpeg })` or
they can first run
`await image.encode({ mozjpeg })` and then
`image.encodedWith.mozjpeg.binary`
This commit is contained in:
ergunsh
2021-09-10 15:18:14 +02:00
parent 914cdea41d
commit 95a1b35c91

View File

@@ -186,7 +186,7 @@ class Image {
public file: ArrayBuffer | ArrayLike<number>; public file: ArrayBuffer | ArrayLike<number>;
public workerPool: WorkerPool<JobMessage, any>; public workerPool: WorkerPool<JobMessage, any>;
public decoded: Promise<{ bitmap: ImageData }>; public decoded: Promise<{ bitmap: ImageData }>;
public encodedWith: { [key in EncoderKey]?: Promise<EncodeResult> }; public encodedWith: { [key in EncoderKey]?: EncodeResult };
constructor( constructor(
workerPool: WorkerPool<JobMessage, any>, workerPool: WorkerPool<JobMessage, any>,
@@ -227,7 +227,7 @@ class Image {
/** /**
* Define one or several encoders to use on the image. * Define one or several encoders to use on the image.
* @param {object} encodeOptions - An object with encoders to use, and their settings. * @param {object} encodeOptions - An object with encoders to use, and their settings.
* @returns {Promise<void>} - A promise that resolves when the image has been encoded with all the specified encoders. * @returns {Promise<{ [key in keyof T]: EncodeResult }>} - A promise that resolves when the image has been encoded with all the specified encoders.
*/ */
async encode<T extends EncoderOptions>( async encode<T extends EncoderOptions>(
encodeOptions: { encodeOptions: {
@@ -236,6 +236,7 @@ class Image {
} & T, } & T,
): Promise<{ [key in keyof T]: EncodeResult }> { ): Promise<{ [key in keyof T]: EncodeResult }> {
const { bitmap } = await this.decoded; const { bitmap } = await this.decoded;
const setEncodedWithPromises = [];
for (const [name, options] of Object.entries(encodeOptions)) { for (const [name, options] of Object.entries(encodeOptions)) {
if (!Object.keys(encoders).includes(name)) { if (!Object.keys(encoders).includes(name)) {
continue; continue;
@@ -246,18 +247,25 @@ class Image {
typeof options === 'string' typeof options === 'string'
? options ? options
: Object.assign({}, encRef.defaultEncoderOptions, options); : Object.assign({}, encRef.defaultEncoderOptions, options);
this.encodedWith[encName] = this.workerPool.dispatchJob({ setEncodedWithPromises.push(
operation: 'encode', this.workerPool
bitmap, .dispatchJob({
encName, operation: 'encode',
encConfig, bitmap,
optimizerButteraugliTarget: Number( encName,
encodeOptions.optimizerButteraugliTarget ?? 1.4, encConfig,
), optimizerButteraugliTarget: Number(
maxOptimizerRounds: Number(encodeOptions.maxOptimizerRounds ?? 6), encodeOptions.optimizerButteraugliTarget ?? 1.4,
}); ),
maxOptimizerRounds: Number(encodeOptions.maxOptimizerRounds ?? 6),
})
.then((encodeResult) => {
this.encodedWith[encName] = encodeResult;
}),
);
} }
await Promise.all(Object.values(this.encodedWith));
await Promise.all(setEncodedWithPromises);
return this.encodedWith as { [key in keyof T]: EncodeResult }; return this.encodedWith as { [key in keyof T]: EncodeResult };
} }
} }