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
2 changes: 1 addition & 1 deletion .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Run tests
run: set -o pipefail && xcodebuild test -scheme BSWFoundation -destination 'platform=iOS Simulator,name=iPhone 16,OS=latest' | xcbeautify --renderer github-actions
run: set -o pipefail && xcodebuild test -scheme BSWFoundation -destination 'platform=iOS Simulator,name=iPhone 17,OS=latest' | xcbeautify --renderer github-actions

android-build:
runs-on: mobile
Expand Down
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ if !zero {
let package = Package(
name: "BSWFoundation",
platforms: [
.iOS(.v16),
.tvOS(.v16),
.macOS(.v13),
.macCatalyst(.v16),
.watchOS(.v9),
.iOS(.v17),
.tvOS(.v17),
.macOS(.v15),
.macCatalyst(.v17),
.watchOS(.v10),
],
products: [
.library(
Expand Down
48 changes: 48 additions & 0 deletions Sources/BSWFoundation/Extensions/Observation+Ext.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Foundation
import Observation

extension Observable where Self: AnyObject & Sendable {

public func stream<Value: Sendable>(
for keyPath: KeyPath<Self, Value>
) -> AsyncStream<Value> {
let box = ObservationBox()
nonisolated(unsafe) let keyPath = keyPath

return AsyncStream(Value.self) { continuation in
@Sendable func track(object: Self) {
Observation.withObservationTracking { [weak object] in
guard let self = object, !box.isCancelled else { return }
let value = self[keyPath: keyPath]
continuation.yield(value)
} onChange: { [weak object] in
DispatchQueue.main.async {
guard let self = object, !box.isCancelled else { return }
track(object: self)
}
}
}

continuation.onTermination = { _ in
box.isCancelled = true
}

track(object: self)
}
}
}

extension AsyncStream where Element: Equatable {
public func until(_ e: Element) async {
var iterator = self.makeAsyncIterator()
while let value = await iterator.next() {
if e == value {
return
}
}
}
}

private final class ObservationBox: @unchecked Sendable {
var isCancelled = false
}
29 changes: 29 additions & 0 deletions Tests/BSWFoundationTests/Extensions/ObservationTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

import Testing
import Observation

@Suite
struct ObservationTests {

@Observable
@MainActor
class ViewModel {
var isReady: Bool = false
func setIsReady() {
isReady = true
}
}

@MainActor
@Test
func itCompletesCorrectly() async throws {
let viewModel = ViewModel()
Task {
try await Task.sleep(for: .milliseconds(20))
viewModel.setIsReady()
}
#expect(viewModel.isReady == false)
await viewModel.stream(for: \.isReady).until(true)
#expect(viewModel.isReady)
}
}