Skip to content

Commit

Permalink
Extract actor input
Browse files Browse the repository at this point in the history
  • Loading branch information
farskid committed Jan 22, 2024
1 parent 7300cfa commit e27d7de
Showing 4 changed files with 149 additions and 0 deletions.
130 changes: 130 additions & 0 deletions new-packages/ts-project/__tests__/actors.test.ts
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ test('should extract an actor with string src (direct)', async () => {
"parentId": "state-0",
"properties": {
"id": "inline:actor-id-0",
"input": {},
"src": "foo",
},
"sourceId": "foo",
@@ -111,6 +112,7 @@ test('should extract multiple actors with different string sources (direct)', as
"parentId": "state-1",
"properties": {
"id": "inline:actor-id-0",
"input": {},
"src": "actor1",
},
"sourceId": "actor1",
@@ -121,6 +123,7 @@ test('should extract multiple actors with different string sources (direct)', as
"parentId": "state-2",
"properties": {
"id": "inline:actor-id-1",
"input": {},
"src": "actor2",
},
"sourceId": "actor2",
@@ -244,6 +247,7 @@ test('should extract multiple actors with the same string source (direct)', asyn
"parentId": "state-1",
"properties": {
"id": "inline:actor-id-0",
"input": {},
"src": "actor1",
},
"sourceId": "actor1",
@@ -254,6 +258,7 @@ test('should extract multiple actors with the same string source (direct)', asyn
"parentId": "state-2",
"properties": {
"id": "inline:actor-id-1",
"input": {},
"src": "actor1",
},
"sourceId": "actor1",
@@ -372,6 +377,7 @@ test('should extract multiple actors with string source (array)', async () => {
"parentId": "state-1",
"properties": {
"id": "inline:actor-id-0",
"input": {},
"src": "actor1",
},
"sourceId": "actor1",
@@ -382,6 +388,7 @@ test('should extract multiple actors with string source (array)', async () => {
"parentId": "state-1",
"properties": {
"id": "inline:actor-id-1",
"input": {},
"src": "actor2",
},
"sourceId": "actor2",
@@ -488,6 +495,7 @@ test('should extract actor with inline source (direct)', async () => {
"parentId": "state-1",
"properties": {
"id": "inline:actor-id-0",
"input": {},
"src": "inline:actor-0",
},
"sourceId": "inline:actor-0",
@@ -498,6 +506,7 @@ test('should extract actor with inline source (direct)', async () => {
"parentId": "state-2",
"properties": {
"id": "inline:actor-id-1",
"input": {},
"src": "inline:actor-1",
},
"sourceId": "inline:actor-1",
@@ -695,6 +704,127 @@ test('should extract actor id if it is present with a string value', async () =>
"parentId": "state-1",
"properties": {
"id": "user-provided-id",
"input": {},
"src": "actor1",
},
"sourceId": "actor1",
"uniqueId": "block-0",
},
},
"data": {
"context": {},
},
"edges": {},
"implementations": {
"actions": {},
"actors": {
"actor1": {
"id": "actor1",
"name": "actor1",
"type": "actor",
},
},
"guards": {},
},
"nodes": {
"state-0": {
"data": {
"description": undefined,
"entry": [],
"exit": [],
"history": undefined,
"initial": "foo",
"invoke": [],
"metaEntries": [],
"tags": [],
"type": "normal",
},
"parentId": undefined,
"type": "node",
"uniqueId": "state-0",
},
"state-1": {
"data": {
"description": undefined,
"entry": [],
"exit": [],
"history": undefined,
"initial": undefined,
"invoke": [
"block-0",
],
"metaEntries": [],
"tags": [],
"type": "normal",
},
"parentId": "state-0",
"type": "node",
"uniqueId": "state-1",
},
},
"root": "state-0",
},
[],
],
]
`);
});
test('should extract actor input', async () => {
const tmpPath = await testdir({
'tsconfig.json': JSON.stringify({}),
'index.ts': ts`
import { createMachine } from "xstate";
createMachine({
initial: "foo",
states: {
foo: {
invoke: {
src: "actor1",
input: {
str: "some string",
num: 123,
bool: true,
arr: [1, 2, [3, 4]],
obj: { x: { y: 1 } },
},
},
},
},
});
`,
});

const project = await createTestProject(tmpPath);
expect(replaceUniqueIds(project.extractMachines('index.ts')))
.toMatchInlineSnapshot(`
[
[
{
"blocks": {
"block-0": {
"blockType": "actor",
"parentId": "state-1",
"properties": {
"id": "inline:actor-id-0",
"input": {
"arr": [
1,
2,
[
3,
4,
],
],
"bool": true,
"num": 123,
"obj": {
"x": {
"y": 1,
},
},
"str": "some string",
},
"src": "actor1",
},
"sourceId": "actor1",
5 changes: 5 additions & 0 deletions new-packages/ts-project/__tests__/transitions.test.ts
Original file line number Diff line number Diff line change
@@ -2283,6 +2283,7 @@ test('should extract invoke.onDone transition (with invoke.id)', async () => {
"parentId": "state-1",
"properties": {
"id": "urgentCall",
"input": {},
"src": "callDavid",
},
"sourceId": "callDavid",
@@ -2415,6 +2416,7 @@ test('should extract invoke.onDone transition (without invoke.id)', async () =>
"parentId": "state-1",
"properties": {
"id": "inline:actor-id-0",
"input": {},
"src": "callDavid",
},
"sourceId": "callDavid",
@@ -2560,6 +2562,7 @@ test('should extract invoke.onDone action', async () => {
"parentId": "state-1",
"properties": {
"id": "inline:actor-id-0",
"input": {},
"src": "callDavid",
},
"sourceId": "callDavid",
@@ -2701,6 +2704,7 @@ test('should extract invoke.onError transition (with invoke.id)', async () => {
"parentId": "state-1",
"properties": {
"id": "urgentCall",
"input": {},
"src": "callDavid",
},
"sourceId": "callDavid",
@@ -2833,6 +2837,7 @@ test('should extract invoke.onError transition (without invoke.id)', async () =>
"parentId": "state-1",
"properties": {
"id": "inline:actor-id-0",
"input": {},
"src": "callDavid",
},
"sourceId": "callDavid",
13 changes: 13 additions & 0 deletions new-packages/ts-project/src/state.ts
Original file line number Diff line number Diff line change
@@ -51,10 +51,12 @@ export function createActorBlock({
sourceId,
parentId,
actorId,
input,
}: {
sourceId: string;
parentId: string;
actorId: string;
input?: JsonObject | undefined;
}): ActorBlock {
const blockId = uniqueId();
return {
@@ -65,6 +67,7 @@ export function createActorBlock({
properties: {
src: sourceId,
id: actorId,
input: input ?? {},
},
};
}
@@ -673,6 +676,7 @@ export function extractState(
let actorId: string | undefined;
let onDone: PropertyAssignment | undefined;
let onError: PropertyAssignment | undefined;
let input: PropertyAssignment | undefined;

forEachStaticProperty(ctx, ts, element, (prop, key) => {
switch (key) {
@@ -691,6 +695,10 @@ export function extractState(
onError = prop;
return;
}
case 'input': {
input = prop;
return;
}
}
});

@@ -700,6 +708,11 @@ export function extractState(
: `inline:${uniqueId()}`,
parentId: node.uniqueId,
actorId: actorId ?? `inline:${uniqueId()}`,
input:
// TODO: Handle invalid input values
input && ts.isObjectLiteralExpression(input.initializer)
? getJsonObject(ctx, ts, input.initializer)
: {},
});

if (onDone) {
1 change: 1 addition & 0 deletions new-packages/ts-project/src/types.ts
Original file line number Diff line number Diff line change
@@ -76,6 +76,7 @@ export interface ActorBlock extends BlockBase {
properties: {
src: string;
id: string;
input: JsonObject;
};
}

0 comments on commit e27d7de

Please sign in to comment.