@@ -47,8 +47,9 @@ import {
4747 ImageDataLike ,
4848 IPdfiumExecutor ,
4949 AnnotationAppearanceMap ,
50+ RenderPriority as Priority ,
5051} from '@embedpdf/models' ;
51- import { WorkerTaskQueue , Priority } from './task-queue' ;
52+ import { WorkerTaskQueue } from './task-queue' ;
5253import type { ImageDataConverter } from '../converters/types' ;
5354
5455// Re-export for convenience
@@ -375,38 +376,18 @@ export class PdfEngine<T = Blob> implements IPdfEngine<T> {
375376 page : PdfPageObject ,
376377 options ?: PdfRenderPageAnnotationOptions ,
377378 ) : PdfTask < AnnotationAppearanceMap < T > > {
378- const resultTask = new Task < AnnotationAppearanceMap < T > , PdfErrorReason > ( ) ;
379-
380- const renderHandle = this . workerQueue . enqueue (
381- {
382- execute : ( ) => this . executor . renderPageAnnotationsRaw ( doc , page , options ) ,
383- meta : { docId : doc . id , pageIndex : page . index , operation : 'renderPageAnnotationsRaw' } ,
384- } ,
385- { priority : Priority . MEDIUM } ,
386- ) ;
387-
388- // Wire up abort: when resultTask is aborted, also abort the queue task
389- const originalAbort = resultTask . abort . bind ( resultTask ) ;
390- resultTask . abort = ( reason ) => {
391- renderHandle . abort ( reason ) ;
392- originalAbort ( reason ) ;
393- } ;
394-
395- renderHandle . wait (
396- ( rawMap ) => {
397- if ( resultTask . state . stage !== 0 /* Pending */ ) {
398- return ;
399- }
400- this . encodeAppearanceMap ( rawMap , options , resultTask ) ;
401- } ,
402- ( error ) => {
403- if ( resultTask . state . stage === 0 /* Pending */ ) {
404- resultTask . fail ( error ) ;
405- }
406- } ,
407- ) ;
408-
409- return resultTask ;
379+ return this . workerQueue
380+ . enqueue (
381+ {
382+ execute : ( ) => this . executor . renderPageAnnotationsRaw ( doc , page , options ) ,
383+ meta : { docId : doc . id , pageIndex : page . index , operation : 'renderPageAnnotationsRaw' } ,
384+ } ,
385+ { priority : Priority . MEDIUM } ,
386+ )
387+ . map (
388+ ( rawMap ) => this . encodeAppearanceMap ( rawMap , options ) ,
389+ ( err : unknown ) => ( { code : PdfErrorCode . Unknown , message : String ( err ) } ) ,
390+ ) ;
410391 }
411392
412393 renderPageAnnotationsRaw (
@@ -433,51 +414,24 @@ export class PdfEngine<T = Blob> implements IPdfEngine<T> {
433414 pageIndex ?: number ,
434415 priority : Priority = Priority . CRITICAL ,
435416 ) : PdfTask < T > {
436- const resultTask = new Task < T , PdfErrorReason > ( ) ;
437-
438- // Step 1: Add HIGH/MEDIUM priority task to render raw bytes
439- const renderHandle = this . workerQueue . enqueue (
440- {
441- execute : ( ) => renderFn ( ) ,
442- meta : { docId, pageIndex, operation : 'render' } ,
443- } ,
444- { priority } ,
445- ) ;
446-
447- // Wire up abort: when resultTask is aborted, also abort the queue task
448- const originalAbort = resultTask . abort . bind ( resultTask ) ;
449- resultTask . abort = ( reason ) => {
450- renderHandle . abort ( reason ) ; // Cancel the queue task!
451- originalAbort ( reason ) ;
452- } ;
453-
454- renderHandle . wait (
455- ( rawImageData ) => {
456- // Check if resultTask was already aborted before encoding
457- if ( resultTask . state . stage !== 0 /* Pending */ ) {
458- return ;
459- }
460- this . encodeImage ( rawImageData , options , resultTask ) ;
461- } ,
462- ( error ) => {
463- // Only forward error if resultTask is still pending
464- if ( resultTask . state . stage === 0 /* Pending */ ) {
465- resultTask . fail ( error ) ;
466- }
467- } ,
468- ) ;
469-
470- return resultTask ;
417+ return this . workerQueue
418+ . enqueue (
419+ {
420+ execute : ( ) => renderFn ( ) ,
421+ meta : { docId, pageIndex, operation : 'render' } ,
422+ } ,
423+ { priority } ,
424+ )
425+ . map (
426+ ( rawImageData ) => this . encodeImage ( rawImageData , options ) ,
427+ ( err : unknown ) => ( { code : PdfErrorCode . Unknown , message : String ( err ) } ) ,
428+ ) ;
471429 }
472430
473431 /**
474432 * Encode image using encoder pool or inline
475433 */
476- private encodeImage (
477- rawImageData : ImageDataLike ,
478- options : any ,
479- resultTask : Task < T , PdfErrorReason > ,
480- ) : void {
434+ private encodeImage ( rawImageData : ImageDataLike , options : any ) : Promise < T > {
481435 const imageType = options ?. imageType ?? 'image/webp' ;
482436 const quality = options ?. quality ;
483437
@@ -488,25 +442,16 @@ export class PdfEngine<T = Blob> implements IPdfEngine<T> {
488442 height : rawImageData . height ,
489443 } ;
490444
491- const sizeLabel = `${ rawImageData . width } x${ rawImageData . height } ` ;
492- this . logger . perf ( LOG_SOURCE , 'encodeImage' , 'encode' , 'Begin' , sizeLabel ) ;
493- this . options
494- . imageConverter ( ( ) => plainImageData , imageType , quality )
495- . then ( ( result ) => {
496- this . logger . perf ( LOG_SOURCE , 'encodeImage' , 'encode' , 'End' , sizeLabel ) ;
497- resultTask . resolve ( result ) ;
498- } )
499- . catch ( ( error ) => resultTask . reject ( { code : PdfErrorCode . Unknown , message : String ( error ) } ) ) ;
445+ return this . options . imageConverter ( ( ) => plainImageData , imageType , quality ) ;
500446 }
501447
502448 /**
503449 * Encode a full annotation appearance map to the output type T.
504450 */
505- private encodeAppearanceMap (
451+ private async encodeAppearanceMap (
506452 rawMap : AnnotationAppearanceMap < ImageDataLike > ,
507453 options : PdfRenderPageAnnotationOptions | undefined ,
508- resultTask : Task < AnnotationAppearanceMap < T > , PdfErrorReason > ,
509- ) : void {
454+ ) : Promise < AnnotationAppearanceMap < T > > {
510455 const imageType = options ?. imageType ?? 'image/webp' ;
511456 const quality = options ?. imageQuality ;
512457
@@ -542,17 +487,8 @@ export class PdfEngine<T = Blob> implements IPdfEngine<T> {
542487 }
543488 }
544489
545- Promise . all ( jobs )
546- . then ( ( ) => {
547- if ( resultTask . state . stage === 0 /* Pending */ ) {
548- resultTask . resolve ( encodedMap ) ;
549- }
550- } )
551- . catch ( ( error ) => {
552- if ( resultTask . state . stage === 0 /* Pending */ ) {
553- resultTask . reject ( { code : PdfErrorCode . Unknown , message : String ( error ) } ) ;
554- }
555- } ) ;
490+ await Promise . all ( jobs ) ;
491+ return encodedMap ;
556492 }
557493
558494 // ========== Annotations ==========
0 commit comments