Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Adapt blocking updates to strict concurrency
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Sep 29, 2024
1 parent ebb6800 commit 9a3760b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
8 changes: 6 additions & 2 deletions Sources/Model/Data Flow/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ extension Model {
setModel(&model)
data.storage.value = model
data.storage.update = true
StateManager.updateViews(force: data.force)
Task {
await StateManager.updateViews(force: data.force)
}
}

/// Get the current version of the model.
Expand Down Expand Up @@ -114,7 +116,9 @@ extension Model where Self: Sendable {
}
data.storage.value = newValue
data.storage.update = true
StateManager.updateViews(force: data.force)
Task {
await StateManager.updateViews(force: data.force)
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion Sources/Model/Data Flow/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public struct State<Value>: StateProtocol, Sendable where Value: Sendable {
nonmutating set {
rawValue = newValue
content.update = true
StateManager.updateViews(force: forceUpdates)
Task {
await StateManager.updateViews(force: forceUpdates)
}
}
}

Expand Down
25 changes: 22 additions & 3 deletions Sources/Model/Data Flow/StateManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@
import Foundation

/// This type manages view updates.
@globalActor
public actor StateManager {

/// Whether to block updates in general.
public static var blockUpdates = false
@StateManager static var blockAllUpdates = false
/// The application identifier.
static var appID: String?
/// The functions handling view updates.
static var updateHandlers: [@Sendable (Bool) async -> Void] = []
/// The shared instance of the actor.
public static let shared = StateManager()

/// Update all of the views.
/// - Parameter force: Whether to force all views to update.
///
/// Nothing happens if ``StateManager/blockUpdates`` is true.
public static func updateViews(force: Bool = false) {
if !blockUpdates {
public static func updateViews(force: Bool = false) async {
if await !blockAllUpdates {
for handler in updateHandlers {
Task {
await handler(force)
Expand All @@ -37,4 +40,20 @@ public actor StateManager {
updateHandlers.append(handler)
}

/// Block all updates.
///
/// The user interface will not respond to changes.
@StateManager
public static func blockUpdates() {
blockAllUpdates = true
}

/// Unblock all updates.
///
/// The user interface will respond to changes.
@StateManager
public static func unblockUpdates() {
blockAllUpdates = false
}

}

0 comments on commit 9a3760b

Please sign in to comment.