Skip to content

Commit

Permalink
resolve comments and fix ambiguous issue when targeting swift 5.9
Browse files Browse the repository at this point in the history
  • Loading branch information
hank121314 committed Nov 14, 2023
1 parent 04d7dbc commit f38a785
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 42 deletions.
11 changes: 11 additions & 0 deletions Sources/Defaults/Defaults+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,14 @@ extension UIColor: Defaults.Serializable {}

extension NSUbiquitousKeyValueStore: Defaults.KeyValueStore {}
extension UserDefaults: Defaults.KeyValueStore {}

extension _DefaultsLockProtocol {
@discardableResult
func with<R>(_ body: @Sendable () throws -> R) rethrows -> R where R : Sendable {
self.lock()
defer {
self.unlock()
}
return try body()
}
}
2 changes: 2 additions & 0 deletions Sources/Defaults/Defaults+Protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ protocol _DefaultsLockProtocol {
func lock()

func unlock()

func with<R>(_ body: @Sendable () throws -> R) rethrows -> R where R : Sendable
}
48 changes: 25 additions & 23 deletions Sources/Defaults/Defaults+iCloud.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import AppKit
import Combine
import Foundation

/// Represent different data sources available for synchronization.
/**
Represent different data sources available for synchronization.
*/
public enum DataSource {
/// Using `key.suite` as data source.
case local
Expand All @@ -18,9 +20,9 @@ public enum DataSource {
}

private enum SyncStatus {
case start
case isSyncing
case finish
case idle
case syncing
case completed
}

extension Defaults {
Expand Down Expand Up @@ -152,7 +154,7 @@ extension Defaults {
- Parameter source: Sync key from which data source(remote or local).
*/
private func syncKey(_ key: Defaults.Keys, _ source: DataSource) async {
Self.logKeySyncStatus(key, source: source, syncStatus: .start)
Self.logKeySyncStatus(key, source: source, syncStatus: .idle)
switch source {
case .remote:
await syncFromRemote(key: key)
Expand All @@ -161,7 +163,7 @@ extension Defaults {
syncFromLocal(key: key)
recordTimestamp(.remote)
}
Self.logKeySyncStatus(key, source: source, syncStatus: .finish)
Self.logKeySyncStatus(key, source: source, syncStatus: .completed)
}

/**
Expand All @@ -176,7 +178,7 @@ extension Defaults {
}

Task { @MainActor in
Self.logKeySyncStatus(key, source: .remote, syncStatus: .isSyncing, value: value)
Self.logKeySyncStatus(key, source: .remote, syncStatus: .syncing, value: value)
key.suite.set(value, forKey: key.name)
continuation.resume()
}
Expand All @@ -189,13 +191,13 @@ extension Defaults {
*/
private func syncFromLocal(key: Defaults.Keys) {
guard let value = key.suite.object(forKey: key.name) else {
Self.logKeySyncStatus(key, source: .local, syncStatus: .isSyncing, value: nil)
Self.logKeySyncStatus(key, source: .local, syncStatus: .syncing, value: nil)
remoteStorage.removeObject(forKey: key.name)
syncRemoteStorageOnChange()
return
}

Self.logKeySyncStatus(key, source: .local, syncStatus: .isSyncing, value: value)
Self.logKeySyncStatus(key, source: .local, syncStatus: .syncing, value: value)
remoteStorage.set(value, forKey: key.name)
syncRemoteStorageOnChange()
}
Expand Down Expand Up @@ -328,13 +330,13 @@ extension Defaults.iCloudSynchronizer {
var status: String
var valueDescription = " "
switch syncStatus {
case .start:
status = "Start synchronization"
case .isSyncing:
case .idle:
status = "Try synchronizing"
case .syncing:
status = "Synchronizing"
valueDescription = " with value \(value ?? "nil") "
case .finish:
status = "Finish synchronization"
case .completed:
status = "Complete synchronization"
}
let message = "\(status) key '\(key.name)'\(valueDescription)\(destination)"

Expand Down Expand Up @@ -388,12 +390,12 @@ extension Defaults {
/**
The singleton for Defaults's iCloudSynchronizer.
*/
static var shared = Defaults.iCloudSynchronizer(remoteStorage: NSUbiquitousKeyValueStore.default)
static var synchronizer = Defaults.iCloudSynchronizer(remoteStorage: NSUbiquitousKeyValueStore.default)

/**
Lists the synced keys.
*/
public static let keys = shared.keys
public static let keys = synchronizer.keys

/**
Enable this if you want to call `NSUbiquitousKeyValueStore.synchronize` when a value is changed.
Expand All @@ -409,42 +411,42 @@ extension Defaults {
Add the keys to be automatically synced.
*/
public static func add(_ keys: Defaults.Keys...) {
shared.add(keys)
synchronizer.add(keys)
}

/**
Remove the keys that are set to be automatically synced.
*/
public static func remove(_ keys: Defaults.Keys...) {
shared.remove(keys)
synchronizer.remove(keys)
}

/**
Remove all keys that are set to be automatically synced.
*/
public static func removeAll() {
shared.removeAll()
synchronizer.removeAll()
}

/**
Explicitly synchronizes in-memory keys and values with those stored on disk.
*/
public static func synchronize() {
shared.synchronize()
synchronizer.synchronize()
}

/**
Wait until all synchronization tasks are complete.
*/
public static func sync() async {
await shared.sync()
await synchronizer.sync()
}

/**
Create synchronization tasks for all the keys that have been added to the `Defaults.iCloud`.
*/
public static func syncWithoutWaiting() {
shared.syncWithoutWaiting()
synchronizer.syncWithoutWaiting()
}

/**
Expand All @@ -456,7 +458,7 @@ extension Defaults {
- Note: `source` should be specify if `key` has not been added to `Defaults.iCloud`.
*/
public static func syncWithoutWaiting(_ keys: Defaults.Keys..., source: DataSource? = nil) {
shared.syncWithoutWaiting(keys, source)
synchronizer.syncWithoutWaiting(keys, source)
}
}
}
27 changes: 9 additions & 18 deletions Sources/Defaults/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,16 +234,6 @@ extension Defaults.Serializable {
}
}

extension AsyncStream {
public static func makeStream(
_ elementType: Element.Type = Element.self,
bufferingPolicy limit: Continuation.BufferingPolicy = .unbounded
) -> (stream: Self, continuation: Continuation?) {
var continuation: Continuation?
return (Self(elementType, bufferingPolicy: limit) { continuation = $0 }, continuation)
}
}

// swiftlint:disable:next final_class
class Lock: Defaults.LockProtocol {
final class UnfairLock: Lock {
Expand Down Expand Up @@ -323,7 +313,7 @@ final class TaskQueue {
private let lock: Lock = .make()

init(priority: TaskPriority? = nil) {
let (taskStream, queueContinuation) = AsyncStream<AsyncTask>.makeStream()
let (taskStream, queueContinuation) = AsyncStream<AsyncTask>.makeStream()
self.queueContinuation = queueContinuation

Task.detached(priority: priority) {
Expand All @@ -341,9 +331,9 @@ final class TaskQueue {
Queue a new asynchronous task.
*/
func async(_ task: @escaping AsyncTask) {
lock.lock()
queueContinuation?.yield(task)
lock.unlock()
lock.with {
queueContinuation?.yield(task)
}
}

/**
Expand All @@ -365,11 +355,12 @@ final class TaskQueue {
*/
func flush() async {
await withCheckedContinuation { continuation in
lock.lock()
queueContinuation?.yield {
continuation.resume()
lock.with {
queueContinuation?.yield {
continuation.resume()
}
return
}
lock.unlock()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/DefaultsTests/Defaults+iCloudTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ private let mockStorage = MockStorage()
final class DefaultsICloudTests: XCTestCase {
override class func setUp() {
Defaults.iCloud.isDebug = true
Defaults.iCloud.shared = Defaults.iCloudSynchronizer(remoteStorage: mockStorage)
Defaults.iCloud.synchronizer = Defaults.iCloudSynchronizer(remoteStorage: mockStorage)
}

override func setUp() {
Expand Down

0 comments on commit f38a785

Please sign in to comment.