Skip to content

[Observation] Refactor for state machine (and behavior robustness) and adjust protocol requirement names #64414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 17, 2023
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
19 changes: 12 additions & 7 deletions lib/Macros/Sources/ObservationMacros/ObservableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,22 @@ public struct ObservableMacro: MemberMacro, MemberAttributeMacro, ConformanceMac
let _registrar = ObservationRegistrar<\(parentName)>()
"""

let transactions: DeclSyntax =
let changes: DeclSyntax =
"""
public nonisolated func transactions<Delivery>(for properties: TrackedProperties<\(parentName)>, isolation: Delivery) -> ObservedTransactions<\(parentName), Delivery> where Delivery: Actor {
_registrar.transactions(for: properties, isolation: isolation)
public nonisolated func changes<Isolation: Actor>(
for properties: TrackedProperties<\(parentName)>,
isolatedTo isolation: Isolation
) -> ObservedChanges<\(parentName), Isolation> {
_registrar.changes(for: properties, isolatedTo: isolation)
}
"""

let changes: DeclSyntax =
let values: DeclSyntax =
"""
public nonisolated func changes<Member>(for keyPath: KeyPath<\(parentName), Member>) -> ObservedChanges<\(parentName), Member> where Member: Sendable {
_registrar.changes(for: keyPath)
public nonisolated func values<Member: Sendable>(
for keyPath: KeyPath<\(parentName), Member>
) -> ObservedValues<\(parentName), Member> {
_registrar.values(for: keyPath)
}
"""

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

return [
registrar,
transactions,
changes,
values,
storageStruct,
storage,
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@

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

add_swift_target_library(swiftObservation ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
add_swift_target_library(swift_Observation ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
Locking.cpp
Locking.swift
Macros.swift
Observable.swift
ObservationRegistrar.swift
ObservationRegistrarStateMachine.swift
ObservationTracking.swift
ObservedChanges.swift
ObservedTransactions.swift
ObservedValues.swift
ThreadLocal.cpp
ThreadLocal.swift
TrackedProperties.swift
Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/Observation/Sources/Observation/Locking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

using namespace swift;

extern "C" SWIFT_CC(swift)
extern "C" SWIFT_CC(swift) __attribute__((visibility("hidden")))
size_t _swift_observation_lock_size() {
size_t bytes = sizeof(Mutex);

Expand All @@ -26,17 +26,17 @@ size_t _swift_observation_lock_size() {
return bytes;
}

extern "C" SWIFT_CC(swift)
extern "C" SWIFT_CC(swift) __attribute__((visibility("hidden")))
void _swift_observation_lock_init(Mutex &lock) {
new (&lock) Mutex();
}

extern "C" SWIFT_CC(swift)
extern "C" SWIFT_CC(swift) __attribute__((visibility("hidden")))
void _swift_observation_lock_lock(Mutex &lock) {
lock.lock();
}

extern "C" SWIFT_CC(swift)
extern "C" SWIFT_CC(swift) __attribute__((visibility("hidden")))
void _swift_observation_lock_unlock(Mutex &lock) {
lock.unlock();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#if $Macros && hasAttribute(attached)

@available(SwiftStdlib 5.9, *)
@attached(member, names: named(_registrar), named(transactions), named(changes), named(_Storage), named(_storage))
@attached(member, names: named(_registrar), named(changes), named(values), named(_Storage), named(_storage))
@attached(memberAttribute)
@attached(conformance)
public macro Observable() =
Expand Down
30 changes: 15 additions & 15 deletions stdlib/public/Observation/Sources/Observation/Observable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import _Concurrency

@available(SwiftStdlib 5.9, *)
public protocol Observable {
nonisolated func transactions<Delivery: Actor>(
nonisolated func changes<Isolation: Actor>(
for properties: TrackedProperties<Self>,
isolation: Delivery
) -> ObservedTransactions<Self, Delivery>
isolatedTo isolation: Isolation
) -> ObservedChanges<Self, Isolation>

nonisolated func changes<Member: Sendable>(
nonisolated func values<Member: Sendable>(
for keyPath: KeyPath<Self, Member>
) -> ObservedChanges<Self, Member>
) -> ObservedValues<Self, Member>

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

@available(SwiftStdlib 5.9, *)
extension Observable {
public nonisolated func transactions<Member, Delivery: Actor>(
public nonisolated func changes<Member, Isolation: Actor>(
for keyPath: KeyPath<Self, Member>,
isolation: Delivery
) -> ObservedTransactions<Self, Delivery> {
transactions(for: [keyPath], isolation: isolation)
isolatedTo isolation: Isolation
) -> ObservedChanges<Self, Isolation> {
changes(for: [keyPath], isolatedTo: isolation)
}

public nonisolated func transactions(
public nonisolated func changes(
for properties: TrackedProperties<Self>
) -> ObservedTransactions<Self, MainActor.ActorType> {
transactions(for: properties, isolation: MainActor.shared)
) -> ObservedChanges<Self, MainActor.ActorType> {
changes(for: properties, isolatedTo: MainActor.shared)
}

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

public nonisolated static func dependencies(
Expand Down
Loading