canvu-react 0.3.13 → 0.3.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -236,10 +236,32 @@ async function runWithConcurrency(items, concurrency, execute) {
236
236
  async function loadPdfToStore(file, store, options) {
237
237
  const scale = options?.scale ?? 1.5;
238
238
  const pageConcurrency = options?.pageConcurrency ?? 2;
239
+ const storeThumbnails = options?.storeThumbnails ?? false;
239
240
  const pdfjs = await getPdfJs();
240
241
  const arrayBuffer = await file.arrayBuffer();
241
242
  const pdf = await pdfjs.getDocument({ data: arrayBuffer }).promise;
242
243
  const pageNumbers = normalizePdfPageNumbers(options?.pageNumbers, pdf.numPages);
244
+ const bufferedResults = /* @__PURE__ */ new Map();
245
+ let nextEmitIndex = 0;
246
+ let emitChain = Promise.resolve();
247
+ const queuePageEmission = async (pageResult) => {
248
+ bufferedResults.set(pageResult.pageNumber, pageResult);
249
+ const run = async () => {
250
+ while (nextEmitIndex < pageNumbers.length) {
251
+ const nextPageNumber = pageNumbers[nextEmitIndex];
252
+ if (nextPageNumber == null) break;
253
+ const bufferedResult = bufferedResults.get(nextPageNumber);
254
+ if (!bufferedResult) break;
255
+ bufferedResults.delete(nextPageNumber);
256
+ nextEmitIndex += 1;
257
+ await options?.onPageStored?.(bufferedResult);
258
+ }
259
+ };
260
+ const nextChain = emitChain.then(run, run);
261
+ emitChain = nextChain.catch(() => {
262
+ });
263
+ await nextChain;
264
+ };
243
265
  return await runWithConcurrency(
244
266
  pageNumbers,
245
267
  pageConcurrency,
@@ -249,27 +271,31 @@ async function loadPdfToStore(file, store, options) {
249
271
  const mime = "image/png";
250
272
  const pageBlob = await canvasToBlob(canvas, mime);
251
273
  const blobId = await store.storeOriginal(pageBlob);
252
- const thumbScale = Math.min(1, 256 / Math.max(width, height));
253
- const tw = Math.max(1, Math.round(width * thumbScale));
254
- const th = Math.max(1, Math.round(height * thumbScale));
255
- const thumbCanvas = document.createElement("canvas");
256
- thumbCanvas.width = tw;
257
- thumbCanvas.height = th;
258
- const tCtx = thumbCanvas.getContext("2d");
259
- if (tCtx) {
260
- tCtx.imageSmoothingEnabled = true;
261
- tCtx.imageSmoothingQuality = "high";
262
- tCtx.drawImage(canvas, 0, 0, tw, th);
263
- }
264
- const thumbBlob = await canvasToBlob(thumbCanvas, mime);
265
- const thumbnailBlobId = await store.storeThumbnail(thumbBlob);
266
- return {
274
+ const thumbnailBlobId = storeThumbnails ? await (async () => {
275
+ const thumbScale = Math.min(1, 256 / Math.max(width, height));
276
+ const tw = Math.max(1, Math.round(width * thumbScale));
277
+ const th = Math.max(1, Math.round(height * thumbScale));
278
+ const thumbCanvas = document.createElement("canvas");
279
+ thumbCanvas.width = tw;
280
+ thumbCanvas.height = th;
281
+ const tCtx = thumbCanvas.getContext("2d");
282
+ if (tCtx) {
283
+ tCtx.imageSmoothingEnabled = true;
284
+ tCtx.imageSmoothingQuality = "high";
285
+ tCtx.drawImage(canvas, 0, 0, tw, th);
286
+ }
287
+ const thumbBlob = await canvasToBlob(thumbCanvas, mime);
288
+ return await store.storeThumbnail(thumbBlob);
289
+ })() : "";
290
+ const pageResult = {
267
291
  blobId,
268
292
  thumbnailBlobId,
269
293
  width,
270
294
  height,
271
295
  pageNumber
272
296
  };
297
+ await queuePageEmission(pageResult);
298
+ return pageResult;
273
299
  }
274
300
  );
275
301
  }