Skip to content

Commit be06899

Browse files
committed
Use source location for subscriber name
1 parent 991b120 commit be06899

File tree

10 files changed

+51
-23
lines changed

10 files changed

+51
-23
lines changed

Sources/Atoms/Context/AtomTestContext.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ public struct AtomTestContext: AtomWatchableContext {
1212
private let state: State
1313

1414
/// Creates a new test context instance with fresh internal state.
15-
public init() {
16-
state = State()
15+
public init(fileID: String = #fileID, line: UInt = #line) {
16+
let location = SourceLocation(fileID: fileID, line: line)
17+
state = State(location: location)
1718
}
1819

1920
/// A callback to perform when any of atoms watched by this context is updated.
@@ -264,16 +265,21 @@ private extension AtomTestContext {
264265
private let _store = Store()
265266
private let _container = SubscriptionContainer()
266267

267-
var overrides = Overrides()
268+
let location: SourceLocation
268269
let notifier = PassthroughSubject<Void, Never>()
270+
var overrides = Overrides()
269271
var onUpdate: (() -> Void)?
270272

273+
init(location: SourceLocation) {
274+
self.location = location
275+
}
276+
271277
var store: StoreContext {
272278
StoreContext(_store, overrides: overrides)
273279
}
274280

275281
var container: SubscriptionContainer.Wrapper {
276-
_container.wrapper
282+
_container.wrapper(location: location)
277283
}
278284

279285
func notifyUpdate() {

Sources/Atoms/Core/AtomTypeKey.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ internal struct AtomTypeKey: Hashable, CustomStringConvertible {
77
getName = { String(describing: Node.self) }
88
}
99

10-
static func == (lhs: Self, rhs: Self) -> Bool {
11-
lhs.identifier == rhs.identifier
12-
}
13-
1410
var description: String {
1511
getName()
1612
}
1713

1814
func hash(into hasher: inout Hasher) {
1915
hasher.combine(identifier)
2016
}
17+
18+
static func == (lhs: Self, rhs: Self) -> Bool {
19+
lhs.identifier == rhs.identifier
20+
}
2121
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
internal struct SourceLocation: Equatable {
2+
let fileID: String
3+
let line: UInt
4+
}

Sources/Atoms/Core/SubscriptionContainer.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
internal final class SubscriptionContainer {
44
private var subscriptions = [AtomKey: Subscription]()
55

6-
var wrapper: Wrapper {
7-
Wrapper(self)
8-
}
9-
106
nonisolated init() {}
117

128
deinit {
139
for subscription in ContiguousArray(subscriptions.values) {
1410
subscription.unsubscribe()
1511
}
1612
}
13+
14+
func wrapper(location: SourceLocation) -> Wrapper {
15+
Wrapper(self, location: location)
16+
}
1717
}
1818

1919
internal extension SubscriptionContainer {
@@ -29,9 +29,9 @@ internal extension SubscriptionContainer {
2929
nonmutating set { container?.subscriptions = newValue }
3030
}
3131

32-
init(_ container: SubscriptionContainer) {
32+
init(_ container: SubscriptionContainer, location: SourceLocation) {
3333
self.container = container
34-
self.key = SubscriptionKey(container)
34+
self.key = SubscriptionKey(container, location: location)
3535
}
3636
}
3737
}
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
internal struct SubscriptionKey: Hashable, CustomStringConvertible {
22
private let identifier: ObjectIdentifier
3+
private let location: SourceLocation
34

4-
init(_ container: SubscriptionContainer) {
5-
identifier = ObjectIdentifier(container)
5+
init(_ container: SubscriptionContainer, location: SourceLocation) {
6+
self.identifier = ObjectIdentifier(container)
7+
self.location = location
68
}
79

810
var description: String {
9-
"Subscriber(\(hashValue))"
11+
"\(location.fileID):\(location.line)"
12+
}
13+
14+
// Ignores `location` because it is a debugging metadata.
15+
func hash(into hasher: inout Hasher) {
16+
hasher.combine(identifier)
17+
}
18+
19+
static func == (lhs: Self, rhs: Self) -> Bool {
20+
lhs.identifier == rhs.identifier
1021
}
1122
}

Sources/Atoms/PropertyWrapper/ViewContext.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ public struct ViewContext: DynamicProperty {
3939
@Environment(\.store)
4040
private var _store
4141

42+
private let location: SourceLocation
43+
4244
/// Creates a view context.
43-
public init() {
45+
public init(fileID: String = #fileID, line: UInt = #line) {
4446
_state = StateObject(wrappedValue: State())
47+
location = SourceLocation(fileID: fileID, line: line)
4548
}
4649

4750
/// The underlying view context to interact with atoms.
@@ -52,7 +55,7 @@ public struct ViewContext: DynamicProperty {
5255
public var wrappedValue: AtomViewContext {
5356
AtomViewContext(
5457
store: _store,
55-
container: state.container.wrapper,
58+
container: state.container.wrapper(location: location),
5659
notifyUpdate: state.objectWillChange.send
5760
)
5861
}

Sources/Atoms/PropertyWrapper/Watch.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ public struct Watch<Node: Atom>: DynamicProperty {
2929
private var context
3030

3131
/// Creates a watch with the atom that to be watched.
32-
public init(_ atom: Node) {
32+
public init(_ atom: Node, fileID: String = #fileID, line: UInt = #line) {
3333
self.atom = atom
34+
self._context = ViewContext(fileID: fileID, line: line)
3435
}
3536

3637
/// The underlying value associated with the given atom.

Sources/Atoms/PropertyWrapper/WatchState.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ public struct WatchState<Node: StateAtom>: DynamicProperty {
3838
private var context
3939

4040
/// Creates a watch with the atom that to be watched.
41-
public init(_ atom: Node) {
41+
public init(_ atom: Node, fileID: String = #fileID, line: UInt = #line) {
4242
self.atom = atom
43+
self._context = ViewContext(fileID: fileID, line: line)
4344
}
4445

4546
/// The underlying value associated with the given atom.

Sources/Atoms/PropertyWrapper/WatchStateObject.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ public struct WatchStateObject<Node: ObservableObjectAtom>: DynamicProperty {
7272
private var context
7373

7474
/// Creates a watch with the atom that to be watched.
75-
public init(_ atom: Node) {
75+
public init(_ atom: Node, fileID: String = #fileID, line: UInt = #line) {
7676
self.atom = atom
77+
self._context = ViewContext(fileID: fileID, line: line)
7778
}
7879

7980
/// The underlying observable object associated with the given atom.

Sources/Atoms/Snapshot.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ public struct Snapshot: CustomStringConvertible {
6363

6464
if let subscribers = subscriptions[key]?.keys, !subscribers.isEmpty {
6565
for subscriber in subscribers {
66+
let node = "\(subscriber.description.quoted) [style=filled]"
6667
let edge = "\(key.description.quoted) -> \(subscriber.description.quoted)"
68+
statements.append(node)
6769
statements.append(edge)
68-
statements.append("\(subscriber.description.quoted) [style=filled]")
6970
}
7071
}
7172
}

0 commit comments

Comments
 (0)