Skip to content

Commit 2298167

Browse files
authored
Avoid extra MainActor hop from Shared[Reader].load(_:) (#154)
1 parent ca94b3a commit 2298167

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

Sources/Sharing/SharedKey.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,15 @@ extension Shared {
8888
/// - Parameter key: A shared key associated with the shared reference. It is responsible for
8989
/// loading and saving the shared reference's value from some external source.
9090
public func load(_ key: some SharedKey<Value>) async throws {
91-
await MainActor.run {
92-
@Dependency(PersistentReferences.self) var persistentReferences
93-
SharedPublisherLocals.$isLoading.withValue(true) {
94-
projectedValue = Shared(
95-
reference: persistentReferences.value(
96-
forKey: key,
97-
default: wrappedValue,
98-
skipInitialLoad: true
99-
)
91+
@Dependency(PersistentReferences.self) var persistentReferences
92+
SharedPublisherLocals.$isLoading.withValue(true) {
93+
projectedValue = Shared(
94+
reference: persistentReferences.value(
95+
forKey: key,
96+
default: wrappedValue,
97+
skipInitialLoad: true
10098
)
101-
}
99+
)
102100
}
103101
try await load()
104102
}

Sources/Sharing/SharedReaderKey.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,15 @@ extension SharedReader {
154154
/// - Parameter key: A shared key associated with the shared reference. It is responsible for
155155
/// loading the shared reference's value from some external source.
156156
public func load(_ key: some SharedReaderKey<Value>) async throws {
157-
await MainActor.run {
158-
@Dependency(PersistentReferences.self) var persistentReferences
159-
SharedPublisherLocals.$isLoading.withValue(true) {
160-
projectedValue = SharedReader(
161-
reference: persistentReferences.value(
162-
forKey: key,
163-
default: wrappedValue,
164-
skipInitialLoad: true
165-
)
157+
@Dependency(PersistentReferences.self) var persistentReferences
158+
SharedPublisherLocals.$isLoading.withValue(true) {
159+
projectedValue = SharedReader(
160+
reference: persistentReferences.value(
161+
forKey: key,
162+
default: wrappedValue,
163+
skipInitialLoad: true
166164
)
167-
}
165+
)
168166
}
169167
try await load()
170168
}

0 commit comments

Comments
 (0)