Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/components/JsonSchemaViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export interface IJsonSchemaViewerComponent extends Omit<IJsonSchemaViewer, 'Fal
export class JsonSchemaViewerComponent extends React.PureComponent<IJsonSchemaViewerComponent> {
protected treeStore: TreeStore;
protected instanceId: string;
protected schemaWorker?: WebWorker;
public static schemaWorker?: WebWorker;

public state = {
computing: false,
Expand Down Expand Up @@ -89,7 +89,8 @@ export class JsonSchemaViewerComponent extends React.PureComponent<IJsonSchemaVi

protected prerenderSchema() {
const schema = this.schema;
const isWorkerSpawn = this.schemaWorker !== void 0 && !('isShim' in this.schemaWorker);
const isWorkerSpawn =
JsonSchemaViewerComponent.schemaWorker !== void 0 && !('isShim' in JsonSchemaViewerComponent.schemaWorker);
let needsFullRendering = isWorkerSpawn;

const renderedSchema = renderSchema(
Expand Down Expand Up @@ -146,23 +147,21 @@ export class JsonSchemaViewerComponent extends React.PureComponent<IJsonSchemaVi
mergeAllOf: this.props.mergeAllOf !== false,
};

if (this.schemaWorker) {
this.schemaWorker.postMessage(message);
if (JsonSchemaViewerComponent.schemaWorker !== void 0) {
JsonSchemaViewerComponent.schemaWorker.postMessage(message);
}

this.setState({ computing: true });
}

public componentDidMount() {
this.schemaWorker = new SchemaWorker();
this.schemaWorker.addEventListener('message', this.handleWorkerMessage);
this.renderSchema();
}

public componentWillUnmount() {
if (this.schemaWorker !== void 0) {
this.schemaWorker.terminate();
// let's initialize it lazily
if (JsonSchemaViewerComponent.schemaWorker === void 0) {
JsonSchemaViewerComponent.schemaWorker = new SchemaWorker();
}

JsonSchemaViewerComponent.schemaWorker.addEventListener('message', this.handleWorkerMessage);
this.renderSchema();
}

public componentDidUpdate(prevProps: Readonly<IJsonSchemaViewer>) {
Expand Down
15 changes: 15 additions & 0 deletions src/components/__tests__/JsonSchemaViewer.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<JsonSchemaViewerComponent schema={schema as JSONSchema4} maxRows={0} onError={jest.fn()} />,
).instance()!.constructor as any).schemaWorker;

expect(SchemaWorker).toHaveBeenCalledTimes(1);

expect(worker).toBe(
(shallow(<JsonSchemaViewerComponent schema={schema as JSONSchema4} maxRows={0} onError={jest.fn()} />).instance()!
.constructor as any).schemaWorker,
);

expect(SchemaWorker).toHaveBeenCalledTimes(1);
});
});