Skip to content

Commit e4d1404

Browse files
authored
Apply inlinabble to all methods of AtomTestContext (#84)
1 parent 13efd80 commit e4d1404

File tree

5 files changed

+63
-23
lines changed

5 files changed

+63
-23
lines changed

Sources/Atoms/Context/AtomTestContext.swift

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@ import Foundation
99
/// by this itself.
1010
@MainActor
1111
public struct AtomTestContext: AtomWatchableContext {
12-
private let state = State()
1312
private let location: SourceLocation
1413

14+
@usableFromInline
15+
internal let _state = State()
16+
1517
/// Creates a new test context instance with fresh internal state.
1618
public init(fileID: String = #fileID, line: UInt = #line) {
1719
location = SourceLocation(fileID: fileID, line: line)
1820
}
1921

2022
/// A callback to perform when any of atoms watched by this context is updated.
23+
@inlinable
2124
public var onUpdate: (() -> Void)? {
22-
get { state.onUpdate }
23-
nonmutating set { state.onUpdate = newValue }
25+
get { _state.onUpdate }
26+
nonmutating set { _state.onUpdate = newValue }
2427
}
2528

2629
/// Waits until any of the atoms watched through this context have been updated up to the
@@ -44,10 +47,11 @@ public struct AtomTestContext: AtomWatchableContext {
4447
/// - Parameter duration: The maximum duration that this function can wait until
4548
/// the next update. The default timeout interval is nil.
4649
/// - Returns: A boolean value indicating whether an update is done.
50+
@inlinable
4751
@discardableResult
4852
public func waitForUpdate(timeout duration: TimeInterval? = nil) async -> Bool {
4953
await withTaskGroup(of: Bool.self) { group in
50-
let updates = state.makeUpdateStream()
54+
let updates = _state.makeUpdateStream()
5155

5256
group.addTask { @MainActor in
5357
var iterator = updates.makeAsyncIterator()
@@ -95,6 +99,7 @@ public struct AtomTestContext: AtomWatchableContext {
9599
///
96100
/// - Returns: A boolean value indicating whether an update is done.
97101
///
102+
@inlinable
98103
@discardableResult
99104
public func wait<Node: Atom>(
100105
for atom: Node,
@@ -111,7 +116,7 @@ public struct AtomTestContext: AtomWatchableContext {
111116
return predicate(value)
112117
}
113118

114-
let updates = state.makeUpdateStream()
119+
let updates = _state.makeUpdateStream()
115120

116121
group.addTask { @MainActor in
117122
guard !check() else {
@@ -155,8 +160,9 @@ public struct AtomTestContext: AtomWatchableContext {
155160
/// - Parameter atom: An atom that associates the value.
156161
///
157162
/// - Returns: The value associated with the given atom.
163+
@inlinable
158164
public func read<Node: Atom>(_ atom: Node) -> Node.Loader.Value {
159-
store.read(atom)
165+
_store.read(atom)
160166
}
161167

162168
/// Sets the new value for the given writable atom.
@@ -177,8 +183,9 @@ public struct AtomTestContext: AtomWatchableContext {
177183
/// - Parameters
178184
/// - value: A value to be set.
179185
/// - atom: An atom that associates the value.
186+
@inlinable
180187
public func set<Node: StateAtom>(_ value: Node.Loader.Value, for atom: Node) {
181-
store.set(value, for: atom)
188+
_store.set(value, for: atom)
182189
}
183190

184191
/// Modifies the cached value of the given writable atom.
@@ -200,8 +207,9 @@ public struct AtomTestContext: AtomWatchableContext {
200207
/// - Parameters
201208
/// - atom: An atom that associates the value.
202209
/// - body: A value modification body.
210+
@inlinable
203211
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)
205213
}
206214

207215
/// Refreshes and then return the value associated with the given refreshable atom.
@@ -221,9 +229,10 @@ public struct AtomTestContext: AtomWatchableContext {
221229
/// - Parameter atom: An atom that associates the value.
222230
///
223231
/// - Returns: The value which completed refreshing associated with the given atom.
232+
@inlinable
224233
@discardableResult
225234
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)
227236
}
228237

229238
/// Resets the value associated with the given atom, and then notify.
@@ -242,8 +251,9 @@ public struct AtomTestContext: AtomWatchableContext {
242251
/// ```
243252
///
244253
/// - Parameter atom: An atom that associates the value.
254+
@inlinable
245255
public func reset(_ atom: some Atom) {
246-
store.reset(atom)
256+
_store.reset(atom)
247257
}
248258

249259
/// Accesses the value associated with the given atom for reading and initialing watch to
@@ -263,10 +273,11 @@ public struct AtomTestContext: AtomWatchableContext {
263273
/// - Parameter atom: An atom that associates the value.
264274
///
265275
/// - Returns: The value associated with the given atom.
276+
@inlinable
266277
@discardableResult
267278
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()
270281
}
271282
}
272283

@@ -285,17 +296,19 @@ public struct AtomTestContext: AtomWatchableContext {
285296
/// - Parameter atom: An atom that associates the value.
286297
///
287298
/// - Returns: The already cached value associated with the given atom.
299+
@inlinable
288300
public func lookup<Node: Atom>(_ atom: Node) -> Node.Loader.Value? {
289-
store.lookup(atom)
301+
_store.lookup(atom)
290302
}
291303

292304
/// Unwatches the given atom and do not receive any more updates of it.
293305
///
294306
/// It simulates cases where other atoms or views no longer watches to the atom.
295307
///
296308
/// - Parameter atom: An atom that associates the value.
309+
@inlinable
297310
public func unwatch(_ atom: some Atom) {
298-
store.unwatch(atom, container: container)
311+
_store.unwatch(atom, container: _container)
299312
}
300313

301314
/// Overrides the atom value with the given value.
@@ -306,8 +319,9 @@ public struct AtomTestContext: AtomWatchableContext {
306319
/// - Parameters:
307320
/// - atom: An atom that to be overridden.
308321
/// - value: A value that to be used instead of the atom's value.
322+
@inlinable
309323
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)
311325
}
312326

313327
/// Overrides the atom value with the given value.
@@ -320,22 +334,30 @@ public struct AtomTestContext: AtomWatchableContext {
320334
/// - Parameters:
321335
/// - atomType: An atom type that to be overridden.
322336
/// - value: A value that to be used instead of the atom's value.
337+
@inlinable
323338
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)
325340
}
326341
}
327342

328-
private extension AtomTestContext {
343+
internal extension AtomTestContext {
344+
@usableFromInline
329345
@MainActor
330346
final class State {
347+
@usableFromInline
331348
let store = AtomStore()
332349
let token = ScopeKey.Token()
333350
let container = SubscriptionContainer()
351+
352+
@usableFromInline
334353
var overrides = [OverrideKey: any AtomOverrideProtocol]()
354+
355+
@usableFromInline
335356
var onUpdate: (() -> Void)?
336357

337358
private let notifier = PassthroughSubject<Void, Never>()
338359

360+
@usableFromInline
339361
func makeUpdateStream() -> AsyncStream<Void> {
340362
AsyncStream { continuation in
341363
let cancellable = notifier.sink(
@@ -362,22 +384,25 @@ private extension AtomTestContext {
362384
}
363385
}
364386

387+
@usableFromInline
365388
func notifyUpdate() {
366389
onUpdate?()
367390
notifier.send()
368391
}
369392
}
370393

371-
var store: StoreContext {
394+
@usableFromInline
395+
var _store: StoreContext {
372396
.scoped(
373-
key: ScopeKey(token: state.token),
374-
store: state.store,
397+
key: ScopeKey(token: _state.token),
398+
store: _state.store,
375399
observers: [],
376-
overrides: state.overrides
400+
overrides: _state.overrides
377401
)
378402
}
379403

380-
var container: SubscriptionContainer.Wrapper {
381-
state.container.wrapper(location: location)
404+
@usableFromInline
405+
var _container: SubscriptionContainer.Wrapper {
406+
_state.container.wrapper(location: location)
382407
}
383408
}

Sources/Atoms/Core/AtomOverride.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@usableFromInline
12
internal protocol AtomOverrideProtocol {
23
associatedtype Node: Atom
34

@@ -6,9 +7,17 @@ internal protocol AtomOverrideProtocol {
67
func scoped(key: ScopeKey) -> any AtomScopedOverrideProtocol
78
}
89

10+
@usableFromInline
911
internal struct AtomOverride<Node: Atom>: AtomOverrideProtocol {
12+
@usableFromInline
1013
let value: (Node) -> Node.Loader.Value
1114

15+
@usableFromInline
16+
init(value: @escaping (Node) -> Node.Loader.Value) {
17+
self.value = value
18+
}
19+
20+
@usableFromInline
1221
func scoped(key: ScopeKey) -> any AtomScopedOverrideProtocol {
1322
AtomScopedOverride<Node>(scopeKey: key, value: value)
1423
}
@@ -20,6 +29,7 @@ internal struct AtomOverride<Node: Atom>: AtomOverrideProtocol {
2029
// their View body is evaluated. This is not ideal from a performance standpoint,
2130
// so it will be improved as soon as an alternative way to grant per-scope keys
2231
// independent of the SwiftUI lifecycle is came up.
32+
@usableFromInline
2333
internal protocol AtomScopedOverrideProtocol {
2434
var scopeKey: ScopeKey { get }
2535
}

Sources/Atoms/Core/OverrideKey.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
@usableFromInline
12
internal struct OverrideKey: Hashable {
23
private let identifier: Identifier
34

5+
@usableFromInline
46
init<Node: Atom>(_ atom: Node) {
57
let key = AnyHashable(atom.key)
68
let type = ObjectIdentifier(Node.self)
79
identifier = .node(key: key, type: type)
810
}
911

12+
@usableFromInline
1013
init<Node: Atom>(_: Node.Type) {
1114
let type = ObjectIdentifier(Node.self)
1215
identifier = .type(type)

Sources/Atoms/Core/ScopeKey.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@usableFromInline
12
internal struct ScopeKey: Hashable {
23
final class Token {}
34

Sources/Atoms/Core/TaskExtensions.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
internal extension Task where Success == Never, Failure == Never {
2+
@inlinable
23
static func sleep(seconds duration: Double) async throws {
34
try await sleep(nanoseconds: UInt64(duration * 1_000_000_000))
45
}

0 commit comments

Comments
 (0)