Moving worker and worker bridge

This commit is contained in:
Jake Archibald
2020-09-25 13:49:58 +01:00
parent f96ae9bdee
commit b47d6b4696
8 changed files with 48 additions and 54 deletions

7
.gitignore vendored
View File

@@ -6,7 +6,6 @@ build
*.o *.o
# Auto-generated by lib/image-worker-plugin.js # Auto-generated by lib/image-worker-plugin.js
src/features/worker/index.ts src/features-worker/index.ts
src/features/worker/tsconfig.json src/features-worker/tsconfig.json
src/features/worker/bridge/meta.ts src/client/lazy-app/worker-bridge/meta.ts
src/features/worker/bridge/tsconfig.json

View File

@@ -24,17 +24,34 @@ export default function () {
return { return {
name: 'image-worker-plugin', name: 'image-worker-plugin',
async buildStart() { async buildStart() {
const base = path.join(process.cwd(), 'src', 'features', 'worker'); const featuresWorkerBase = path.join(
process.cwd(),
'src',
'features-worker',
);
const featuresWorkerBridgeBase = path.join(
process.cwd(),
'src',
'client',
'lazy-app',
'worker-bridge',
);
const tsImports = ( const tsImports = (
await globP('../*/**/worker/*.ts', { await globP('src/features/*/**/worker/*.ts', {
cwd: base, absolute: true,
}) })
) )
.filter((tsFile) => !tsFile.endsWith('.d.ts')) .filter((tsFile) => !tsFile.endsWith('.d.ts'))
.map((tsFile) => tsFile.slice(0, -'.ts'.length)); .map((tsFile) => tsFile.slice(0, -'.ts'.length));
const tsNames = tsImports.map((tsImport) => [ const featuresWorkerTsNames = tsImports.map((tsImport) => [
tsImport, path.relative(featuresWorkerBase, tsImport),
path.basename(tsImport),
]);
const featuresWorkerBridgeTsNames = tsImports.map((tsImport) => [
path.relative(featuresWorkerBridgeBase, tsImport),
path.basename(tsImport), path.basename(tsImport),
]); ]);
@@ -42,9 +59,11 @@ export default function () {
`// This file is autogenerated by lib/image-worker-plugin.js`, `// This file is autogenerated by lib/image-worker-plugin.js`,
`import { expose } from 'comlink';`, `import { expose } from 'comlink';`,
`import { timed } from './util';`, `import { timed } from './util';`,
tsNames.map(([path, name]) => `import ${name} from './${path}';`), featuresWorkerTsNames.map(
([path, name]) => `import ${name} from './${path}';`,
),
`const exports = {`, `const exports = {`,
tsNames.map(([_, name]) => [ featuresWorkerTsNames.map(([_, name]) => [
` ${name}(`, ` ${name}(`,
` ...args: Parameters<typeof ${name}>`, ` ...args: Parameters<typeof ${name}>`,
` ): ReturnType<typeof ${name}> {`, ` ): ReturnType<typeof ${name}> {`,
@@ -62,40 +81,28 @@ export default function () {
if (previousWorkerContent === workerFile) return; if (previousWorkerContent === workerFile) return;
previousWorkerContent = workerFile; previousWorkerContent = workerFile;
const tsConfigReferences = tsImports.map((tsImport) => ({
path: path.dirname(tsImport),
}));
const workerTsConfig = { const workerTsConfig = {
extends: '../../../generic-tsconfig.json', extends: '../../generic-tsconfig.json',
compilerOptions: { compilerOptions: {
lib: ['webworker', 'esnext'], lib: ['webworker', 'esnext'],
}, },
references: tsConfigReferences, references: featuresWorkerTsNames.map(([tsImport]) => ({
}; path: path.dirname(tsImport),
const bridgeTsConfig = {
extends: '../../../../generic-tsconfig.json',
compilerOptions: {
lib: ['esnext', 'dom', 'dom.iterable'],
types: [],
},
include: ['../../../client/lazy-app/util.ts', '**/*.ts'],
references: tsConfigReferences.map((ref) => ({
path: path.join('..', ref.path),
})), })),
}; };
const bridgeMeta = [ const bridgeMeta = [
`// This file is autogenerated by lib/image-worker-plugin.js`, `// This file is autogenerated by lib/image-worker-plugin.js`,
tsNames.map(([path, name]) => `import type ${name} from '../${path}';`), featuresWorkerBridgeTsNames.map(
([path, name]) => `import type ${name} from '${path}';`,
),
`export const methodNames = ${JSON.stringify( `export const methodNames = ${JSON.stringify(
tsNames.map(([_, name]) => name), featuresWorkerBridgeTsNames.map(([_, name]) => name),
null, null,
' ', ' ',
)} as const;`, )} as const;`,
`export interface BridgeMethods {`, `export interface BridgeMethods {`,
tsNames.map(([_, name]) => [ featuresWorkerBridgeTsNames.map(([_, name]) => [
` ${name}(`, ` ${name}(`,
` signal: AbortSignal,`, ` signal: AbortSignal,`,
` ...args: Parameters<typeof ${name}>`, ` ...args: Parameters<typeof ${name}>`,
@@ -108,15 +115,14 @@ export default function () {
await Promise.all([ await Promise.all([
fsp.writeFile( fsp.writeFile(
path.join(base, 'tsconfig.json'), path.join(featuresWorkerBase, 'tsconfig.json'),
JSON.stringify(workerTsConfig, null, ' '), JSON.stringify(workerTsConfig, null, ' '),
), ),
fsp.writeFile(path.join(featuresWorkerBase, 'index.ts'), workerFile),
fsp.writeFile( fsp.writeFile(
path.join(base, 'bridge', 'tsconfig.json'), path.join(featuresWorkerBridgeBase, 'meta.ts'),
JSON.stringify(bridgeTsConfig, null, ' '), bridgeMeta,
), ),
fsp.writeFile(path.join(base, 'index.ts'), workerFile),
fsp.writeFile(path.join(base, 'bridge', 'meta.ts'), bridgeMeta),
]); ]);
}, },
}; };

View File

@@ -75,6 +75,8 @@ export default async function ({ watch }) {
'src/client', 'src/client',
'src/shared', 'src/shared',
'src/features', 'src/features',
'src/features-worker',
'src/features-worker-worker-bridge',
'src/sw', 'src/sw',
'codecs', 'codecs',
]), ]),

View File

@@ -1,8 +1,8 @@
import { wrap } from 'comlink'; import { wrap } from 'comlink';
import { BridgeMethods, methodNames } from './meta'; import { BridgeMethods, methodNames } from './meta';
import workerURL from 'omt:../index'; import workerURL from 'omt:../features-worker';
import type { ProcessorWorkerApi } from '../'; import type { ProcessorWorkerApi } from '../../../features-worker';
import { abortable } from '../../../client/lazy-app/util'; import { abortable } from '../util';
/** How long the worker should be idle before terminating. */ /** How long the worker should be idle before terminating. */
const workerTimeout = 10000; const workerTimeout = 10000;

View File

@@ -4,5 +4,5 @@
"lib": ["esnext", "dom", "dom.iterable"], "lib": ["esnext", "dom", "dom.iterable"],
"types": [] "types": []
}, },
"references": [{ "path": "../features/worker" }, { "path": "../shared" }] "references": [{ "path": "../features-worker" }, { "path": "../shared" }]
} }

View File

@@ -10,4 +10,4 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/// <reference path="../../../missing-types.d.ts" /> /// <reference path="../../missing-types.d.ts" />

View File

@@ -1,13 +0,0 @@
/**
* Copyright 2020 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <reference path="../../../../missing-types.d.ts" />