@@ -40,7 +40,7 @@ export interface IJsonSchemaViewerComponent extends Omit<IJsonSchemaViewer, 'Fal
4040export class JsonSchemaViewerComponent extends React . PureComponent < IJsonSchemaViewerComponent > {
4141 protected treeStore : TreeStore ;
4242 protected instanceId : string ;
43- protected schemaWorker ?: WebWorker ;
43+ public static schemaWorker ?: WebWorker ;
4444
4545 public state = {
4646 computing : false ,
@@ -89,7 +89,8 @@ export class JsonSchemaViewerComponent extends React.PureComponent<IJsonSchemaVi
8989
9090 protected prerenderSchema ( ) {
9191 const schema = this . schema ;
92- const isWorkerSpawn = this . schemaWorker !== void 0 && ! ( 'isShim' in this . schemaWorker ) ;
92+ const isWorkerSpawn =
93+ JsonSchemaViewerComponent . schemaWorker !== void 0 && ! ( 'isShim' in JsonSchemaViewerComponent . schemaWorker ) ;
9394 let needsFullRendering = isWorkerSpawn ;
9495
9596 const renderedSchema = renderSchema (
@@ -146,23 +147,21 @@ export class JsonSchemaViewerComponent extends React.PureComponent<IJsonSchemaVi
146147 mergeAllOf : this . props . mergeAllOf !== false ,
147148 } ;
148149
149- if ( this . schemaWorker ) {
150- this . schemaWorker . postMessage ( message ) ;
150+ if ( JsonSchemaViewerComponent . schemaWorker !== void 0 ) {
151+ JsonSchemaViewerComponent . schemaWorker . postMessage ( message ) ;
151152 }
152153
153154 this . setState ( { computing : true } ) ;
154155 }
155156
156157 public componentDidMount ( ) {
157- this . schemaWorker = new SchemaWorker ( ) ;
158- this . schemaWorker . addEventListener ( 'message' , this . handleWorkerMessage ) ;
159- this . renderSchema ( ) ;
160- }
161-
162- public componentWillUnmount ( ) {
163- if ( this . schemaWorker !== void 0 ) {
164- this . schemaWorker . terminate ( ) ;
158+ // let's initialize it lazily
159+ if ( JsonSchemaViewerComponent . schemaWorker === void 0 ) {
160+ JsonSchemaViewerComponent . schemaWorker = new SchemaWorker ( ) ;
165161 }
162+
163+ JsonSchemaViewerComponent . schemaWorker . addEventListener ( 'message' , this . handleWorkerMessage ) ;
164+ this . renderSchema ( ) ;
166165 }
167166
168167 public componentDidUpdate ( prevProps : Readonly < IJsonSchemaViewer > ) {
0 commit comments