Skip to content

Commit 4ed40bc

Browse files
authored
Add getRunningAsyncEffects API on root circuit (#2087)
1 parent d256212 commit 4ed40bc

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

lib/IsolatedCircuit.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ export class IsolatedCircuit {
7777
_hasRenderedAtleastOnce = false
7878
private _asyncEffectIdsByPhase = new Map<RenderPhase, Set<string>>()
7979
private _asyncEffectPhaseById = new Map<string, RenderPhase>()
80+
private _runningAsyncEffectsById = new Map<
81+
string,
82+
{
83+
asyncEffectId: string
84+
effectName?: string
85+
componentDisplayName?: string
86+
phase?: RenderPhase
87+
error?: string
88+
}
89+
>()
8090

8191
constructor({
8292
platform,
@@ -212,6 +222,16 @@ export class IsolatedCircuit {
212222
return (this._asyncEffectIdsByPhase.get(phase)?.size ?? 0) > 0
213223
}
214224

225+
getRunningAsyncEffects(): Array<{
226+
asyncEffectId: string
227+
effectName?: string
228+
componentDisplayName?: string
229+
phase?: RenderPhase
230+
error?: string
231+
}> {
232+
return Array.from(this._runningAsyncEffectsById.values())
233+
}
234+
215235
getCircuitJson(): AnyCircuitElement[] {
216236
if (!this._hasRenderedAtleastOnce) this.render()
217237
return this.db.toArray()
@@ -338,7 +358,10 @@ export class IsolatedCircuit {
338358

339359
private _registerAsyncEffectStart(payload: {
340360
asyncEffectId?: string
361+
effectName?: string
362+
componentDisplayName?: string
341363
phase?: RenderPhase
364+
error?: string
342365
}) {
343366
if (!payload?.asyncEffectId || !payload.phase) return
344367
const { asyncEffectId, phase } = payload
@@ -351,6 +374,13 @@ export class IsolatedCircuit {
351374
}
352375
this._asyncEffectIdsByPhase.get(phase)!.add(asyncEffectId)
353376
this._asyncEffectPhaseById.set(asyncEffectId, phase)
377+
this._runningAsyncEffectsById.set(asyncEffectId, {
378+
asyncEffectId,
379+
effectName: payload.effectName,
380+
componentDisplayName: payload.componentDisplayName,
381+
phase,
382+
error: payload.error,
383+
})
354384
}
355385

356386
private _registerAsyncEffectEnd(payload: {
@@ -368,5 +398,6 @@ export class IsolatedCircuit {
368398
}
369399
}
370400
this._asyncEffectPhaseById.delete(asyncEffectId)
401+
this._runningAsyncEffectsById.delete(asyncEffectId)
371402
}
372403
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { test, expect } from "bun:test"
2+
import { RootCircuit } from "lib/RootCircuit"
3+
4+
test("getRunningAsyncEffects returns currently running async effects", () => {
5+
const circuit = new RootCircuit()
6+
7+
circuit.emit("asyncEffect:start", {
8+
asyncEffectId: "render:1",
9+
effectName: "first-effect",
10+
componentDisplayName: "<board#1 />",
11+
phase: "PcbTraceRender",
12+
})
13+
14+
circuit.emit("asyncEffect:start", {
15+
asyncEffectId: "render:2",
16+
effectName: "second-effect",
17+
componentDisplayName: "<resistor#1 />",
18+
phase: "PcbTraceRender",
19+
})
20+
21+
expect(circuit.getRunningAsyncEffects()).toEqual([
22+
{
23+
asyncEffectId: "render:1",
24+
effectName: "first-effect",
25+
componentDisplayName: "<board#1 />",
26+
phase: "PcbTraceRender",
27+
error: undefined,
28+
},
29+
{
30+
asyncEffectId: "render:2",
31+
effectName: "second-effect",
32+
componentDisplayName: "<resistor#1 />",
33+
phase: "PcbTraceRender",
34+
error: undefined,
35+
},
36+
])
37+
38+
circuit.emit("asyncEffect:end", { asyncEffectId: "render:1" })
39+
40+
expect(circuit.getRunningAsyncEffects()).toEqual([
41+
{
42+
asyncEffectId: "render:2",
43+
effectName: "second-effect",
44+
componentDisplayName: "<resistor#1 />",
45+
phase: "PcbTraceRender",
46+
error: undefined,
47+
},
48+
])
49+
50+
circuit.emit("asyncEffect:end", { asyncEffectId: "render:2" })
51+
52+
expect(circuit.getRunningAsyncEffects()).toEqual([])
53+
})

0 commit comments

Comments
 (0)