From c2766371bbb2ccc2f8534ff516871660a6a5e5ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Thu, 5 Sep 2019 19:46:55 +0200 Subject: [PATCH 1/2] feat: shim web worker --- package.json | 2 +- src/components/JsonSchemaViewer.tsx | 18 +++++++++++------- .../__tests__/JsonSchemaViewer.spec.tsx | 12 ++++++++++++ web-worker-loader.d.ts | 12 +++++++++--- yarn.lock | 4 ++-- 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index 5b5f031b..824ee146 100644 --- a/package.json +++ b/package.json @@ -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#shim", "ts-jest": "^24.0.2", "tslint": "^5.19.0", "tslint-config-stoplight": "^1.2.0", diff --git a/src/components/JsonSchemaViewer.tsx b/src/components/JsonSchemaViewer.tsx index 5352de1d..571000a4 100644 --- a/src/components/JsonSchemaViewer.tsx +++ b/src/components/JsonSchemaViewer.tsx @@ -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'; @@ -35,7 +35,7 @@ export interface IJsonSchemaViewer { export class JsonSchemaViewerComponent extends React.PureComponent { protected treeStore: TreeStore; protected instanceId: string; - protected schemaWorker?: Worker; + protected schemaWorker?: WebWorker; public state = { computing: false, @@ -82,19 +82,23 @@ export class JsonSchemaViewerComponent extends React.PureComponent { (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(); @@ -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(); + + 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(); expect(SchemaWorker.prototype.postMessage).toHaveBeenCalledWith({ diff --git a/web-worker-loader.d.ts b/web-worker-loader.d.ts index 7972ef7c..96d88367 100644 --- a/web-worker-loader.d.ts +++ b/web-worker-loader.d.ts @@ -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' { diff --git a/yarn.lock b/yarn.lock index 26ed8ce2..535a8280 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13014,9 +13014,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: +rollup-plugin-web-worker-loader@p0lip/rollup-plugin-web-worker-loader#shim: version "0.5.2" - resolved "https://codeload.github.com/p0lip/rollup-plugin-web-worker-loader/tar.gz/c06acd26282c537e03d30848dcabecd4581e4111" + resolved "https://codeload.github.com/p0lip/rollup-plugin-web-worker-loader/tar.gz/8ad95a47f5d753913563dcbb183c7328ea144b8c" rollup-pluginutils@2.6.0: version "2.6.0" From 35d2a526691262483676e6cfc57250ad227b30cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Thu, 5 Sep 2019 21:36:56 +0200 Subject: [PATCH 2/2] chore: bump dep lol --- package.json | 2 +- yarn.lock | 36 +++++++----------------------------- 2 files changed, 8 insertions(+), 30 deletions(-) diff --git a/package.json b/package.json index 824ee146..27d2c5b4 100644 --- a/package.json +++ b/package.json @@ -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#shim", + "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", diff --git a/yarn.lock b/yarn.lock index 535a8280..f1ca7f7e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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= @@ -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= @@ -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" @@ -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= @@ -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" @@ -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#shim: - version "0.5.2" - resolved "https://codeload.github.com/p0lip/rollup-plugin-web-worker-loader/tar.gz/8ad95a47f5d753913563dcbb183c7328ea144b8c" +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"