From d1d36239da8aba40260a21e127cbfc94b5518c6c Mon Sep 17 00:00:00 2001 From: Muukii Date: Sat, 28 Oct 2023 19:03:33 +0900 Subject: [PATCH] Patch --- Sources/Verge/Store/AnyTargetQueue.swift | 54 +++++++++++++++-------- Sources/Verge/Utility/ReferenceEdge.swift | 7 ++- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/Sources/Verge/Store/AnyTargetQueue.swift b/Sources/Verge/Store/AnyTargetQueue.swift index 10db10c0c7..f88a3b2d6d 100644 --- a/Sources/Verge/Store/AnyTargetQueue.swift +++ b/Sources/Verge/Store/AnyTargetQueue.swift @@ -114,6 +114,13 @@ extension TargetQueueType where Self == Queues.Passthrough { } +extension TargetQueueType where Self == Queues.AsyncBackground { + /// It dispatches to the serial background queue asynchronously. + public static var asyncSerialBackground: Self { + self.init() + } +} + extension TargetQueueType where Self == AnyTargetQueue { @@ -123,10 +130,7 @@ extension TargetQueueType where Self == AnyTargetQueue { targetQueue.async(execute: workItem) } } - /// It dispatches to the serial background queue asynchronously. - public static var asyncSerialBackground: AnyTargetQueue { - AnyTargetQueue._asyncSerialBackground - } + /// Enqueue first item on current-thread(synchronously). /// From then, using specified queue. @@ -175,21 +179,6 @@ extension MainActorTargetQueue { } } -extension AnyTargetQueue { - - /// Returns a instance that never dispatches. - /// The Sink use this targetQueue performs in the queue which the upstream commit dispatched. - static let _passthrough: AnyTargetQueue = .init { workItem in - workItem() - } - - /// It dispatches to the serial background queue asynchronously. - static let _asyncSerialBackground: AnyTargetQueue = .init { workItem in - StaticMember.serialBackgroundDispatchQueue.async(execute: workItem) - } - -} - public enum Queues { struct MainActor: TargetQueueType { @@ -214,4 +203,31 @@ public enum Queues { } + public struct AsyncBackground: TargetQueueType { + + private let executor: BackgroundActor = .init() + + public func execute(_ workItem: @escaping () -> Void) { + Task { + await executor.perform { + workItem() + } + } + } + + private actor BackgroundActor: Actor { + + init() { + + } + + func perform(_ operation: () throws -> R) rethrows -> R { + try operation() + } + + } + + } + } + diff --git a/Sources/Verge/Utility/ReferenceEdge.swift b/Sources/Verge/Utility/ReferenceEdge.swift index c884c376b6..8fe7b06ed7 100644 --- a/Sources/Verge/Utility/ReferenceEdge.swift +++ b/Sources/Verge/Utility/ReferenceEdge.swift @@ -80,7 +80,12 @@ public struct ReferenceEdge: EdgeType { } public var projectedValue: Self { - self + get { + self + } + mutating set { + self = newValue + } } public func read(_ thunk: (borrowing State) -> T) -> T {