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
# Auto-generated by lib/image-worker-plugin.js
src/features/worker/index.ts
src/features/worker/tsconfig.json
src/features/worker/bridge/meta.ts
src/features/worker/bridge/tsconfig.json
src/features-worker/index.ts
src/features-worker/tsconfig.json
src/client/lazy-app/worker-bridge/meta.ts

View File

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

View File

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

View File

@@ -4,5 +4,5 @@
"lib": ["esnext", "dom", "dom.iterable"],
"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
* 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" />