Skip to content

Commit ef3aaea

Browse files
author
alex-abanin
committed
global reset event
1 parent 4bb1244 commit ef3aaea

8 files changed

Lines changed: 54 additions & 16 deletions

File tree

apps/chaingraph-frontend/src/providers/RootProvider.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ export function RootProvider({
9191
}
9292

9393
return () => {
94-
reset()
94+
if (isInitializedRef.current)
95+
reset()
9596
}
9697
}, [nodeRegistry, sessionToken, superjsonCustom, trpcURL])
9798

apps/chaingraph-frontend/src/store/categories/stores.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { CategorizedNodes, CategoryMetadata } from '@badaitech/chaingraph-t
1010
import type { FetchCategoriesError } from './types'
1111
import { NODE_CATEGORIES } from '@badaitech/chaingraph-nodes'
1212
import { combine } from 'effector'
13+
import { globalReset } from '../common'
1314
import { categoriesDomain } from '../domains'
1415
import { $trpcClient } from '../trpc/store'
1516

@@ -29,6 +30,7 @@ export const fetchCategorizedNodesFx = categoriesDomain.createEffect(() => {
2930
const $categorizedNodes = categoriesDomain.createStore<CategorizedNodes[]>([])
3031
.on(fetchCategorizedNodesFx.doneData, (_, nodes) => nodes)
3132
.reset(resetCategories)
33+
.reset(globalReset)
3234

3335
export const $categoryMetadata = categoriesDomain.createStore<Map<string, CategoryMetadata>>(new Map())
3436
.on(fetchCategorizedNodesFx.doneData, (_, nodes) => {
@@ -39,6 +41,7 @@ export const $categoryMetadata = categoriesDomain.createStore<Map<string, Catego
3941
return map
4042
})
4143
.reset(resetCategories)
44+
.reset(globalReset)
4245

4346
const $isLoading = fetchCategorizedNodesFx.pending
4447

@@ -48,6 +51,7 @@ const $error = categoriesDomain.createStore<FetchCategoriesError | null>(null)
4851
timestamp: new Date(),
4952
}))
5053
.reset(fetchCategorizedNodesFx.doneData)
54+
.reset(globalReset)
5155

5256
// Computed stores
5357
export const $categoriesState = combine({
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (c) 2025 BadLabs
3+
*
4+
* Use of this software is governed by the Business Source License 1.1 included in the file LICENSE.txt.
5+
*
6+
* As of the Change Date specified in that file, in accordance with the Business Source License, use of this software will be governed by the Apache License, version 2.0.
7+
*/
8+
9+
import { createEvent } from 'effector'
10+
11+
export const globalReset = createEvent()

apps/chaingraph-frontend/src/store/edges/stores.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { Edge } from '@xyflow/react'
1111
import type { AddEdgeEventData, EdgeData, RemoveEdgeEventData } from './types'
1212
import { edgesDomain } from '@/store/domains'
1313
import { attach, combine, sample } from 'effector'
14+
import { globalReset } from '../common'
1415
import { $executionNodes, $executionState, $highlightedEdgeId, $highlightedNodeId } from '../execution'
1516
import { $xyflowNodes } from '../nodes'
1617
import { $trpcClient } from '../trpc/store'
@@ -71,6 +72,7 @@ export const $edges = edgesDomain.createStore<EdgeData[]>([])
7172
edge => edge.edgeId !== event.edgeId,
7273
))
7374
.reset(resetEdges)
75+
.reset(globalReset)
7476
// .reset(clearActiveFlow)
7577

7678
/**

apps/chaingraph-frontend/src/store/execution/stores.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
} from './types'
1818
import { ExecutionEventEnum } from '@badaitech/chaingraph-types'
1919
import { attach, combine, sample } from 'effector'
20+
import { globalReset } from '../common'
2021
import { executionDomain } from '../domains'
2122
import { $trpcClient } from '../trpc/store'
2223
import { ExecutionStatus, ExecutionSubscriptionStatus, isTerminalStatus } from './types'
@@ -67,7 +68,7 @@ const initialState: ExecutionState = {
6768
},
6869
}
6970

70-
export const $executionState = executionDomain.createStore<ExecutionState>(initialState)
71+
export const $executionState = executionDomain.createStore<ExecutionState>(initialState).reset(globalReset)
7172

7273
// Control effects
7374
export const createExecutionFx = executionDomain.createEffect(async (payload: CreateExecutionOptions) => {
@@ -185,6 +186,7 @@ export const $executionEvents = executionDomain.createStore<ExecutionEventImpl[]
185186
.reset(clearExecutionState)
186187
.reset(stopExecutionFx.done)
187188
.reset(createExecutionFx.doneData)
189+
.reset(globalReset)
188190

189191
export const $executionNodes = executionDomain.createStore<Record<string, NodeExecutionState>>({}, {
190192
updateFilter: (prev, next) => {
@@ -307,6 +309,7 @@ export const $executionNodes = executionDomain.createStore<Record<string, NodeEx
307309
.reset(clearExecutionState)
308310
.reset(stopExecutionFx.done)
309311
.reset(createExecutionFx.doneData)
312+
.reset(globalReset)
310313

311314
export const $executionEdges = executionDomain.createStore<Record<string, EdgeExecutionState>>({})
312315
.on(newExecutionEvent, (state, event) => {
@@ -365,6 +368,7 @@ export const $executionEdges = executionDomain.createStore<Record<string, EdgeEx
365368
.reset(clearExecutionState)
366369
.reset(stopExecutionFx.done)
367370
.reset(createExecutionFx.doneData)
371+
.reset(globalReset)
368372

369373
// Handle execution status changes
370374
$executionState
@@ -521,12 +525,14 @@ export const $highlightedNodeId = executionDomain.createStore<string[] | null>(n
521525
typeof highlightedNodeId === 'string'
522526
? [highlightedNodeId]
523527
: highlightedNodeId)
528+
.reset(globalReset)
524529

525530
export const $highlightedEdgeId = executionDomain.createStore<string[] | null>(null)
526531
.on(setHighlightedEdgeId, (state, highlightedEdgeId) =>
527532
typeof highlightedEdgeId === 'string'
528533
? [highlightedEdgeId]
529534
: highlightedEdgeId)
535+
.reset(globalReset)
530536

531537
// Computed stores
532538
export const $isExecuting = $executionState.map(
@@ -578,7 +584,7 @@ export const $autoStartConditions = combine({
578584
})
579585

580586
// Store to prevent multiple start attempts
581-
export const $startAttempted = executionDomain.createStore(false)
587+
export const $startAttempted = executionDomain.createStore(false).reset(globalReset)
582588

583589
// export const $highlightedNodeId = $executionState.map(state => state.ui.highlightedNodeId)
584590
// export const $highlightedEdgeId = $executionState.map(state => state.ui.highlightedEdgeId)

apps/chaingraph-frontend/src/store/flow/stores.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { FlowMetadata } from '@badaitech/chaingraph-types'
1010
import type { CreateFlowEvent, FlowSubscriptionError, UpdateFlowEvent } from './types'
1111
import { flowDomain } from '@/store/domains'
1212
import { combine, sample } from 'effector'
13+
import { globalReset } from '../common'
1314
import { $trpcClient } from '../trpc/store'
1415
import { FlowSubscriptionStatus } from './types'
1516

@@ -118,45 +119,55 @@ export const $flows = flowDomain.createStore<FlowMetadata[]>([])
118119
})
119120
.on(deleteFlow, (flows, id) =>
120121
flows.filter(f => f.id !== id))
122+
.reset(globalReset)
121123

122124
// Currently active flow ID
123125
export const $activeFlowId = flowDomain.createStore<string | null>(null)
124126
.on(setActiveFlowId, (_, id) => id)
125127
.reset(clearActiveFlow)
128+
.reset(globalReset)
126129

127130
// Main loading state
128131
export const $isFlowsLoading = flowDomain.createStore<boolean>(false)
129132
.on(setFlowsLoading, (_, isLoading) => isLoading)
130133
.on(loadFlowsListFx.pending, (_, isPending) => isPending)
134+
.reset(globalReset)
131135

132136
// Main error state
133137
export const $flowsError = flowDomain.createStore<Error | null>(null)
134138
.on(setFlowsError, (_, error) => error)
135139
.on(loadFlowsListFx.failData, (_, error) => error)
136140
.reset(loadFlowsListFx.done)
141+
.reset(globalReset)
137142

138143
// Specific operation error stores
139144
export const $createFlowError = flowDomain.createStore<Error | null>(null)
140145
.on(createFlowFx.failData, (_, error) => error)
141146
.reset(createFlowFx.done)
147+
.reset(globalReset)
142148

143149
export const $updateFlowError = flowDomain.createStore<Error | null>(null)
144150
.on(editFlowFx.failData, (_, error) => error)
145151
.reset(editFlowFx.done)
152+
.reset(globalReset)
146153

147154
export const $deleteFlowError = flowDomain.createStore<Error | null>(null)
148155
.on(deleteFlowFx.failData, (_, error) => error)
149156
.reset(deleteFlowFx.done)
157+
.reset(globalReset)
150158

151159
// Specific operation loading states
152160
export const $isCreatingFlow = flowDomain.createStore<boolean>(false)
153161
.on(createFlowFx.pending, (_, isPending) => isPending)
162+
.reset(globalReset)
154163

155164
export const $isUpdatingFlow = flowDomain.createStore<boolean>(false)
156165
.on(editFlowFx.pending, (_, isPending) => isPending)
166+
.reset(globalReset)
157167

158168
export const $isDeletingFlow = flowDomain.createStore<boolean>(false)
159169
.on(deleteFlowFx.pending, (_, isPending) => isPending)
170+
.reset(globalReset)
160171

161172
// Combined error store
162173
export const $allFlowsErrors = combine(
@@ -180,9 +191,13 @@ export const $activeFlowMetadata = combine(
180191
// Subscription related stores
181192
export const $flowSubscriptionStatus = flowDomain.createStore<FlowSubscriptionStatus>(
182193
FlowSubscriptionStatus.IDLE,
183-
).on(setFlowSubscriptionStatus, (_, status) => status).reset(resetFlowSubscription).reset(clearActiveFlow)
194+
).on(setFlowSubscriptionStatus, (_, status) => status).reset(resetFlowSubscription).reset(clearActiveFlow).reset(globalReset)
184195

185-
export const $flowSubscriptionError = flowDomain.createStore<FlowSubscriptionError | null>(null).on(setFlowSubscriptionError, (_, error) => error).reset(resetFlowSubscription).reset(clearActiveFlow)
196+
export const $flowSubscriptionError = flowDomain.createStore<FlowSubscriptionError | null>(null)
197+
.on(setFlowSubscriptionError, (_, error) => error)
198+
.reset(resetFlowSubscription)
199+
.reset(clearActiveFlow)
200+
.reset(globalReset)
186201

187202
// Derived store to check if subscription is active
188203
export const $isFlowSubscribed = $flowSubscriptionStatus.map(

apps/chaingraph-frontend/src/store/init.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
*/
88

99
import type { CategorizedNodes, FlowMetadata } from '@badaitech/chaingraph-types'
10-
import { fetchCategorizedNodesFx, resetCategories } from './categories'
11-
import { resetEdges } from './edges'
12-
import { clearExecutionState } from './execution'
13-
import { clearActiveFlow, loadFlowsListFx, setFlowsList } from './flow'
14-
import { clearNodes, initInterpolatorFx } from './nodes'
10+
import { fetchCategorizedNodesFx } from './categories'
11+
import { globalReset } from './common'
12+
import { loadFlowsListFx } from './flow'
13+
import { initInterpolatorFx } from './nodes'
1514

1615
/**
1716
* Initialize all stores and load initial data
@@ -43,10 +42,5 @@ export async function initializeStores(callback?: (
4342
}
4443

4544
export function reset() {
46-
resetCategories()
47-
clearNodes()
48-
resetEdges()
49-
clearActiveFlow()
50-
setFlowsList([])
51-
clearExecutionState()
45+
globalReset()
5246
}

apps/chaingraph-frontend/src/store/nodes/stores.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { DefaultPosition } from '@badaitech/chaingraph-types'
1414

1515
import { combine, sample } from 'effector'
1616
import { $categoryMetadata } from '../categories'
17+
import { globalReset } from '../common'
1718
import { nodesDomain } from '../domains'
1819
import { updatePort, updatePortUI, updatePortValue } from '../ports'
1920
import { $trpcClient } from '../trpc/store'
@@ -294,6 +295,7 @@ export const $nodes = nodesDomain.createStore<Record<string, INode>>({})
294295

295296
return { ...state, [nodeId]: updatedNode }
296297
})
298+
.reset(globalReset)
297299

298300
/**
299301
* Enhanced store for XYFlow nodes that preserves node references when only positions change.
@@ -480,15 +482,18 @@ export const $isNodesLoading = nodesDomain.createStore(false)
480482
.on(setNodesLoading, (_, isLoading) => isLoading)
481483
.on(addNodeToFlowFx.pending, (_, isPending) => isPending)
482484
.on(removeNodeFromFlowFx.pending, (_, isPending) => isPending)
485+
.reset(globalReset)
483486

484487
// Error states
485488
export const $addNodeError = nodesDomain.createStore<Error | null>(null)
486489
.on(addNodeToFlowFx.failData, (_, error) => error)
487490
.reset(addNodeToFlowFx.done)
491+
.reset(globalReset)
488492

489493
export const $removeNodeError = nodesDomain.createStore<Error | null>(null)
490494
.on(removeNodeFromFlowFx.failData, (_, error) => error)
491495
.reset(removeNodeFromFlowFx.done)
496+
.reset(globalReset)
492497

493498
// Combined error store
494499
export const $nodesError = combine(

0 commit comments

Comments
 (0)