From 9b79ac57acbe31e710becd7265e96424ed549798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Thu, 12 Sep 2019 01:31:09 +0200 Subject: [PATCH] feat: share worker --- src/components/JsonSchemaViewer.tsx | 23 +++++++++---------- .../__tests__/JsonSchemaViewer.spec.tsx | 15 ++++++++++++ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/components/JsonSchemaViewer.tsx b/src/components/JsonSchemaViewer.tsx index 1eda17a4..897482a8 100644 --- a/src/components/JsonSchemaViewer.tsx +++ b/src/components/JsonSchemaViewer.tsx @@ -40,7 +40,7 @@ export interface IJsonSchemaViewerComponent extends Omit { protected treeStore: TreeStore; protected instanceId: string; - protected schemaWorker?: WebWorker; + public static schemaWorker?: WebWorker; public state = { computing: false, @@ -89,7 +89,8 @@ export class JsonSchemaViewerComponent extends React.PureComponent) { diff --git a/src/components/__tests__/JsonSchemaViewer.spec.tsx b/src/components/__tests__/JsonSchemaViewer.spec.tsx index 0418dd3f..8f14100e 100644 --- a/src/components/__tests__/JsonSchemaViewer.spec.tsx +++ b/src/components/__tests__/JsonSchemaViewer.spec.tsx @@ -210,4 +210,19 @@ describe('JSON Schema Viewer component', () => { expect(wrapper.instance()).toHaveProperty('treeStore.nodes', []); }); + + test('should create one shared instance of schema worker one mounted for a first time and keep it', () => { + const worker = (shallow( + , + ).instance()!.constructor as any).schemaWorker; + + expect(SchemaWorker).toHaveBeenCalledTimes(1); + + expect(worker).toBe( + (shallow().instance()! + .constructor as any).schemaWorker, + ); + + expect(SchemaWorker).toHaveBeenCalledTimes(1); + }); });