Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static TargetQueue #444

Merged
merged 1 commit into from
Oct 28, 2023
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
54 changes: 35 additions & 19 deletions Sources/Verge/Store/AnyTargetQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {


Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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<R>(_ operation: () throws -> R) rethrows -> R {
try operation()
}

}

}

}

7 changes: 6 additions & 1 deletion Sources/Verge/Utility/ReferenceEdge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ public struct ReferenceEdge<State>: EdgeType {
}

public var projectedValue: Self {
self
get {
self
}
mutating set {
self = newValue
}
}

public func read<T>(_ thunk: (borrowing State) -> T) -> T {
Expand Down
Loading