Skip to content

Commit

Permalink
Add lock and projectedValue (#9)
Browse files Browse the repository at this point in the history
* Add lock and projectedValue

* Reduce Swift version

* Build instead of test

* Update macOS.yml

* Update macOS.yml
  • Loading branch information
0xLeif authored Nov 3, 2023
1 parent c539f14 commit 1dbfedd
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 25 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/docc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/macOS.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// 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

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.
Expand Down
9 changes: 8 additions & 1 deletion Sources/AppState/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import SwiftUI
}
}

public var projectedValue: Binding<Value> {
Binding(
get: { wrappedValue },
set: { wrappedValue = $0 }
)
}

public init(
_ keyPath: KeyPath<Application, Application.State<Value>>
) {
Expand All @@ -38,7 +45,7 @@ import SwiftUI
guard
let publisher = observed.objectWillChange as? ObservableObjectPublisher
else { return }

publisher.send()
observed[keyPath: storageKeyPath].wrappedValue = newValue
}
Expand Down
8 changes: 2 additions & 6 deletions Sources/AppState/Application+internal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value>(keyPath: KeyPath<Application, Value>) -> Value {
self[keyPath: keyPath]
}
}
8 changes: 4 additions & 4 deletions Sources/AppState/Application+public.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value> {
state(
initial: initial(),
Expand Down Expand Up @@ -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(),
Expand Down
8 changes: 8 additions & 0 deletions Sources/AppState/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnyCancellable>

let cache: Cache<String, Any>
Expand All @@ -16,12 +17,19 @@ public class Application: ObservableObject {
}

private init() {
lock = NSLock()
bag = Set()
cache = Cache()

consume(object: cache)
}

func value<Value>(keyPath: KeyPath<Application, Value>) -> Value {
lock.lock(); defer { lock.unlock() }

return self[keyPath: keyPath]
}

private func consume<Object: ObservableObject>(
object: Object
) where ObjectWillChangePublisher == ObservableObjectPublisher {
Expand Down

0 comments on commit 1dbfedd

Please sign in to comment.