Skip to content

Commit 12deb1e

Browse files
authored
Fix CachedEnvironment attribute cache issue (#313)
1 parent bcfa2dc commit 12deb1e

File tree

5 files changed

+34
-9
lines changed

5 files changed

+34
-9
lines changed

Package.resolved

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
2-
"originHash" : "669d1f5a7baa4f6093075d288ac99f536ebb7801497e931b763050191ecbf7d0",
2+
"originHash" : "c6403e6357703cd6a416b17852a4312a975c4f5a164d0170118352abe1be3e2d",
33
"pins" : [
44
{
55
"identity" : "darwinprivateframeworks",
66
"kind" : "remoteSourceControl",
77
"location" : "https://github.com/OpenSwiftUIProject/DarwinPrivateFrameworks.git",
88
"state" : {
99
"branch" : "main",
10-
"revision" : "88f975a6b211a5c9b3f2c26e55079b7d8f40275b"
10+
"revision" : "ad18bd6feaa29e9b138ff7823796c6bc76295fb7"
1111
}
1212
},
1313
{
@@ -25,7 +25,7 @@
2525
"location" : "https://github.com/OpenSwiftUIProject/OpenGraph",
2626
"state" : {
2727
"branch" : "main",
28-
"revision" : "fed93356e5876f1fc1ae452a9a799e481c8ea170"
28+
"revision" : "cc2df5ac75d32f33fb6906b4abfbe18114138c91"
2929
}
3030
},
3131
{

Sources/OpenSwiftUICore/Data/Environment/CachedEnvironment.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ package struct CachedEnvironment {
4646
self.platformCache = PlatformCache()
4747
}
4848

49-
package mutating func attribute<T>(id: CachedEnvironment.ID, _ body: (EnvironmentValues) -> T) -> Attribute<T> {
49+
package mutating func attribute<T>(id: CachedEnvironment.ID, _ body: @escaping (EnvironmentValues) -> T) -> Attribute<T> {
5050
guard let item = mapItems.first(where: { $0.key == id }) else {
51-
// Blocked by OG's Map
52-
// preconditionFailure("TODO")
53-
return Attribute(value: body(environment.value))
51+
let map = Map(environment, body)
52+
let attribute = Attribute(map)
53+
mapItems.append(MapItem(key: id, value: attribute.identifier))
54+
return attribute
5455
}
5556
return item.value.unsafeCast(to: T.self)
5657
}

Sources/OpenSwiftUICore/Graph/GraphInputs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public struct _GraphInputs {
154154
preconditionFailure("TODO")
155155
}
156156

157-
package func mapEnvironment<T>(id: CachedEnvironment.ID, _ body: (EnvironmentValues) -> T) -> Attribute<T> {
157+
package func mapEnvironment<T>(id: CachedEnvironment.ID, _ body: @escaping (EnvironmentValues) -> T) -> Attribute<T> {
158158
cachedEnvironment.wrappedValue.attribute(id: id, body)
159159
}
160160

Sources/OpenSwiftUICore/View/Input/ViewInputs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public struct _ViewInputs {
170170
)
171171
}
172172

173-
package func mapEnvironment<T>(id: CachedEnvironment.ID, _ body: (EnvironmentValues) -> T) -> Attribute<T> {
173+
package func mapEnvironment<T>(id: CachedEnvironment.ID, _ body: @escaping (EnvironmentValues) -> T) -> Attribute<T> {
174174
base.mapEnvironment(id: id, body)
175175
}
176176

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// CachedEnvironmentTests.swift
3+
// OpenSwiftUICoreTests
4+
5+
import OpenGraphShims
6+
@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore
7+
@testable import OpenSwiftUICore
8+
import OpenSwiftUITestsSupport
9+
import Testing
10+
11+
@MainActor
12+
struct CachedEnvironmentTests {
13+
@Test
14+
func attribute() {
15+
let graph = Graph(shared: Graph())
16+
let globalSubgraph = Subgraph(graph: graph)
17+
Subgraph.current = globalSubgraph
18+
defer { Subgraph.current = nil }
19+
var env = CachedEnvironment(.init(value: .init()))
20+
let attribute1 = env.attribute(id: .layoutDirection) { $0.layoutDirection }
21+
let attribute2 = env.attribute(id: .layoutDirection) { $0.layoutDirection }
22+
#expect(attribute1 == attribute2)
23+
}
24+
}

0 commit comments

Comments
 (0)