Skip to content

Commit 13998f0

Browse files
committed
Refactoring
1 parent 578e108 commit 13998f0

File tree

2 files changed

+9
-18
lines changed

2 files changed

+9
-18
lines changed

Sources/Atoms/Core/StoreContext.swift

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -389,28 +389,19 @@ private extension StoreContext {
389389
dependency._loader.performTransitiveUpdate(subscription.update)
390390
}
391391

392-
// Do not transitively update atoms that have dependency recorded not to update downstream.
393-
// However, if the topological sorting has already skipped the vertex as a redundant,
394-
// it should be performed.
395392
func convertToValidEdge(_ edge: Edge) -> Edge? {
393+
// Do not transitively update atoms that have dependency recorded not to update downstream.
396394
guard skippedDependencies.contains(edge.from) else {
397395
return edge
398396
}
399397

400-
guard let redundantDependencies = redundants[edge.to] else {
398+
// If the topological sorting has marked the vertex as a redundant, the update still performed.
399+
guard let fromKey = redundants[edge.to]?.first(where: { !skippedDependencies.contains($0) }) else {
401400
return nil
402401
}
403402

404-
// Topological sorting itself does not guarantee idempotent result when multiple
405-
// dependencies update simultaneously and there's no valid update order to determine
406-
// which atom triggered the transitive update, and thus here chooses a random
407-
// dependency atom from redundant edges.
408-
guard let fromKey = redundantDependencies.subtracting(skippedDependencies).first else {
409-
return nil
410-
}
411-
412-
// Convert edge's `from`, which represents a dependency atom, to a non-skipped one on
413-
// a best-effort basis to switch the update transaction context (e.g. animation).
403+
// Convert edge's `from`, which represents a dependency atom, to a non-skipped one to
404+
// change the update transaction context (e.g. animation).
414405
return Edge(from: fromKey, to: edge.to)
415406
}
416407

Sources/Atoms/Core/TopologicalSort.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ internal struct Edge: Hashable {
1212
@MainActor
1313
internal func topologicalSort(key: AtomKey, store: AtomStore) -> (
1414
edges: ReversedCollection<[Edge]>,
15-
redundants: [Vertex: Set<AtomKey>] // key = vertex, value = dependencies
15+
redundants: [Vertex: [AtomKey]] // key = vertex, value = dependencies
1616
) {
1717
var trace = Set<Vertex>()
1818
var edges = [Edge]()
19-
var redundants = [Vertex: Set<AtomKey>]()
19+
var redundants = [Vertex: [AtomKey]]()
2020

2121
func traverse(key: AtomKey, isRedundant: Bool) {
2222
if let children = store.graph.children[key] {
@@ -43,7 +43,7 @@ internal func topologicalSort(key: AtomKey, store: AtomStore) -> (
4343
traverse(key: key, isRedundant: isRedundant)
4444

4545
if isRedundant {
46-
redundants[vertex, default: []].insert(fromKey)
46+
redundants[vertex, default: []].append(fromKey)
4747
}
4848
else {
4949
let edge = Edge(from: fromKey, to: vertex)
@@ -58,7 +58,7 @@ internal func topologicalSort(key: AtomKey, store: AtomStore) -> (
5858
trace.insert(vertex)
5959

6060
if isRedundant {
61-
redundants[vertex, default: []].insert(fromKey)
61+
redundants[vertex, default: []].append(fromKey)
6262
}
6363
else {
6464
let edge = Edge(from: fromKey, to: vertex)

0 commit comments

Comments
 (0)