Skip to content

Commit 0fb265c

Browse files
P0lipbilliegoose
authored andcommitted
feat: share worker (#54)
1 parent f9e11b6 commit 0fb265c

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/components/JsonSchemaViewer.tsx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export interface IJsonSchemaViewerComponent extends Omit<IJsonSchemaViewer, 'Fal
4040
export 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>) {

src/components/__tests__/JsonSchemaViewer.spec.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,19 @@ describe('JSON Schema Viewer component', () => {
210210

211211
expect(wrapper.instance()).toHaveProperty('treeStore.nodes', []);
212212
});
213+
214+
test('should create one shared instance of schema worker one mounted for a first time and keep it', () => {
215+
const worker = (shallow(
216+
<JsonSchemaViewerComponent schema={schema as JSONSchema4} maxRows={0} onError={jest.fn()} />,
217+
).instance()!.constructor as any).schemaWorker;
218+
219+
expect(SchemaWorker).toHaveBeenCalledTimes(1);
220+
221+
expect(worker).toBe(
222+
(shallow(<JsonSchemaViewerComponent schema={schema as JSONSchema4} maxRows={0} onError={jest.fn()} />).instance()!
223+
.constructor as any).schemaWorker,
224+
);
225+
226+
expect(SchemaWorker).toHaveBeenCalledTimes(1);
227+
});
213228
});

0 commit comments

Comments
 (0)