Skip to content

Commit fdfb135

Browse files
authored
feat: pass value through activate to worker threads (#2842)
1 parent 23bdc19 commit fdfb135

9 files changed

Lines changed: 58 additions & 11 deletions

File tree

packages/runtime-node/src/micro-rpc.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ export function rpcPost(worker: Worker, type: string, value?: unknown) {
3535
worker.postMessage(outgoingMessage);
3636
}
3737

38-
export function bindRpcListener<T>(type: string, customFetcher: () => Promise<T> | T) {
38+
export function bindRpcListener<T>(type: string, customFetcher: (value: unknown) => Promise<T> | T) {
3939
const handler = async (message: unknown) => {
4040
if (isValidRpcMessage(message) && message.type === type) {
4141
const outgoingMessage = {
4242
id: message.id,
43-
value: await customFetcher(),
43+
value: await customFetcher(message.value),
4444
};
4545
if (parentPort) {
4646
parentPort.postMessage(outgoingMessage);
@@ -60,7 +60,7 @@ export function bindRpcListener<T>(type: string, customFetcher: () => Promise<T>
6060
};
6161
}
6262

63-
export function isValidRpcMessage(message: unknown): message is { type: string; id: string } {
63+
export function isValidRpcMessage(message: unknown): message is { type: string; id: string; value?: unknown } {
6464
return !!(
6565
message &&
6666
typeof message === 'object' &&

packages/runtime-node/src/node-env-manager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface RunningNodeEnvironment {
1919
id: string;
2020
dispose(): Promise<void>;
2121
getMetrics(): Promise<PerformanceMetrics>;
22-
activate?(): Promise<void>;
22+
activate?(value?: unknown): Promise<void>;
2323
}
2424

2525
export interface NodeEnvConfig extends Pick<AnyEnvironment, 'env' | 'endpointType'> {
@@ -126,11 +126,11 @@ export class NodeEnvManager implements IDisposable {
126126
return { port };
127127
}
128128

129-
async activateEnvs() {
129+
async activateEnvs(value?: unknown) {
130130
const activatedEnvs: Promise<void>[] = [];
131131
for (const env of this.openEnvironments.values()) {
132132
if (!env.activate) continue;
133-
activatedEnvs.push(env.activate());
133+
activatedEnvs.push(env.activate(value));
134134
}
135135
await Promise.all(activatedEnvs);
136136
}

packages/runtime-node/src/worker-thread-initializer2.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { RunningNodeEnvironment } from './node-env-manager.js';
99
export interface WorkerThreadInitializer2 extends RunningNodeEnvironment {
1010
initialize: () => Promise<void>;
1111
preLoad: () => void;
12-
activate: () => Promise<void>;
12+
activate: (value?: unknown) => Promise<void>;
1313
}
1414

1515
export interface WorkerThreadInitializerOptions2 {
@@ -78,8 +78,8 @@ export function workerThreadInitializer2({
7878
});
7979
};
8080

81-
const activate = async () => {
82-
rpcPost(worker, 'activate');
81+
const activate = async (value?: unknown) => {
82+
rpcPost(worker, 'activate', value);
8383
await envIsReady;
8484
};
8585

packages/runtime-node/test-kit/entrypoints/a.node.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ if (verbose) {
1313
console.log(`[${env.env}: Started with options: `, options);
1414
}
1515

16+
let activateValue: unknown;
17+
export function getActivateValue() {
18+
return activateValue;
19+
}
20+
1621
export function runEnv({
1722
Feature = TestFeature,
1823
topLevelConfig = [],
@@ -39,7 +44,8 @@ export function runEnv({
3944
if (workerData) {
4045
const unbindMetricsListener = bindMetricsListener();
4146
let running: ReturnType<typeof runEnv>;
42-
const unbindActivateListener = bindRpcListener('activate', () => {
47+
const unbindActivateListener = bindRpcListener('activate', (value: unknown) => {
48+
activateValue = value;
4349
unbindActivateListener();
4450
running = runEnv();
4551
});

packages/runtime-node/test-kit/entrypoints/b.node.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ if (verbose) {
1313
console.log(`[${env.env}: Started with options: `, options);
1414
}
1515

16+
let activateValue: unknown;
17+
export function getActivateValue() {
18+
return activateValue;
19+
}
20+
1621
export function runEnv({
1722
Feature = TestFeature,
1823
topLevelConfig = [],
@@ -39,7 +44,8 @@ export function runEnv({
3944
if (workerData) {
4045
const unbindMetricsListener = bindMetricsListener();
4146
let running: ReturnType<typeof runEnv>;
42-
const unbindActivateListener = bindRpcListener('activate', () => {
47+
const unbindActivateListener = bindRpcListener('activate', (value: unknown) => {
48+
activateValue = value;
4349
unbindActivateListener();
4450
running = runEnv();
4551
});

packages/runtime-node/test-kit/feature/test-feature.a.env.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { aEnv } from './envs.js';
22
import TestFeature from './test-feature.js';
3+
import { getActivateValue } from '../entrypoints/a.node.js';
34

45
TestFeature.setup(aEnv, ({ echoBService }) => {
56
return {
@@ -8,6 +9,7 @@ TestFeature.setup(aEnv, ({ echoBService }) => {
89
echoChained: async () => {
910
return echoBService.echo();
1011
},
12+
getActivateValue,
1113
},
1214
};
1315
});

packages/runtime-node/test-kit/feature/test-feature.b.env.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { bEnv } from './envs.js';
22
import TestFeature from './test-feature.js';
3+
import { getActivateValue } from '../entrypoints/b.node.js';
34

45
TestFeature.setup(bEnv, ({ echoAService }) => {
56
return {
@@ -8,6 +9,7 @@ TestFeature.setup(bEnv, ({ echoAService }) => {
89
echoChained: async () => {
910
return echoAService.echo();
1011
},
12+
getActivateValue,
1113
},
1214
};
1315
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type EchoService = {
22
echo: () => string;
33
echoChained: () => Promise<string>;
4+
getActivateValue: () => unknown;
45
};

packages/runtime-node/test/node-env.manager.unit.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,36 @@ describe('NodeEnvManager', () => {
193193

194194
expect(await api.echo()).to.equal('a');
195195
});
196+
197+
it('should pass activate value to the worker', async () => {
198+
const featureEnvironmentsMapping: NodeEnvsFeatureMapping = {
199+
featureToEnvironments: {
200+
'test-feature': [aEnv.env, bEnv.env],
201+
},
202+
availableEnvironments: {
203+
a: {
204+
env: aEnv.env,
205+
endpointType: 'single',
206+
envType: 'node',
207+
},
208+
b: {
209+
env: bEnv.env,
210+
endpointType: 'single',
211+
envType: 'node',
212+
},
213+
},
214+
};
215+
216+
const manager = disposeAfterTest(new NodeEnvManager(meta, featureEnvironmentsMapping));
217+
const { port } = await manager.autoLaunch(new Map([['feature', 'test-feature']]), {}, true);
218+
const communication = disposeAfterTest(getClientCom(port));
219+
const api = communication.apiProxy<EchoService>({ id: aEnv.env }, { id: 'test-feature.echoAService' });
220+
221+
const activateData = { hello: 'world' };
222+
await manager.activateEnvs(activateData);
223+
224+
expect(await api.getActivateValue()).to.deep.equal(activateData);
225+
});
196226
});
197227

198228
describe('NodeEnvManager with connection handlers', () => {

0 commit comments

Comments
 (0)