Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoncal committed Feb 28, 2024
1 parent 335db2d commit ebcc786
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
18 changes: 14 additions & 4 deletions Source/Caches/HACacheKeyStates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ internal struct HACacheKeyStates: HACacheKey {
.init(
subscription: .subscribeEntities(),
transform: { info in
.replace(processUpdates(info: info))
.replace(processUpdates(
info: info,
shouldResetEntities: info.operationType == .secondary
))
}
)
)
Expand All @@ -19,10 +22,17 @@ internal struct HACacheKeyStates: HACacheKey {
/// - Parameter info: The compressed state update and the current cached states
/// - Returns: HAEntity cached states
/// Logic from: https://github.com/home-assistant/home-assistant-js-websocket/blob/master/lib/entities.ts
static func processUpdates(info: HACacheTransformInfo<HACompressedStatesUpdates, HACachedStates?>)
static func processUpdates(
info: HACacheTransformInfo<HACompressedStatesUpdates, HACachedStates?>,
shouldResetEntities: Bool
)
-> HACachedStates {
var states: HACachedStates = info.current ?? .init(entities: [])

var states: HACachedStates
if shouldResetEntities {
states = .init(entities: [])

Check warning on line 32 in Source/Caches/HACacheKeyStates.swift

View check run for this annotation

Codecov / codecov/patch

Source/Caches/HACacheKeyStates.swift#L32

Added line #L32 was not covered by tests
} else {
states = info.current ?? .init(entities: [])
}
if let additions = info.incoming.add {
for (entityId, updates) in additions {
if var currentState = states[entityId] {
Expand Down
11 changes: 9 additions & 2 deletions Source/Caches/HACacheSubscribeInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,16 @@ public struct HACacheSubscribeInfo<OutgoingType> {

return transform(value)
}, start: { connection, perform in
connection.subscribe(to: nonRetrySubscription, handler: { _, incoming in
var operationType: HACacheTransformInfoOperationType = .primary
return connection.subscribe(to: nonRetrySubscription, handler: { _, incoming in
perform { current in
transform(.init(incoming: incoming, current: current))
let transform = transform(.init(
incoming: incoming,
current: current,
operationType: operationType
))
operationType = .secondary
return transform
}
})
}
Expand Down
7 changes: 7 additions & 0 deletions Source/Caches/HACacheTransformInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ public struct HACacheTransformInfo<IncomingType, OutgoingType> {
/// For populate transforms, this is nil if an initial request hasn't been sent yet and the cache not reset.
/// For subscribe transforms, this is nil if the populate did not produce results (or does not exist).
public var current: OutgoingType

public var operationType: HACacheTransformInfoOperationType = .primary

Check failure on line 13 in Source/Caches/HACacheTransformInfo.swift

View workflow job for this annotation

GitHub Actions / lint

public declarations should be documented (missing_docs)
}

public enum HACacheTransformInfoOperationType {

Check failure on line 16 in Source/Caches/HACacheTransformInfo.swift

View workflow job for this annotation

GitHub Actions / lint

public declarations should be documented (missing_docs)
case primary

Check failure on line 17 in Source/Caches/HACacheTransformInfo.swift

View workflow job for this annotation

GitHub Actions / lint

public declarations should be documented (missing_docs)
case secondary

Check failure on line 18 in Source/Caches/HACacheTransformInfo.swift

View workflow job for this annotation

GitHub Actions / lint

public declarations should be documented (missing_docs)
}
8 changes: 4 additions & 4 deletions Tests/HACacheKeyStates.test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal final class HACacheKeyStates_test: XCTestCase {
current: .init(
entities: []
)
)
), shouldResetEntities: false
)

XCTAssertEqual(result.all.count, 2)
Expand Down Expand Up @@ -137,7 +137,7 @@ internal final class HACacheKeyStates_test: XCTestCase {
existentEntity,
]
)
)
), shouldResetEntities: false
)

XCTAssertEqual(result.all.count, 1)
Expand Down Expand Up @@ -206,7 +206,7 @@ internal final class HACacheKeyStates_test: XCTestCase {
existentEntity,
]
)
)
), shouldResetEntities: false
)

XCTAssertEqual(result.all.count, 1)
Expand Down Expand Up @@ -254,7 +254,7 @@ internal final class HACacheKeyStates_test: XCTestCase {
current: .init(
entities: []
)
)
), shouldResetEntities: false
)

XCTAssertEqual(result.all.count, 0)
Expand Down

0 comments on commit ebcc786

Please sign in to comment.