Skip to content

Commit 52b2dbc

Browse files
Fix the issue of missing attached fx name
1 parent 6b382ba commit 52b2dbc

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

src/index.ts

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ function getInstanceName(name?: string): string {
122122
}
123123

124124
// reporting
125-
const fxIdMap = new Map<unknown, string>();
126-
127125
function createReporter(state: ReturnType<typeof createState>) {
128126
return (m: Message): Record<string, unknown> | void => {
129127
// errors
@@ -139,9 +137,7 @@ function createReporter(state: ReturnType<typeof createState>) {
139137

140138
// effects
141139
if (isEffectCall(m)) {
142-
const name = getName(m);
143-
const callId = getEffectCallId(m);
144-
fxIdMap.set(callId, name);
140+
saveEffectCall(m);
145141
return {
146142
type: `☄️ [effect] ${m.name || "unknown"}`,
147143
params: m.value,
@@ -153,9 +149,7 @@ function createReporter(state: ReturnType<typeof createState>) {
153149
}
154150

155151
if (isEffectFinally(m)) {
156-
const callId = getEffectCallId(m);
157-
const name = fxIdMap.get(callId)!;
158-
fxIdMap.delete(callId);
152+
const name = getParentEffectName(m);
159153

160154
if ((m.value as any).status === "done") {
161155
return {
@@ -255,6 +249,35 @@ function isForward(m: Message) {
255249
return m.kind === "forward";
256250
}
257251

252+
// effects tracking
253+
const fxIdMap = new Map<unknown, string[]>();
254+
255+
function getCallsById(id: unknown) {
256+
if (!fxIdMap.has(id)) {
257+
fxIdMap.set(id, []);
258+
}
259+
return fxIdMap.get(id)!;
260+
}
261+
262+
function saveEffectCall(m: Message) {
263+
const name = getName(m);
264+
const callId = getEffectCallId(m);
265+
const calls = getCallsById(callId);
266+
calls.push(name);
267+
}
268+
269+
function getParentEffectName(m: Message) {
270+
const callId = getEffectCallId(m);
271+
const calls = getCallsById(callId);
272+
const name = calls.pop();
273+
274+
if (calls.length === 0) {
275+
fxIdMap.delete(callId);
276+
}
277+
278+
return name;
279+
}
280+
258281
// util
259282
function isEffectorInternal(m: Message) {
260283
return !!m.meta.named;

0 commit comments

Comments
 (0)