@@ -23,8 +23,9 @@ const parsingOptions: ParsingOptions = {
2323} ;
2424
2525interface Snapshot {
26- image : string
27- id : string
26+ id : string ;
27+ image : string | null ;
28+ error : string | null ;
2829}
2930
3031const core = init ( )
@@ -36,70 +37,69 @@ function generateNMRiumURL() {
3637 return url . toString ( )
3738}
3839
39- async function captureSpectraViewAsBase64 ( nmriumState : Partial < NmriumState > ) {
40- const { data : { spectra } = { spectra : [ ] } , version } = nmriumState
41- const browser = await playwright . chromium . launch ( )
42- const context = await browser . newContext (
43- playwright . devices [ 'Desktop Chrome HiDPI' ]
44- )
45- const page = await context . newPage ( )
46-
47- const url = generateNMRiumURL ( )
40+ async function launchBrowser ( ) {
41+ return playwright . chromium . launch ( ) ;
42+ }
4843
49- await page . goto ( url )
44+ async function captureSpectraViewAsBase64 ( nmriumState : Partial < NmriumState > ) : Promise < Snapshot [ ] > {
45+ const { data : { spectra } = { spectra : [ ] } , version } = nmriumState ;
5046
51- await page . locator ( 'text=Loading' ) . waitFor ( { state : 'hidden' } )
47+ if ( ! spectra ?. length ) return [ ] ;
5248
53- let snapshots : Snapshot [ ] = [ ]
49+ const url = generateNMRiumURL ( ) ;
50+ const snapshots : Snapshot [ ] = [ ] ;
51+ let browser = await launchBrowser ( ) ;
5452
55- for ( const spectrum of spectra || [ ] ) {
56- const spectrumObject = {
57- version,
58- data : {
59- spectra : [ { ...spectrum } ] ,
60- } ,
61- }
53+ for ( const spectrum of spectra ) {
54+ let context = null ;
6255
63- // convert typed array to array
64- const stringObject = JSON . stringify (
65- spectrumObject ,
66- ( key , value : unknown ) => {
67- return ArrayBuffer . isView ( value )
68- ? Array . from ( value as unknown as Iterable < unknown > )
69- : value
56+ try {
57+ // recreate browser if it has crashed
58+ if ( ! browser . isConnected ( ) ) {
59+ browser = await launchBrowser ( ) ;
7060 }
71- )
7261
73- // load the spectrum into NMRium using the custom event
74- await page . evaluate (
75- `
76- window.postMessage({ type: "nmr-wrapper:load", data:{data: ${ stringObject } ,type:"nmrium"}}, '*');
77- `
78- )
62+ context = await browser . newContext ( playwright . devices [ 'Desktop Chrome HiDPI' ] ) ;
63+ const page = await context . newPage ( ) ;
7964
80- //wait for NMRium process and load spectra
81- await page . locator ( 'text=Loading' ) . waitFor ( { state : 'hidden' } )
65+ await page . goto ( url ) ;
66+ await page . locator ( 'text=Loading' ) . waitFor ( { state : 'hidden' } ) ;
8267
83- // take a snapshot for the spectrum
84- try {
85- const snapshot = await page . locator ( '#nmrSVG .container' ) . screenshot ( )
68+ const stringObject = JSON . stringify (
69+ { version, data : { spectra : [ { ...spectrum } ] } } ,
70+ ( key , value : unknown ) => ArrayBuffer . isView ( value ) ? Array . from ( value as unknown as Iterable < unknown > ) : value
71+ ) ;
72+
73+ await page . evaluate ( `
74+ window.postMessage({ type: "nmr-wrapper:load", data: { data: ${ stringObject } , type: "nmrium" } }, '*');
75+ ` ) ;
76+
77+ await page . locator ( 'text=Loading' ) . waitFor ( { state : 'hidden' } ) ;
8678
79+ const snapshot = await page . locator ( '#nmrSVG .container' ) . screenshot ( ) ;
80+ snapshots . push ( { id : spectrum . id , image : snapshot . toString ( 'base64' ) , error : null } ) ;
81+
82+ } catch ( e ) {
8783 snapshots . push ( {
88- image : snapshot . toString ( 'base64' ) ,
8984 id : spectrum . id ,
90- } )
91- } catch ( e ) {
92- console . log ( e )
93- }
94- }
85+ image : null ,
86+ error : e instanceof Error ? e . message : String ( e ) ,
87+ } ) ;
9588
96- await context . close ( )
97- await browser . close ( )
89+ // browser crashed — close and recreate for next spectrum
90+ await browser . close ( ) . catch ( ( ) => { } ) ;
91+ browser = await launchBrowser ( ) ;
9892
99- return snapshots
93+ } finally {
94+ await context ?. close ( ) . catch ( ( ) => { } ) ;
95+ }
96+ }
10097
98+ await browser . close ( ) . catch ( ( ) => { } ) ;
99+ return snapshots ;
101100}
102101
102+
103103interface ProcessSpectraOptions {
104104 autoDetection : boolean ; autoProcessing : boolean ;
105105}
0 commit comments