Skip to content

Commit

Permalink
Fix the issue of missing attached fx name
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrHoroshih committed Jul 27, 2023
1 parent 6b382ba commit 52b2dbc
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,6 @@ function getInstanceName(name?: string): string {
}

// reporting
const fxIdMap = new Map<unknown, string>();

function createReporter(state: ReturnType<typeof createState>) {
return (m: Message): Record<string, unknown> | void => {
// errors
Expand All @@ -139,9 +137,7 @@ function createReporter(state: ReturnType<typeof createState>) {

// effects
if (isEffectCall(m)) {
const name = getName(m);
const callId = getEffectCallId(m);
fxIdMap.set(callId, name);
saveEffectCall(m);
return {
type: `☄️ [effect] ${m.name || "unknown"}`,
params: m.value,
Expand All @@ -153,9 +149,7 @@ function createReporter(state: ReturnType<typeof createState>) {
}

if (isEffectFinally(m)) {
const callId = getEffectCallId(m);
const name = fxIdMap.get(callId)!;
fxIdMap.delete(callId);
const name = getParentEffectName(m);

if ((m.value as any).status === "done") {
return {
Expand Down Expand Up @@ -255,6 +249,35 @@ function isForward(m: Message) {
return m.kind === "forward";
}

// effects tracking
const fxIdMap = new Map<unknown, string[]>();

function getCallsById(id: unknown) {
if (!fxIdMap.has(id)) {
fxIdMap.set(id, []);
}
return fxIdMap.get(id)!;
}

function saveEffectCall(m: Message) {
const name = getName(m);
const callId = getEffectCallId(m);
const calls = getCallsById(callId);
calls.push(name);
}

function getParentEffectName(m: Message) {
const callId = getEffectCallId(m);
const calls = getCallsById(callId);
const name = calls.pop();

if (calls.length === 0) {
fxIdMap.delete(callId);
}

return name;
}

// util
function isEffectorInternal(m: Message) {
return !!m.meta.named;
Expand Down

0 comments on commit 52b2dbc

Please sign in to comment.