Skip to content

Commit 7fd2f52

Browse files
authored
[Observation] Refactor for state machine (and behavior robustness) and adjust protocol requirement names (#64414)
* [Observation] Change visibility of observation runtime functions to be hidden * [Observation] Update API requirements for Observable AsyncSequence names and alter the behavior of value emissions to be based upon transactionality * [Observation] Slight naming alteration of isolation -> isolatedTo Note: This re-enables the previously disabled tests since the implementation should be considerably more robust to hangs.
1 parent 06725cc commit 7fd2f52

File tree

14 files changed

+797
-496
lines changed

14 files changed

+797
-496
lines changed

lib/Macros/Sources/ObservationMacros/ObservableMacro.swift

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,22 @@ public struct ObservableMacro: MemberMacro, MemberAttributeMacro, ConformanceMac
5151
let _registrar = ObservationRegistrar<\(parentName)>()
5252
"""
5353

54-
let transactions: DeclSyntax =
54+
let changes: DeclSyntax =
5555
"""
56-
public nonisolated func transactions<Delivery>(for properties: TrackedProperties<\(parentName)>, isolation: Delivery) -> ObservedTransactions<\(parentName), Delivery> where Delivery: Actor {
57-
_registrar.transactions(for: properties, isolation: isolation)
56+
public nonisolated func changes<Isolation: Actor>(
57+
for properties: TrackedProperties<\(parentName)>,
58+
isolatedTo isolation: Isolation
59+
) -> ObservedChanges<\(parentName), Isolation> {
60+
_registrar.changes(for: properties, isolatedTo: isolation)
5861
}
5962
"""
6063

61-
let changes: DeclSyntax =
64+
let values: DeclSyntax =
6265
"""
63-
public nonisolated func changes<Member>(for keyPath: KeyPath<\(parentName), Member>) -> ObservedChanges<\(parentName), Member> where Member: Sendable {
64-
_registrar.changes(for: keyPath)
66+
public nonisolated func values<Member: Sendable>(
67+
for keyPath: KeyPath<\(parentName), Member>
68+
) -> ObservedValues<\(parentName), Member> {
69+
_registrar.values(for: keyPath)
6570
}
6671
"""
6772

@@ -85,8 +90,8 @@ public struct ObservableMacro: MemberMacro, MemberAttributeMacro, ConformanceMac
8590

8691
return [
8792
registrar,
88-
transactions,
8993
changes,
94+
values,
9095
storageStruct,
9196
storage,
9297
]

stdlib/public/Observation/Sources/Observation/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212

1313
list(APPEND swift_runtime_library_compile_flags -I${SWIFT_SOURCE_DIR}/stdlib/include -I${SWIFT_SOURCE_DIR}/include)
1414

15-
add_swift_target_library(swiftObservation ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
15+
add_swift_target_library(swift_Observation ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
1616
Locking.cpp
1717
Locking.swift
1818
Macros.swift
1919
Observable.swift
2020
ObservationRegistrar.swift
21+
ObservationRegistrarStateMachine.swift
2122
ObservationTracking.swift
2223
ObservedChanges.swift
23-
ObservedTransactions.swift
24+
ObservedValues.swift
2425
ThreadLocal.cpp
2526
ThreadLocal.swift
2627
TrackedProperties.swift

stdlib/public/Observation/Sources/Observation/Locking.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
using namespace swift;
1717

18-
extern "C" SWIFT_CC(swift)
18+
extern "C" SWIFT_CC(swift) __attribute__((visibility("hidden")))
1919
size_t _swift_observation_lock_size() {
2020
size_t bytes = sizeof(Mutex);
2121

@@ -26,17 +26,17 @@ size_t _swift_observation_lock_size() {
2626
return bytes;
2727
}
2828

29-
extern "C" SWIFT_CC(swift)
29+
extern "C" SWIFT_CC(swift) __attribute__((visibility("hidden")))
3030
void _swift_observation_lock_init(Mutex &lock) {
3131
new (&lock) Mutex();
3232
}
3333

34-
extern "C" SWIFT_CC(swift)
34+
extern "C" SWIFT_CC(swift) __attribute__((visibility("hidden")))
3535
void _swift_observation_lock_lock(Mutex &lock) {
3636
lock.lock();
3737
}
3838

39-
extern "C" SWIFT_CC(swift)
39+
extern "C" SWIFT_CC(swift) __attribute__((visibility("hidden")))
4040
void _swift_observation_lock_unlock(Mutex &lock) {
4141
lock.unlock();
4242
}

stdlib/public/Observation/Sources/Observation/Locking.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2022 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information

stdlib/public/Observation/Sources/Observation/Macros.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#if $Macros && hasAttribute(attached)
1313

1414
@available(SwiftStdlib 5.9, *)
15-
@attached(member, names: named(_registrar), named(transactions), named(changes), named(_Storage), named(_storage))
15+
@attached(member, names: named(_registrar), named(changes), named(values), named(_Storage), named(_storage))
1616
@attached(memberAttribute)
1717
@attached(conformance)
1818
public macro Observable() =

stdlib/public/Observation/Sources/Observation/Observable.swift

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ import _Concurrency
1313

1414
@available(SwiftStdlib 5.9, *)
1515
public protocol Observable {
16-
nonisolated func transactions<Delivery: Actor>(
16+
nonisolated func changes<Isolation: Actor>(
1717
for properties: TrackedProperties<Self>,
18-
isolation: Delivery
19-
) -> ObservedTransactions<Self, Delivery>
18+
isolatedTo isolation: Isolation
19+
) -> ObservedChanges<Self, Isolation>
2020

21-
nonisolated func changes<Member: Sendable>(
21+
nonisolated func values<Member: Sendable>(
2222
for keyPath: KeyPath<Self, Member>
23-
) -> ObservedChanges<Self, Member>
23+
) -> ObservedValues<Self, Member>
2424

2525
nonisolated static func dependencies(
2626
of keyPath: PartialKeyPath<Self>
@@ -30,23 +30,23 @@ public protocol Observable {
3030

3131
@available(SwiftStdlib 5.9, *)
3232
extension Observable {
33-
public nonisolated func transactions<Member, Delivery: Actor>(
33+
public nonisolated func changes<Member, Isolation: Actor>(
3434
for keyPath: KeyPath<Self, Member>,
35-
isolation: Delivery
36-
) -> ObservedTransactions<Self, Delivery> {
37-
transactions(for: [keyPath], isolation: isolation)
35+
isolatedTo isolation: Isolation
36+
) -> ObservedChanges<Self, Isolation> {
37+
changes(for: [keyPath], isolatedTo: isolation)
3838
}
3939

40-
public nonisolated func transactions(
40+
public nonisolated func changes(
4141
for properties: TrackedProperties<Self>
42-
) -> ObservedTransactions<Self, MainActor.ActorType> {
43-
transactions(for: properties, isolation: MainActor.shared)
42+
) -> ObservedChanges<Self, MainActor.ActorType> {
43+
changes(for: properties, isolatedTo: MainActor.shared)
4444
}
4545

46-
public nonisolated func transactions<Member>(
46+
public nonisolated func changes<Member>(
4747
for keyPath: KeyPath<Self, Member>
48-
) -> ObservedTransactions<Self, MainActor.ActorType> {
49-
transactions(for: [keyPath], isolation: MainActor.shared)
48+
) -> ObservedChanges<Self, MainActor.ActorType> {
49+
changes(for: [keyPath], isolatedTo: MainActor.shared)
5050
}
5151

5252
public nonisolated static func dependencies(

0 commit comments

Comments
 (0)