diff --git a/Sources/StateGraph/Observation/withGraphTrackingGroup.swift b/Sources/StateGraph/Observation/withGraphTrackingGroup.swift index 3dd3401..5c290b3 100644 --- a/Sources/StateGraph/Observation/withGraphTrackingGroup.swift +++ b/Sources/StateGraph/Observation/withGraphTrackingGroup.swift @@ -56,11 +56,15 @@ public func withGraphTrackingGroup( return } - let _handlerBox = OSAllocatedUnfairLock<(() -> Void)?>(uncheckedState: handler) + let _handlerBox = OSAllocatedUnfairLock( + uncheckedState: .init(handler: handler) + ) withContinuousStateGraphTracking( apply: { - _handlerBox.withLock { $0?() } + _handlerBox.withLock { + $0?.handler() + } }, didChange: { guard !_handlerBox.withLock({ $0 == nil }) else { return .stop } @@ -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 + } +} diff --git a/Sources/StateGraph/Observation/withGraphTrackingMap.swift b/Sources/StateGraph/Observation/withGraphTrackingMap.swift index 7f5e0d5..b02f39e 100644 --- a/Sources/StateGraph/Observation/withGraphTrackingMap.swift +++ b/Sources/StateGraph/Observation/withGraphTrackingMap.swift @@ -133,18 +133,19 @@ public func withGraphTrackingMap( var filter = filter - typealias Handler = () -> Void - let _handlerBox = OSAllocatedUnfairLock(uncheckedState: { - let result = applier() - let filtered = filter.send(value: result) - if let filtered { - onChange(filtered) - } - }) + let _handlerBox = OSAllocatedUnfairLock( + 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 } @@ -283,17 +284,18 @@ public func withGraphTrackingMap( var filter = filter - typealias Handler = () -> Void - let _handlerBox = OSAllocatedUnfairLock(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( + 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: { @@ -301,7 +303,7 @@ public func withGraphTrackingMap( _handlerBox.withLock { $0 = nil } return } - _handlerBox.withLock { $0?() } + _handlerBox.withLock { $0?.handler() } }, didChange: { guard !_handlerBox.withLock({ $0 == nil }) else { return .stop } @@ -317,3 +319,11 @@ public func withGraphTrackingMap( ThreadLocal.subscriptions.value!.append(cancellable) } + +private struct ClosureBox { + let handler: () -> Void + + init(handler: @escaping () -> Void) { + self.handler = handler + } +}