Skip to content

Commit

Permalink
Remove functions worker (#3013)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot authored Dec 20, 2023
1 parent 04a1e57 commit d4e88b3
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 315 deletions.
57 changes: 22 additions & 35 deletions packages/toolpad-app/src/server/FunctionsManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import {
import { errorFrom } from '@mui/toolpad-utils/errors';
import { ToolpadDataProviderIntrospection } from '@mui/toolpad-core/runtime';
import * as url from 'node:url';
import invariant from 'invariant';
import type { GridRowId } from '@mui/x-data-grid';
import invariant from 'invariant';
import EnvManager from './EnvManager';
import { ProjectEvents, ToolpadProjectOptions } from '../types';
import { createWorker as createDevWorker } from './functionsDevWorker';
import * as functionsRuntime from './functionsRuntime';
import type { ExtractTypesParams, IntrospectionResult } from './functionsTypesWorker';
import { Awaitable } from '../utils/types';
import { format } from '../utils/prettier';
Expand Down Expand Up @@ -114,8 +114,6 @@ export default class FunctionsManager {

private buildErrors: esbuild.Message[] = [];

private devWorker: ReturnType<typeof createDevWorker> | undefined;

private extractedTypes: Awaitable<IntrospectionResult> | undefined;

private extractTypesWorker: Piscina | undefined;
Expand Down Expand Up @@ -252,24 +250,10 @@ export default class FunctionsManager {
resourcesWatcher.on('unlink', reinitializeWatcher);
}

private async createRuntimeWorker() {
const oldWorker = this.devWorker;
this.devWorker = createDevWorker(this.project.envManager.getEnv());
await oldWorker?.terminate();
this.project.invalidateQueries();
}

async start() {
await this.createRuntimeWorker();

if (this.project.options.dev) {
await this.migrateLegacy();

await this.startWatchingFunctionFiles();

this.project.events.subscribe('envChanged', async () => {
await this.createRuntimeWorker();
});
}
}

Expand All @@ -293,11 +277,7 @@ export default class FunctionsManager {
}

async dispose() {
await Promise.all([
this.disposeBuildcontext(),
this.devWorker?.terminate(),
this.extractTypesWorker?.destroy(),
]);
await Promise.all([this.disposeBuildcontext(), this.extractTypesWorker?.destroy()]);
}

async getBuiltOutputFilePath(fileName: string): Promise<string> {
Expand Down Expand Up @@ -351,8 +331,7 @@ export default class FunctionsManager {
): Promise<ExecFetchResult<unknown>> {
const outputFilePath = await this.getBuiltOutputFilePath(fileName);

invariant(this.devWorker, 'devWorker must be initialized');
const data = await this.devWorker.execute(outputFilePath, name, parameters);
const data = await functionsRuntime.execute(outputFilePath, name, parameters);

return { data };
}
Expand Down Expand Up @@ -396,8 +375,13 @@ export default class FunctionsManager {
exportName: string = 'default',
): Promise<ToolpadDataProviderIntrospection> {
const fullPath = await this.getBuiltOutputFilePath(fileName);
invariant(this.devWorker, 'devWorker must be initialized');
return this.devWorker.introspectDataProvider(fullPath, exportName);
const dataProvider = await functionsRuntime.loadDataProvider(fullPath, exportName);
return {
paginationMode: dataProvider.paginationMode,
hasDeleteRecord: !!dataProvider.deleteRecord,
hasUpdateRecord: !!dataProvider.updateRecord,
hasCreateRecord: !!dataProvider.createRecord,
};
}

async getDataProviderRecords<R, P extends PaginationMode>(
Expand All @@ -406,8 +390,8 @@ export default class FunctionsManager {
params: GetRecordsParams<R, P>,
): Promise<GetRecordsResult<R, P>> {
const fullPath = await this.getBuiltOutputFilePath(fileName);
invariant(this.devWorker, 'devWorker must be initialized');
return this.devWorker.getDataProviderRecords(fullPath, exportName, params);
const dataProvider = await functionsRuntime.loadDataProvider(fullPath, exportName);
return dataProvider.getRecords(params);
}

async deleteDataProviderRecord(
Expand All @@ -416,8 +400,9 @@ export default class FunctionsManager {
id: GridRowId,
): Promise<void> {
const fullPath = await this.getBuiltOutputFilePath(fileName);
invariant(this.devWorker, 'devWorker must be initialized');
return this.devWorker.deleteDataProviderRecord(fullPath, exportName, id);
const dataProvider = await functionsRuntime.loadDataProvider(fullPath, exportName);
invariant(dataProvider.deleteRecord, 'DataProvider does not support deleteRecord');
return dataProvider.deleteRecord(id);
}

async updateDataProviderRecord(
Expand All @@ -427,8 +412,9 @@ export default class FunctionsManager {
values: Record<string, unknown>,
): Promise<void> {
const fullPath = await this.getBuiltOutputFilePath(fileName);
invariant(this.devWorker, 'devWorker must be initialized');
return this.devWorker.updateDataProviderRecord(fullPath, exportName, id, values);
const dataProvider = await functionsRuntime.loadDataProvider(fullPath, exportName);
invariant(dataProvider.updateRecord, 'DataProvider does not support updateRecord');
return dataProvider.updateRecord(id, values);
}

async createDataProviderRecord(
Expand All @@ -437,7 +423,8 @@ export default class FunctionsManager {
values: Record<string, unknown>,
): Promise<void> {
const fullPath = await this.getBuiltOutputFilePath(fileName);
invariant(this.devWorker, 'devWorker must be initialized');
return this.devWorker.createDataProviderRecord(fullPath, exportName, values);
const dataProvider = await functionsRuntime.loadDataProvider(fullPath, exportName);
invariant(dataProvider.createRecord, 'DataProvider does not support createRecord');
return dataProvider.createRecord(values);
}
}
Loading

0 comments on commit d4e88b3

Please sign in to comment.