Skip to content

Commit

Permalink
Allow passing data when subscribing for state updates
Browse files Browse the repository at this point in the history
  • Loading branch information
bgoncal committed Nov 12, 2024
1 parent c7f34a4 commit 4cc137c
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Source/Caches/HACacheKeyStates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import Foundation

/// Key for the cache
internal struct HACacheKeyStates: HACacheKey {
static func create(connection: HAConnection) -> HACache<HACachedStates> {
static func create(connection: HAConnection, data: [String: Any]) -> HACache<HACachedStates> {
.init(
connection: connection,
subscribe:
.init(
subscription: .subscribeEntities(),
subscription: .subscribeEntities(data: data),
transform: { info in
.replace(processUpdates(
info: info,
Expand Down
6 changes: 5 additions & 1 deletion Source/Caches/HACachedStates.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
public extension HACachesContainer {
/// Cache of entity states, see `HACachedStates` for values.
var states: HACache<HACachedStates> { self[HACacheKeyStates.self] }
/// - Parameter data: The data passed to connection request
/// - Returns: The cache object with states
func states(_ data: [String: Any] = [:]) -> HACache<HACachedStates> {
self[HACacheKeyStates.self, data]
}
}

/// Cached version of all entity states
Expand Down
2 changes: 1 addition & 1 deletion Source/Caches/HACachedUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public extension HACachesContainer {

/// Key for the cache
private struct HACacheKeyCurrentUser: HACacheKey {
static func create(connection: HAConnection) -> HACache<HAResponseCurrentUser> {
static func create(connection: HAConnection, data: [String: Any]) -> HACache<HAResponseCurrentUser> {
.init(
connection: connection,
populate: .init(request: .currentUser(), transform: \.incoming),
Expand Down
10 changes: 7 additions & 3 deletions Source/Caches/HACachesContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ public protocol HACacheKey {
/// This is called exactly once per connection per cache key.
///
/// - Parameter connection: The connection to create on
/// - Parameter data: The data passed to connection request
/// - Returns: The cache you want to associate with the key
static func create(connection: HAConnection) -> HACache<Value>
static func create(
connection: HAConnection,
data: [String: Any]
) -> HACache<Value>
}

/// Container for caches
Expand Down Expand Up @@ -56,15 +60,15 @@ public class HACachesContainer {
/// - SeeAlso: `HACachesContainer` class description for how to use keys to retrieve caches.
/// - Subscript: The key to look up
/// - Returns: Either the existing cache for the key, or a new one created on-the-fly if none was available
public subscript<KeyType: HACacheKey>(_ key: KeyType.Type) -> HACache<KeyType.Value> {
public subscript<KeyType: HACacheKey>(_ key: KeyType.Type, data: [String: Any] = [:]) -> HACache<KeyType.Value> {
// ObjectIdentifier is globally unique per class _or_ meta type, and we're using meta type here
let key = ObjectIdentifier(KeyType.self)

if let value = values[key] as? HACache<KeyType.Value> {
return value
}

let value = KeyType.create(connection: connection)
let value = KeyType.create(connection: connection, data: data)
values[key] = value
return value
}
Expand Down
5 changes: 3 additions & 2 deletions Source/Convenience/States.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ public extension HATypedSubscription {
}

/// Listen for compressed state changes of all entities
/// - Parameter data: The data passed to connection request
/// - Returns: A typed subscriptions that can be sent via `HAConnection`
static func subscribeEntities() -> HATypedSubscription<HACompressedStatesUpdates> {
.init(request: .init(type: .subscribeEntities, data: [:]))
static func subscribeEntities(data: [String: Any]) -> HATypedSubscription<HACompressedStatesUpdates> {
.init(request: .init(type: .subscribeEntities, data: data))
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/HACachedStates.test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class HACachedStatesTests: XCTestCase {
}

func testPopulateAndSubscribeInfo() throws {
let cache = container.states
let cache = container.states()
XCTAssertNil(cache.populateInfo)
XCTAssertNil(cache.subscribeInfo)
let subscribeOnlyInfo = try XCTUnwrap(cache.subscribeOnlyInfo)
Expand Down
4 changes: 2 additions & 2 deletions Tests/HACachesContainer.test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal class HACachesContainerTests: XCTestCase {
}

private struct Key1: HACacheKey {
static func create(connection: HAConnection) -> HACache<Value1> {
static func create(connection: HAConnection, data: [String: Any]) -> HACache<Value1> {
.init(
connection: connection,
populate: HACachePopulateInfo<Value1>(
Expand All @@ -61,7 +61,7 @@ private struct Value1: HADataDecodable {
}

private struct Key2: HACacheKey {
static func create(connection: HAConnection) -> HACache<Value1> {
static func create(connection: HAConnection, data: [String: Any]) -> HACache<Value1> {
.init(
connection: connection,
populate: HACachePopulateInfo<Value1>(
Expand Down
2 changes: 1 addition & 1 deletion Tests/States.test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ internal class StatesTests: XCTestCase {
}

func testSubscribeEntitiesRequest() throws {
let request = HATypedSubscription<HACompressedStatesUpdates>.subscribeEntities()
let request = HATypedSubscription<HACompressedStatesUpdates>.subscribeEntities(data: [:])
XCTAssertEqual(request.request.type, .subscribeEntities)
}
}

0 comments on commit 4cc137c

Please sign in to comment.