Skip to content
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
1 change: 0 additions & 1 deletion .github/workflows/ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ on:
- 'mise*.toml'
- '.github/workflows/ios.yml'
pull_request:
branches: [main]
paths:
- 'Sources/**'
- 'Tests/**'
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ on:
- 'Scripts/CI/**'
- '.github/workflows/macos.yml'
pull_request:
branches: [main]
paths:
- 'Sources/**'
- 'Tests/**'
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ on:
- 'Package.swift'
- '.github/workflows/ubuntu.yml'
pull_request:
branches: [main]
paths:
- 'Sources/**'
- 'Tests/**'
Expand Down
71 changes: 68 additions & 3 deletions Sources/OpenSwiftUICore/Data/State/StoredLocation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ package class StoredLocationBase<Value>: AnyLocation<Value>, Location, @unchecke
_openSwiftUIBaseClassAbstractMethod()
}

fileprivate func commit(transaction: Transaction, mutation: BeginUpdate) {
fileprivate func commit(transaction: Transaction, id: Transaction.ID, mutation: BeginUpdate) {
_openSwiftUIBaseClassAbstractMethod()
}

Expand Down Expand Up @@ -139,12 +139,13 @@ package class StoredLocationBase<Value>: AnyLocation<Value>, Location, @unchecke
return
}
let transaction = transaction.current
let id = Transaction.id
onMainThread { [weak self] in
guard let self else {
return
}
let update = BeginUpdate(box: self)
commit(transaction: transaction, mutation: update)
commit(transaction: transaction, id: id, mutation: update)
}
}

Expand Down Expand Up @@ -174,9 +175,14 @@ final package class StoredLocation<Value>: StoredLocationBase<Value>, @unchecked
host?.isUpdating ?? false
}

override fileprivate func commit(transaction: Transaction, mutation: StoredLocationBase<Value>.BeginUpdate) {
override fileprivate func commit(
transaction: Transaction,
id: Transaction.ID,
mutation: StoredLocationBase<Value>.BeginUpdate
) {
host?.asyncTransaction(
transaction,
id: id,
mutation: mutation
)
}
Expand All @@ -185,3 +191,62 @@ final package class StoredLocation<Value>: StoredLocationBase<Value>, @unchecked
$signal?.invalidateValue()
}
}

// MARK: - ObservableLocation

final package class ObservableLocation<Value>: StoredLocationBase<Value>, TransactionHostProvider, @unchecked Sendable {
private struct Observer {
weak var host: GraphHost?
var signal: WeakAttribute<Void>
}

private var observers: [Observer] = []

package func addObserver(host: GraphHost, signal: WeakAttribute<Void>) {
observers.append(Observer(host: host, signal: signal))
}

package func removeObserver(signal: WeakAttribute<Void>) {
if let index = observers.firstIndex(where: { $0.signal == signal }) {
observers.remove(at: index)
}
}

override fileprivate var isUpdating: Bool {
GraphHost.isUpdating
}

override fileprivate func commit(
transaction: Transaction,
id: Transaction.ID,
mutation: StoredLocationBase<Value>.BeginUpdate
) {
GraphHost.globalTransaction(
transaction,
id: id,
mutation: mutation,
hostProvider: self
)
}

override fileprivate func notifyObservers() {
var index = 0
var count = observers.count
while index < count {
if let signal = observers[index].signal.attribute {
signal.invalidateValue()
index += 1
} else {
count -= 1
if index != count {
observers.swapAt(index, count)
}
observers.remove(at: count)
}
}
}

package var mutationHost: GraphHost? {
observers.reduce(nil) { $0 ?? $1.host }
}
}
Loading