Skip to content

Commit

Permalink
feat: add support for worker environment
Browse files Browse the repository at this point in the history
ApiDOM playground now demonstrates that offloading all processing
of apidom packages works in web worker environment.

For actual implementation of web worker I've used library
called Comlink to abstract web worker communication with
RPC protocol.

For referencing the global object in different environments
we now use globalThis symbol which abstracts environments
and is standardized.

Refs #287
  • Loading branch information
char0n committed Mar 3, 2021
1 parent 8eb0f47 commit b375f57
Show file tree
Hide file tree
Showing 12 changed files with 317 additions and 230 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { isString } from 'ramda-adjunct';
import treeSitterWasm from 'web-tree-sitter/tree-sitter.wasm';

// patch fetch() to let emscripten load the WASM file
const realFetch = window.fetch;
window.fetch = (...args) => {
const realFetch = globalThis.fetch;
globalThis.fetch = (...args) => {
// @ts-ignore
if (isString(args[0]) && args[0].endsWith('/tree-sitter.wasm')) {
// @ts-ignore
return realFetch.apply(window, [treeSitterWasm, tail(args)]);
return realFetch.apply(globalThis, [treeSitterWasm, tail(args)]);
}
return realFetch.apply(window, args);
return realFetch.apply(globalThis, args);
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { isString } from 'ramda-adjunct';
import treeSitterWasm from 'web-tree-sitter/tree-sitter.wasm';

// patch fetch() to let emscripten load the WASM file
const realFetch = window.fetch;
window.fetch = (...args) => {
const realFetch = globalThis.fetch;
globalThis.fetch = (...args) => {
// @ts-ignore
if (isString(args[0]) && args[0].endsWith('/tree-sitter.wasm')) {
// @ts-ignore
return realFetch.apply(window, [treeSitterWasm, tail(args)]);
return realFetch.apply(globalThis, [treeSitterWasm, tail(args)]);
}
return realFetch.apply(window, args);
return realFetch.apply(globalThis, args);
};
2 changes: 1 addition & 1 deletion apidom/packages/apidom-reference/src/util/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export const getHash = (uri: string): string => {
export const cwd = (): string => {
// @ts-ignore
if (process.browser) {
return window.location.href;
return globalThis.location.href;
}

const path = process.cwd();
Expand Down
2 changes: 1 addition & 1 deletion experiments/apidom-playground/babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const apiDOMResolverPlugin = [
{
root: ['./src'],
alias: {
apidom: '../../apidom/packages/apidom/cjs',
apidom: '../../apidom/packages/apidom',
'apidom-ast': '../../apidom/packages/apidom-ast',
'apidom-ns-asyncapi-2-0': '../../apidom/packages/apidom-ns-asyncapi-2-0',
'apidom-ns-openapi-3-1': '../../apidom/packages/apidom-ns-openapi-3-1',
Expand Down
7 changes: 3 additions & 4 deletions experiments/apidom-playground/config/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const getPublicUrlOrPath = require('react-dev-utils/getPublicUrlOrPath');
// Make sure any symlinks in the project folder are resolved:
// https://github.com/facebook/create-react-app/issues/637
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);

// We use `PUBLIC_URL` environment variable or "homepage" field to infer
// "public path" at which the app is served.
Expand Down Expand Up @@ -37,7 +37,7 @@ const moduleFileExtensions = [

// Resolve file paths in the same order as webpack
const resolveModule = (resolveFn, filePath) => {
const extension = moduleFileExtensions.find(extension =>
const extension = moduleFileExtensions.find((extension) =>
fs.existsSync(resolveFn(`${filePath}.${extension}`))
);

Expand Down Expand Up @@ -65,9 +65,8 @@ module.exports = {
proxySetup: resolveApp('src/setupProxy.js'),
appNodeModules: resolveApp('node_modules'),
swSrc: resolveModule(resolveApp, 'src/service-worker'),
apidomPath: path.resolve(__dirname, '..', '..', '..', 'apidom', 'packages'),
publicUrlOrPath,
};



module.exports.moduleFileExtensions = moduleFileExtensions;
Loading

0 comments on commit b375f57

Please sign in to comment.