@@ -1339,12 +1339,10 @@ describe("api", function () {
13391339 . catch ( done . fail ) ;
13401340 } ) ;
13411341
1342- it ( "cleans up document resources" , function ( done ) {
1343- const promise = pdfDocument . cleanup ( ) ;
1344- promise . then ( function ( ) {
1345- expect ( true ) . toEqual ( true ) ;
1346- done ( ) ;
1347- } , done . fail ) ;
1342+ it ( "cleans up document resources" , async function ( ) {
1343+ await pdfDocument . cleanup ( ) ;
1344+
1345+ expect ( true ) . toEqual ( true ) ;
13481346 } ) ;
13491347
13501348 it ( "checks that fingerprints are unique" , function ( done ) {
@@ -1982,85 +1980,69 @@ describe("api", function () {
19821980 ] ) . then ( done ) ;
19831981 } ) ;
19841982
1985- it ( "cleans up document resources after rendering of page" , function ( done ) {
1983+ it ( "cleans up document resources after rendering of page" , async function ( ) {
19861984 const loadingTask = getDocument ( buildGetDocumentParams ( basicApiFileName ) ) ;
1987- let canvasAndCtx ;
1985+ const pdfDoc = await loadingTask . promise ;
1986+ const pdfPage = await pdfDoc . getPage ( 1 ) ;
19881987
1989- loadingTask . promise
1990- . then ( pdfDoc => {
1991- return pdfDoc . getPage ( 1 ) . then ( pdfPage => {
1992- const viewport = pdfPage . getViewport ( { scale : 1 } ) ;
1993- canvasAndCtx = CanvasFactory . create (
1994- viewport . width ,
1995- viewport . height
1996- ) ;
1988+ const viewport = pdfPage . getViewport ( { scale : 1 } ) ;
1989+ const canvasAndCtx = CanvasFactory . create (
1990+ viewport . width ,
1991+ viewport . height
1992+ ) ;
19971993
1998- const renderTask = pdfPage . render ( {
1999- canvasContext : canvasAndCtx . context ,
2000- canvasFactory : CanvasFactory ,
2001- viewport,
2002- } ) ;
2003- return renderTask . promise . then ( ( ) => {
2004- return pdfDoc . cleanup ( ) ;
2005- } ) ;
2006- } ) ;
2007- } )
2008- . then ( ( ) => {
2009- expect ( true ) . toEqual ( true ) ;
1994+ const renderTask = pdfPage . render ( {
1995+ canvasContext : canvasAndCtx . context ,
1996+ canvasFactory : CanvasFactory ,
1997+ viewport,
1998+ } ) ;
1999+ await renderTask . promise ;
20102000
2011- CanvasFactory . destroy ( canvasAndCtx ) ;
2012- loadingTask . destroy ( ) . then ( done ) ;
2013- } , done . fail ) ;
2001+ await pdfDoc . cleanup ( ) ;
2002+
2003+ expect ( true ) . toEqual ( true ) ;
2004+
2005+ CanvasFactory . destroy ( canvasAndCtx ) ;
2006+ await loadingTask . destroy ( ) ;
20142007 } ) ;
20152008
2016- it ( "cleans up document resources during rendering of page" , function ( done ) {
2009+ it ( "cleans up document resources during rendering of page" , async function ( ) {
20172010 const loadingTask = getDocument (
20182011 buildGetDocumentParams ( "tracemonkey.pdf" )
20192012 ) ;
2020- let canvasAndCtx ;
2013+ const pdfDoc = await loadingTask . promise ;
2014+ const pdfPage = await pdfDoc . getPage ( 1 ) ;
20212015
2022- loadingTask . promise
2023- . then ( pdfDoc => {
2024- return pdfDoc . getPage ( 1 ) . then ( pdfPage => {
2025- const viewport = pdfPage . getViewport ( { scale : 1 } ) ;
2026- canvasAndCtx = CanvasFactory . create (
2027- viewport . width ,
2028- viewport . height
2029- ) ;
2016+ const viewport = pdfPage . getViewport ( { scale : 1 } ) ;
2017+ const canvasAndCtx = CanvasFactory . create (
2018+ viewport . width ,
2019+ viewport . height
2020+ ) ;
20302021
2031- const renderTask = pdfPage . render ( {
2032- canvasContext : canvasAndCtx . context ,
2033- canvasFactory : CanvasFactory ,
2034- viewport,
2035- } ) ;
2022+ const renderTask = pdfPage . render ( {
2023+ canvasContext : canvasAndCtx . context ,
2024+ canvasFactory : CanvasFactory ,
2025+ viewport,
2026+ } ) ;
2027+ // Ensure that clean-up runs during rendering.
2028+ renderTask . onContinue = function ( cont ) {
2029+ waitSome ( cont ) ;
2030+ } ;
20362031
2037- renderTask . onContinue = function ( cont ) {
2038- waitSome ( cont ) ;
2039- } ;
2032+ try {
2033+ await pdfDoc . cleanup ( ) ;
20402034
2041- return pdfDoc
2042- . cleanup ( )
2043- . then (
2044- ( ) => {
2045- throw new Error ( "shall fail cleanup" ) ;
2046- } ,
2047- reason => {
2048- expect ( reason instanceof Error ) . toEqual ( true ) ;
2049- expect ( reason . message ) . toEqual (
2050- "startCleanup: Page 1 is currently rendering."
2051- ) ;
2052- }
2053- )
2054- . then ( ( ) => {
2055- return renderTask . promise ;
2056- } )
2057- . then ( ( ) => {
2058- CanvasFactory . destroy ( canvasAndCtx ) ;
2059- loadingTask . destroy ( ) . then ( done ) ;
2060- } ) ;
2061- } ) ;
2062- } )
2063- . catch ( done . fail ) ;
2035+ throw new Error ( "shall fail cleanup" ) ;
2036+ } catch ( reason ) {
2037+ expect ( reason instanceof Error ) . toEqual ( true ) ;
2038+ expect ( reason . message ) . toEqual (
2039+ "startCleanup: Page 1 is currently rendering."
2040+ ) ;
2041+ }
2042+ await renderTask . promise ;
2043+
2044+ CanvasFactory . destroy ( canvasAndCtx ) ;
2045+ await loadingTask . destroy ( ) ;
20642046 } ) ;
20652047
20662048 it ( "caches image resources at the document/page level as expected (issue 11878)" , async function ( ) {
0 commit comments