From 1dbfedde046e3b87b4258860223a3e5b256901fa Mon Sep 17 00:00:00 2001 From: Zach Date: Thu, 2 Nov 2023 19:55:09 -0600 Subject: [PATCH] Add lock and projectedValue (#9) * Add lock and projectedValue * Reduce Swift version * Build instead of test * Update macOS.yml * Update macOS.yml --- .github/workflows/docc.yml | 5 +---- .github/workflows/macOS.yml | 8 +++----- Package.swift | 10 +++++----- Sources/AppState/AppState.swift | 9 ++++++++- Sources/AppState/Application+internal.swift | 8 ++------ Sources/AppState/Application+public.swift | 8 ++++---- Sources/AppState/Application.swift | 8 ++++++++ 7 files changed, 31 insertions(+), 25 deletions(-) diff --git a/.github/workflows/docc.yml b/.github/workflows/docc.yml index ddd91c4..b9d19e2 100644 --- a/.github/workflows/docc.yml +++ b/.github/workflows/docc.yml @@ -14,13 +14,10 @@ jobs: environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} - runs-on: macos-13 + runs-on: macos-12 steps: - name: git checkout uses: actions/checkout@v3 - - uses: maxim-lobanov/setup-xcode@v1 - with: - xcode-version: latest-stable - name: docbuild run: | xcodebuild docbuild -scheme AppState \ diff --git a/.github/workflows/macOS.yml b/.github/workflows/macOS.yml index d655f3e..bca58a6 100644 --- a/.github/workflows/macOS.yml +++ b/.github/workflows/macOS.yml @@ -1,7 +1,7 @@ # This workflow will build a Swift project # For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-swift -name: macOS +name: Tests on: push: @@ -17,7 +17,5 @@ jobs: with: xcode-version: latest-stable - uses: actions/checkout@v3 - - name: Build - run: swift build -v - - name: Run tests - run: swift test -v + - name: Build for release + run: swift build -v -c release diff --git a/Package.swift b/Package.swift index 7787ad2..86c3879 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 5.9 +// swift-tools-version: 5.7 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription @@ -6,10 +6,10 @@ import PackageDescription let package = Package( name: "AppState", platforms: [ - .iOS(.v17), - .watchOS(.v10), - .macOS(.v14), - .tvOS(.v17) + .iOS(.v16), + .watchOS(.v9), + .macOS(.v13), + .tvOS(.v16) ], products: [ // Products define the executables and libraries a package produces, making them visible to other packages. diff --git a/Sources/AppState/AppState.swift b/Sources/AppState/AppState.swift index 4db60fe..8904e14 100644 --- a/Sources/AppState/AppState.swift +++ b/Sources/AppState/AppState.swift @@ -20,6 +20,13 @@ import SwiftUI } } + public var projectedValue: Binding { + Binding( + get: { wrappedValue }, + set: { wrappedValue = $0 } + ) + } + public init( _ keyPath: KeyPath> ) { @@ -38,7 +45,7 @@ import SwiftUI guard let publisher = observed.objectWillChange as? ObservableObjectPublisher else { return } - + publisher.send() observed[keyPath: storageKeyPath].wrappedValue = newValue } diff --git a/Sources/AppState/Application+internal.swift b/Sources/AppState/Application+internal.swift index 9ac2de5..4f44936 100644 --- a/Sources/AppState/Application+internal.swift +++ b/Sources/AppState/Application+internal.swift @@ -2,13 +2,9 @@ extension Application { static func codeID( fileID: StaticString, function: StaticString, - line: StaticBigInt, - column: StaticBigInt + line: Int, + column: Int ) -> String { "\(fileID)[\(function)@\(line)|\(column)]" } - - func value(keyPath: KeyPath) -> Value { - self[keyPath: keyPath] - } } diff --git a/Sources/AppState/Application+public.swift b/Sources/AppState/Application+public.swift index 50c3e1a..9d28cfd 100644 --- a/Sources/AppState/Application+public.swift +++ b/Sources/AppState/Application+public.swift @@ -39,8 +39,8 @@ public extension Application { initial: @autoclosure () -> Value, _ fileID: StaticString = #fileID, _ function: StaticString = #function, - _ line: StaticBigInt = #line, - _ column: StaticBigInt = #column + _ line: Int = #line, + _ column: Int = #column ) -> State { state( initial: initial(), @@ -74,8 +74,8 @@ public extension Application { _ object: @autoclosure () -> Value, _ fileID: StaticString = #fileID, _ function: StaticString = #function, - _ line: StaticBigInt = #line, - _ column: StaticBigInt = #column + _ line: Int = #line, + _ column: Int = #column ) -> Value { dependency( object(), diff --git a/Sources/AppState/Application.swift b/Sources/AppState/Application.swift index 2df8e23..a393e3b 100644 --- a/Sources/AppState/Application.swift +++ b/Sources/AppState/Application.swift @@ -7,6 +7,7 @@ public class Application: ObservableObject { public static let logger: Logger = Logger(subsystem: "Application", category: "AppState") + private let lock: NSLock private var bag: Set let cache: Cache @@ -16,12 +17,19 @@ public class Application: ObservableObject { } private init() { + lock = NSLock() bag = Set() cache = Cache() consume(object: cache) } + func value(keyPath: KeyPath) -> Value { + lock.lock(); defer { lock.unlock() } + + return self[keyPath: keyPath] + } + private func consume( object: Object ) where ObjectWillChangePublisher == ObservableObjectPublisher {