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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"react-dom": "16.9.0",
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-node-resolve": "^5.2.0",
"rollup-plugin-web-worker-loader": "p0lip/rollup-plugin-web-worker-loader",
"rollup-plugin-web-worker-loader": "p0lip/rollup-plugin-web-worker-loader#d833c63590598ec66d051066826b6ce8d57710a9",
"ts-jest": "^24.0.2",
"tslint": "^5.19.0",
"tslint-config-stoplight": "^1.2.0",
Expand Down
18 changes: 11 additions & 7 deletions src/components/JsonSchemaViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Intent, Spinner } from '@stoplight/ui-kit';
import cn from 'classnames';
import { runInAction } from 'mobx';
import * as React from 'react';
import SchemaWorker from 'web-worker:../workers/schema.ts';
import SchemaWorker, { WebWorker } from 'web-worker:../workers/schema.ts';

import { JSONSchema4 } from 'json-schema';
import { GoToRefHandler, RowRenderer, SchemaTreeListNode } from '../types';
Expand Down Expand Up @@ -35,7 +35,7 @@ export interface IJsonSchemaViewer {
export class JsonSchemaViewerComponent extends React.PureComponent<IJsonSchemaViewer> {
protected treeStore: TreeStore;
protected instanceId: string;
protected schemaWorker?: Worker;
protected schemaWorker?: WebWorker;

public state = {
computing: false,
Expand Down Expand Up @@ -82,19 +82,23 @@ export class JsonSchemaViewerComponent extends React.PureComponent<IJsonSchemaVi

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

const renderedSchema = renderSchema(
schema,
0,
{ path: [] },
{
depth: this.expandedDepth + 1,
},
isWorkerSpawn
? {
depth: this.expandedDepth + 1,
}
: void 0,
);

const nodes: SchemaTreeListNode[] = [];

if (this.props.maxRows !== undefined) {
if (isWorkerSpawn && this.props.maxRows !== undefined) {
let i = this.props.maxRows;
let hasAllOf = false;
for (const node of renderedSchema) {
Expand Down
12 changes: 12 additions & 0 deletions src/components/__tests__/JsonSchemaViewer.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ describe('JSON Schema Viewer component', () => {
(SchemaWorker.prototype.addEventListener as jest.Mock).mockClear();
});

afterEach(() => {
delete SchemaWorker.prototype.isShim;
});

test('should render empty message if schema is empty', () => {
(isSchemaViewerEmpty as jest.Mock).mockReturnValue(true);
const wrapper = shallow(<JsonSchemaViewerComponent schema={{}} />);
Expand Down Expand Up @@ -155,6 +159,14 @@ describe('JSON Schema Viewer component', () => {
expect(wrapper.instance()).toHaveProperty('treeStore.nodes', nodes);
});

test('should render all nodes on main thread when worker cannot be spawned regardless of maxRows or schema', () => {
SchemaWorker.prototype.isShim = true;
const wrapper = shallow(<JsonSchemaViewerComponent schema={schema as JSONSchema4} maxRows={1} />);

expect(SchemaWorker.prototype.postMessage).not.toHaveBeenCalled();
expect(wrapper.instance()).toHaveProperty('treeStore.nodes.length', 4);
});

test('should not apply result of full processing in a worker if instance ids do not match', () => {
const wrapper = shallow(<JsonSchemaViewerComponent schema={schema as JSONSchema4} maxRows={0} />);
expect(SchemaWorker.prototype.postMessage).toHaveBeenCalledWith({
Expand Down
12 changes: 9 additions & 3 deletions web-worker-loader.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
declare module 'web-worker:*' {
declare var WebWorker: {
prototype: Worker;
new (): Worker;
// tslint:disable-next-line:interface-name
declare interface ShimmedWebWorker extends Worker {
isShim: true;
}

declare const WebWorker: {
prototype: Worker | ShimmedWebWorker;
new (): Worker | ShimmedWebWorker;
};

export default WebWorker;
export type WebWorker = Worker | ShimmedWebWorker;
}

declare module 'rollup-plugin-web-worker-loader-helper' {
Expand Down
36 changes: 7 additions & 29 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5354,7 +5354,7 @@ debug@^4.0.0, debug@^4.0.1, debug@^4.1.0, debug@^4.1.1:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -7580,7 +7580,7 @@ import-local@^2.0.0:
pkg-dir "^3.0.0"
resolve-cwd "^2.0.0"

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -9261,11 +9261,6 @@ lodash-es@^4.17.11, lodash-es@^4.17.15:
resolved "https://registry.yarnpkg.com/lodash-es/-/lodash-es-4.17.15.tgz#21bd96839354412f23d7a10340e5eac6ee455d78"
integrity sha512-rlrc3yU3+JNOpZ9zj5pQtxnx2THmvRykwL4Xlxoa8I9lHBlVbbyPhgyPMioxVZ4NqyxaVVtaJnzsyOidQIhyyQ==

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseisequal@^3.0.0:
version "3.0.7"
resolved "https://registry.yarnpkg.com/lodash._baseisequal/-/lodash._baseisequal-3.0.7.tgz#d8025f76339d29342767dcc887ce5cb95a5b51f1"
Expand All @@ -9283,29 +9278,17 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*, lodash._bindcallback@^3.0.0:
lodash._bindcallback@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=
Expand Down Expand Up @@ -9422,11 +9405,6 @@ lodash.pick@^4.4.0:
resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"
integrity sha1-UvBWEP/53tQiYRRB7R/BI6AwAbM=

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

lodash.set@^4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23"
Expand Down Expand Up @@ -13014,9 +12992,9 @@ rollup-plugin-typescript2@^0.21.1:
rollup-pluginutils "2.6.0"
tslib "1.9.3"

rollup-plugin-web-worker-loader@p0lip/rollup-plugin-web-worker-loader:
version "0.5.2"
resolved "https://codeload.github.com/p0lip/rollup-plugin-web-worker-loader/tar.gz/c06acd26282c537e03d30848dcabecd4581e4111"
rollup-plugin-web-worker-loader@p0lip/rollup-plugin-web-worker-loader#d833c63590598ec66d051066826b6ce8d57710a9:
version "0.5.3"
resolved "https://codeload.github.com/p0lip/rollup-plugin-web-worker-loader/tar.gz/d833c63590598ec66d051066826b6ce8d57710a9"

rollup-pluginutils@2.6.0:
version "2.6.0"
Expand Down