Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Sources/StateGraph/Observation/withGraphTrackingGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,15 @@ public func withGraphTrackingGroup(
return
}

let _handlerBox = OSAllocatedUnfairLock<(() -> Void)?>(uncheckedState: handler)
let _handlerBox = OSAllocatedUnfairLock<ClosureBox?>(
uncheckedState: .init(handler: handler)
)

withContinuousStateGraphTracking(
apply: {
_handlerBox.withLock { $0?() }
_handlerBox.withLock {
$0?.handler()
}
},
didChange: {
guard !_handlerBox.withLock({ $0 == nil }) else { return .stop }
Expand All @@ -76,3 +80,11 @@ public func withGraphTrackingGroup(
ThreadLocal.subscriptions.value!.append(cancellabe)

}

private struct ClosureBox {
let handler: () -> Void

init(handler: @escaping () -> Void) {
self.handler = handler
}
}
52 changes: 31 additions & 21 deletions Sources/StateGraph/Observation/withGraphTrackingMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,19 @@ public func withGraphTrackingMap<Projection>(

var filter = filter

typealias Handler = () -> Void
let _handlerBox = OSAllocatedUnfairLock<Handler?>(uncheckedState: {
let result = applier()
let filtered = filter.send(value: result)
if let filtered {
onChange(filtered)
}
})
let _handlerBox = OSAllocatedUnfairLock<ClosureBox?>(
uncheckedState: .init(handler: {
let result = applier()
let filtered = filter.send(value: result)
if let filtered {
onChange(filtered)
}
})
)

withContinuousStateGraphTracking(
apply: {
_handlerBox.withLock { $0?() }
_handlerBox.withLock { $0?.handler() }
},
didChange: {
guard !_handlerBox.withLock({ $0 == nil }) else { return .stop }
Expand Down Expand Up @@ -283,25 +284,26 @@ public func withGraphTrackingMap<Dependency: AnyObject, Projection>(

var filter = filter

typealias Handler = () -> Void
let _handlerBox = OSAllocatedUnfairLock<Handler?>(uncheckedState: {
guard let dependency = weakDependency else {
return
}
let result = map(dependency)
let filtered = filter.send(value: result)
if let filtered {
onChange(filtered)
}
})
let _handlerBox = OSAllocatedUnfairLock<ClosureBox?>(
uncheckedState: .init(handler: {
guard let dependency = weakDependency else {
return
}
let result = map(dependency)
let filtered = filter.send(value: result)
if let filtered {
onChange(filtered)
}
})
)

withContinuousStateGraphTracking(
apply: {
guard weakDependency != nil else {
_handlerBox.withLock { $0 = nil }
return
}
_handlerBox.withLock { $0?() }
_handlerBox.withLock { $0?.handler() }
},
didChange: {
guard !_handlerBox.withLock({ $0 == nil }) else { return .stop }
Expand All @@ -317,3 +319,11 @@ public func withGraphTrackingMap<Dependency: AnyObject, Projection>(

ThreadLocal.subscriptions.value!.append(cancellable)
}

private struct ClosureBox {
let handler: () -> Void

init(handler: @escaping () -> Void) {
self.handler = handler
}
}
Loading