@@ -9,18 +9,21 @@ import Foundation
9
9
/// by this itself.
10
10
@MainActor
11
11
public struct AtomTestContext : AtomWatchableContext {
12
- private let state = State ( )
13
12
private let location : SourceLocation
14
13
14
+ @usableFromInline
15
+ internal let _state = State ( )
16
+
15
17
/// Creates a new test context instance with fresh internal state.
16
18
public init ( fileID: String = #fileID, line: UInt = #line) {
17
19
location = SourceLocation ( fileID: fileID, line: line)
18
20
}
19
21
20
22
/// A callback to perform when any of atoms watched by this context is updated.
23
+ @inlinable
21
24
public var onUpdate : ( ( ) -> Void ) ? {
22
- get { state . onUpdate }
23
- nonmutating set { state . onUpdate = newValue }
25
+ get { _state . onUpdate }
26
+ nonmutating set { _state . onUpdate = newValue }
24
27
}
25
28
26
29
/// Waits until any of the atoms watched through this context have been updated up to the
@@ -44,10 +47,11 @@ public struct AtomTestContext: AtomWatchableContext {
44
47
/// - Parameter duration: The maximum duration that this function can wait until
45
48
/// the next update. The default timeout interval is nil.
46
49
/// - Returns: A boolean value indicating whether an update is done.
50
+ @inlinable
47
51
@discardableResult
48
52
public func waitForUpdate( timeout duration: TimeInterval ? = nil ) async -> Bool {
49
53
await withTaskGroup ( of: Bool . self) { group in
50
- let updates = state . makeUpdateStream ( )
54
+ let updates = _state . makeUpdateStream ( )
51
55
52
56
group. addTask { @MainActor in
53
57
var iterator = updates. makeAsyncIterator ( )
@@ -95,6 +99,7 @@ public struct AtomTestContext: AtomWatchableContext {
95
99
///
96
100
/// - Returns: A boolean value indicating whether an update is done.
97
101
///
102
+ @inlinable
98
103
@discardableResult
99
104
public func wait< Node: Atom > (
100
105
for atom: Node ,
@@ -111,7 +116,7 @@ public struct AtomTestContext: AtomWatchableContext {
111
116
return predicate ( value)
112
117
}
113
118
114
- let updates = state . makeUpdateStream ( )
119
+ let updates = _state . makeUpdateStream ( )
115
120
116
121
group. addTask { @MainActor in
117
122
guard !check( ) else {
@@ -155,8 +160,9 @@ public struct AtomTestContext: AtomWatchableContext {
155
160
/// - Parameter atom: An atom that associates the value.
156
161
///
157
162
/// - Returns: The value associated with the given atom.
163
+ @inlinable
158
164
public func read< Node: Atom > ( _ atom: Node ) -> Node . Loader . Value {
159
- store . read ( atom)
165
+ _store . read ( atom)
160
166
}
161
167
162
168
/// Sets the new value for the given writable atom.
@@ -177,8 +183,9 @@ public struct AtomTestContext: AtomWatchableContext {
177
183
/// - Parameters
178
184
/// - value: A value to be set.
179
185
/// - atom: An atom that associates the value.
186
+ @inlinable
180
187
public func set< Node: StateAtom > ( _ value: Node . Loader . Value , for atom: Node ) {
181
- store . set ( value, for: atom)
188
+ _store . set ( value, for: atom)
182
189
}
183
190
184
191
/// Modifies the cached value of the given writable atom.
@@ -200,8 +207,9 @@ public struct AtomTestContext: AtomWatchableContext {
200
207
/// - Parameters
201
208
/// - atom: An atom that associates the value.
202
209
/// - body: A value modification body.
210
+ @inlinable
203
211
public func modify< Node: StateAtom > ( _ atom: Node , body: ( inout Node . Loader . Value ) -> Void ) {
204
- store . modify ( atom, body: body)
212
+ _store . modify ( atom, body: body)
205
213
}
206
214
207
215
/// Refreshes and then return the value associated with the given refreshable atom.
@@ -221,9 +229,10 @@ public struct AtomTestContext: AtomWatchableContext {
221
229
/// - Parameter atom: An atom that associates the value.
222
230
///
223
231
/// - Returns: The value which completed refreshing associated with the given atom.
232
+ @inlinable
224
233
@discardableResult
225
234
public func refresh< Node: Atom > ( _ atom: Node ) async -> Node . Loader . Value where Node. Loader: RefreshableAtomLoader {
226
- await store . refresh ( atom)
235
+ await _store . refresh ( atom)
227
236
}
228
237
229
238
/// Resets the value associated with the given atom, and then notify.
@@ -242,8 +251,9 @@ public struct AtomTestContext: AtomWatchableContext {
242
251
/// ```
243
252
///
244
253
/// - Parameter atom: An atom that associates the value.
254
+ @inlinable
245
255
public func reset( _ atom: some Atom ) {
246
- store . reset ( atom)
256
+ _store . reset ( atom)
247
257
}
248
258
249
259
/// Accesses the value associated with the given atom for reading and initialing watch to
@@ -263,10 +273,11 @@ public struct AtomTestContext: AtomWatchableContext {
263
273
/// - Parameter atom: An atom that associates the value.
264
274
///
265
275
/// - Returns: The value associated with the given atom.
276
+ @inlinable
266
277
@discardableResult
267
278
public func watch< Node: Atom > ( _ atom: Node ) -> Node . Loader . Value {
268
- store . watch ( atom, container: container , requiresObjectUpdate: true ) { [ weak state ] in
269
- state ? . notifyUpdate ( )
279
+ _store . watch ( atom, container: _container , requiresObjectUpdate: true ) { [ weak _state ] in
280
+ _state ? . notifyUpdate ( )
270
281
}
271
282
}
272
283
@@ -285,17 +296,19 @@ public struct AtomTestContext: AtomWatchableContext {
285
296
/// - Parameter atom: An atom that associates the value.
286
297
///
287
298
/// - Returns: The already cached value associated with the given atom.
299
+ @inlinable
288
300
public func lookup< Node: Atom > ( _ atom: Node ) -> Node . Loader . Value ? {
289
- store . lookup ( atom)
301
+ _store . lookup ( atom)
290
302
}
291
303
292
304
/// Unwatches the given atom and do not receive any more updates of it.
293
305
///
294
306
/// It simulates cases where other atoms or views no longer watches to the atom.
295
307
///
296
308
/// - Parameter atom: An atom that associates the value.
309
+ @inlinable
297
310
public func unwatch( _ atom: some Atom ) {
298
- store . unwatch ( atom, container: container )
311
+ _store . unwatch ( atom, container: _container )
299
312
}
300
313
301
314
/// Overrides the atom value with the given value.
@@ -306,8 +319,9 @@ public struct AtomTestContext: AtomWatchableContext {
306
319
/// - Parameters:
307
320
/// - atom: An atom that to be overridden.
308
321
/// - value: A value that to be used instead of the atom's value.
322
+ @inlinable
309
323
public func override< Node: Atom > ( _ atom: Node , with value: @escaping ( Node ) -> Node . Loader . Value ) {
310
- state . overrides [ OverrideKey ( atom) ] = AtomOverride ( value: value)
324
+ _state . overrides [ OverrideKey ( atom) ] = AtomOverride ( value: value)
311
325
}
312
326
313
327
/// Overrides the atom value with the given value.
@@ -320,22 +334,30 @@ public struct AtomTestContext: AtomWatchableContext {
320
334
/// - Parameters:
321
335
/// - atomType: An atom type that to be overridden.
322
336
/// - value: A value that to be used instead of the atom's value.
337
+ @inlinable
323
338
public func override< Node: Atom > ( _ atomType: Node . Type , with value: @escaping ( Node ) -> Node . Loader . Value ) {
324
- state . overrides [ OverrideKey ( atomType) ] = AtomOverride ( value: value)
339
+ _state . overrides [ OverrideKey ( atomType) ] = AtomOverride ( value: value)
325
340
}
326
341
}
327
342
328
- private extension AtomTestContext {
343
+ internal extension AtomTestContext {
344
+ @usableFromInline
329
345
@MainActor
330
346
final class State {
347
+ @usableFromInline
331
348
let store = AtomStore ( )
332
349
let token = ScopeKey . Token ( )
333
350
let container = SubscriptionContainer ( )
351
+
352
+ @usableFromInline
334
353
var overrides = [ OverrideKey : any AtomOverrideProtocol ] ( )
354
+
355
+ @usableFromInline
335
356
var onUpdate : ( ( ) -> Void ) ?
336
357
337
358
private let notifier = PassthroughSubject < Void , Never > ( )
338
359
360
+ @usableFromInline
339
361
func makeUpdateStream( ) -> AsyncStream < Void > {
340
362
AsyncStream { continuation in
341
363
let cancellable = notifier. sink (
@@ -362,22 +384,25 @@ private extension AtomTestContext {
362
384
}
363
385
}
364
386
387
+ @usableFromInline
365
388
func notifyUpdate( ) {
366
389
onUpdate ? ( )
367
390
notifier. send ( )
368
391
}
369
392
}
370
393
371
- var store : StoreContext {
394
+ @usableFromInline
395
+ var _store : StoreContext {
372
396
. scoped(
373
- key: ScopeKey ( token: state . token) ,
374
- store: state . store,
397
+ key: ScopeKey ( token: _state . token) ,
398
+ store: _state . store,
375
399
observers: [ ] ,
376
- overrides: state . overrides
400
+ overrides: _state . overrides
377
401
)
378
402
}
379
403
380
- var container : SubscriptionContainer . Wrapper {
381
- state. container. wrapper ( location: location)
404
+ @usableFromInline
405
+ var _container : SubscriptionContainer . Wrapper {
406
+ _state. container. wrapper ( location: location)
382
407
}
383
408
}
0 commit comments