Skip to content

Commit 85cf539

Browse files
authored
Handle KeepAlive as a marker protocol (#50)
1 parent 58225e6 commit 85cf539

File tree

6 files changed

+4
-65
lines changed

6 files changed

+4
-65
lines changed

Sources/Atoms/Atom/Atom.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@ public protocol Atom {
2323
/// with other atoms.
2424
typealias UpdatedContext = AtomUpdatedContext
2525

26-
/// A boolean value indicating whether the atom value should be preserved even if
27-
/// no longer watched to.
28-
///
29-
/// It's recommended to conform the ``KeepAlive`` to this atom, instead of overriding
30-
/// this property to return `true`.
31-
/// The default is `false`.
32-
static var shouldKeepAlive: Bool { get }
33-
3426
/// A unique value used to identify the atom internally.
3527
///
3628
/// This key don't have to be unique with respect to other atoms in the entire application
@@ -70,10 +62,6 @@ public protocol Atom {
7062
}
7163

7264
public extension Atom {
73-
static var shouldKeepAlive: Bool {
74-
false
75-
}
76-
7765
func makeCoordinator() -> Coordinator where Coordinator == Void {
7866
()
7967
}

Sources/Atoms/Core/AtomCache.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ internal protocol AtomCacheProtocol: CustomStringConvertible {
44

55
var atom: Node { get set }
66
var value: Node.Loader.Value? { get set }
7-
var shouldKeepAlive: Bool { get }
87

98
func reset(with store: StoreContext)
109
}
@@ -19,10 +18,6 @@ internal struct AtomCache<Node: Atom>: AtomCacheProtocol {
1918
var atom: Node
2019
var value: Node.Loader.Value?
2120

22-
var shouldKeepAlive: Bool {
23-
Node.shouldKeepAlive
24-
}
25-
2621
func reset(with store: StoreContext) {
2722
store.reset(atom)
2823
}

Sources/Atoms/Core/StoreContext.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,10 @@ private extension StoreContext {
423423
// 1. It's not marked as `KeepAlive`.
424424
// 2. It has no downstream atoms.
425425
// 3. It has no subscriptions from views.
426-
let shouldKeepAlive = store.state.caches[key]?.shouldKeepAlive ?? false
427-
let shouldRelease =
428-
!shouldKeepAlive
429-
&& (store.graph.children[key]?.isEmpty ?? true)
430-
&& (store.state.subscriptions[key]?.isEmpty ?? true)
426+
let shouldKeepAlive = store.state.caches[key].map { $0 is any KeepAlive } ?? false
427+
let isChildrenEmpty = store.graph.children[key]?.isEmpty ?? true
428+
let isSubscriptionEmpty = store.state.subscriptions[key]?.isEmpty ?? true
429+
let shouldRelease = !shouldKeepAlive && isChildrenEmpty && isSubscriptionEmpty
431430

432431
guard shouldRelease else {
433432
return

Sources/Atoms/KeepAlive.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,3 @@
1212
/// ```
1313
///
1414
public protocol KeepAlive where Self: Atom {}
15-
16-
public extension KeepAlive {
17-
static var shouldKeepAlive: Bool {
18-
true
19-
}
20-
}

Tests/AtomsTests/Core/AtomCacheTests.swift

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,6 @@ import XCTest
44

55
@MainActor
66
final class AtomCacheTests: XCTestCase {
7-
func testShouldKeepAlive() {
8-
struct KeepAliveAtom: ValueAtom, KeepAlive, Hashable {
9-
func value(context: Context) -> Int {
10-
0
11-
}
12-
}
13-
14-
let cache0 = AtomCache(atom: TestValueAtom(value: 0))
15-
let cache1 = AtomCache(atom: KeepAliveAtom())
16-
17-
XCTAssertFalse(cache0.shouldKeepAlive)
18-
XCTAssertTrue(cache1.shouldKeepAlive)
19-
}
20-
217
func testReset() {
228
let store = AtomStore()
239
let context = StoreContext(store)

Tests/AtomsTests/KeepAliveTests.swift

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)