Skip to content

Commit fdba194

Browse files
authored
fix: Matching state and invoke names work now (#230)
1 parent ca9e653 commit fdba194

File tree

10 files changed

+126
-35
lines changed

10 files changed

+126
-35
lines changed

.changeset/slow-planets-begin.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
"xstate-component-tree": major
3+
---
4+
5+
Matching state and invoke names would cause `xstate-component-tree` to fail to return child components in some cases because internally it named them the same thing and stomped all over itself.
6+
7+
```
8+
states : {
9+
foo : {
10+
invoke : {
11+
id : "foo",
12+
// ...
13+
},
14+
},
15+
},
16+
```
17+
18+
This should work now.
19+
20+
**BREAKING CHANGE**: All `machine` values in the output that previously looked like `root.foo` will now look like `root.#foo`, in order to help differentiate the child machine `id` from the parent state path.

.vscode/launch.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
78
{
89
"name": "uvu All",
910
"type": "node",
@@ -21,5 +22,22 @@
2122
"program": "${workspaceFolder}/node_modules/uvu/bin.js"
2223
}
2324
},
25+
{
26+
"name": "uvu Current",
27+
"type": "node",
28+
"request": "launch",
29+
"skipFiles": [
30+
"<node_internals>/**"
31+
],
32+
"program": "${workspaceFolder}/node_modules/.bin/uvu",
33+
"args": [
34+
"tests",
35+
"${fileBasename}"
36+
],
37+
"console": "integratedTerminal",
38+
"windows": {
39+
"program": "${workspaceFolder}/node_modules/uvu/bin.js"
40+
}
41+
},
2442
]
2543
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/component-tree.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class ComponentTree {
144144
_watch(path) {
145145
const { _paths, _actors, _invokables, _options, _log } = this;
146146

147-
_log(`[${path}][_prep] prepping`);
147+
_log(`[${path}][_watch] prepping`);
148148

149149
const { actor } = _actors.get(path);
150150

@@ -163,7 +163,7 @@ class ComponentTree {
163163
// xstate maps ids to state nodes, but the value object only
164164
// has paths, so need to create our own path-only map here
165165
for(const item of ids.values()) {
166-
const key = [ path, ...item.path ].join(".");
166+
const key = childPath(path, ...item.path);
167167

168168
if(item.meta) {
169169
_paths.set(key, Object.assign({
@@ -174,11 +174,11 @@ class ComponentTree {
174174
}
175175

176176
// .invoke is always an array
177-
_invokables.set(key, item.invoke.map(({ id : invoked }) => childPath(path, invoked)));
177+
_invokables.set(key, item.invoke.map(({ id : invoked }) => childPath(path, `#${invoked}`)));
178178
}
179179

180-
_log(`[${path}][_prep] _paths`, [ ..._paths.keys() ]);
181-
_log(`[${path}][_prep] _invokables`, [ ..._invokables.entries() ]);
180+
_log(`[${path}][_watch] _paths`, [ ..._paths.keys() ]);
181+
_log(`[${path}][_watch] _invokables`, [ ..._invokables.entries() ]);
182182

183183
_log(`[${path}][_watch] subscribing`);
184184

@@ -214,6 +214,8 @@ class ComponentTree {
214214
const current = _actors.get(path);
215215

216216
if(state === current.state && current.run > 0) {
217+
_log(`[${path}][_onState] State hasn't changed`);
218+
217219
return;
218220
}
219221

@@ -226,9 +228,11 @@ class ComponentTree {
226228

227229
// Add any new children to be tracked
228230
Object.keys(children).forEach((child) => {
229-
const id = [ path, child ].join(".");
231+
const id = childPath(path, `#${child}`);
230232

231233
if(_actors.has(id)) {
234+
_log(`[${path}][_onState] Already seen child ${id}`);
235+
232236
return;
233237
}
234238

tests/api/broadcast.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe("broadcast", (it) => {
8282
children: []
8383
},
8484
[Object: null prototype] {
85-
machine: "root.child",
85+
machine: "root.#child",
8686
Actual:
8787
-- path: "child.one",
8888
-- component: [Function: child-one],
@@ -93,7 +93,7 @@ describe("broadcast", (it) => {
9393
children: []
9494
},
9595
[Object: null prototype] {
96-
machine: "root.child.grandchild",
96+
machine: "root.#child.#grandchild",
9797
Actual:
9898
-- path: "grandchild.one",
9999
-- component: [Function: grandchild-one],
@@ -119,7 +119,7 @@ describe("broadcast", (it) => {
119119
children: []
120120
},
121121
[Object: null prototype] {
122-
machine: "root.child",
122+
machine: "root.#child",
123123
Actual:
124124
-- path: "child.two",
125125
-- component: [Function: child-two],
@@ -130,7 +130,7 @@ describe("broadcast", (it) => {
130130
children: []
131131
},
132132
[Object: null prototype] {
133-
machine: "root.child.grandchild",
133+
machine: "root.#child.#grandchild",
134134
Actual:
135135
-- path: "grandchild.two",
136136
-- component: [Function: grandchild-two],

tests/api/send.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe("send", (it) => {
6565
children: []
6666
},
6767
[Object: null prototype] {
68-
machine: "root.child",
68+
machine: "root.#child",
6969
path: "child.one",
7070
component: [Function: child-one],
7171
props: false,

tests/api/verbose.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe("verbose", (it) => {
2020
restoreAll();
2121

2222
// Only check first & last for now
23-
snapshot(log.calls[0], `[ "[root][_prep] prepping" ]`);
23+
snapshot(log.calls[0], `[ "[root][_watch] prepping" ]`);
2424
snapshot(log.calls[log.calls.length - 1], `[ "[root][_run #2] returning data" ]`);
2525
});
2626
});

tests/basic.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe("basic functionality", (it) => {
6666
children: []
6767
},
6868
[Object: null prototype] {
69-
machine: "root.child",
69+
machine: "root.#child",
7070
path: "child.one",
7171
component: [Function: child-one],
7272
props: false,

tests/invoked-load.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe(".load in invoked machines", (it) => {
4848
props: false,
4949
children: [
5050
[Object: null prototype] {
51-
machine: "test.child",
51+
machine: "test.#child",
5252
path: "child",
5353
component: [Function: child],
5454
props: false,
@@ -97,7 +97,7 @@ describe(".load in invoked machines", (it) => {
9797
props: false,
9898
children: [
9999
[Object: null prototype] {
100-
machine: "test.child",
100+
machine: "test.#child",
101101
path: "child",
102102
component: [Function: child],
103103
props: false,
@@ -146,7 +146,7 @@ describe(".load in invoked machines", (it) => {
146146
props: false,
147147
children: [
148148
[Object: null prototype] {
149-
machine: "test.child",
149+
machine: "test.#child",
150150
path: "child",
151151
component: [Function: child],
152152
props: {
@@ -201,7 +201,7 @@ describe(".load in invoked machines", (it) => {
201201
props: false,
202202
children: [
203203
[Object: null prototype] {
204-
machine: "test.child",
204+
machine: "test.#child",
205205
path: "child",
206206
component: {
207207
ctx: "child context",

0 commit comments

Comments
 (0)