-
Notifications
You must be signed in to change notification settings - Fork 1
Feature: keep feed own capabilities up to date #51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
86b46d5
Update ownCapabilities in models by caching them locally
laevandus cc47e8b
Add tests for caching capabilities, enriching events, delayed fetch
laevandus 837d035
Merge branch 'develop' into feature/update-feed-own-capabilities
laevandus dc7c436
Add more docs to state handling
laevandus c56d2d4
Add CHANGELOG entry
laevandus ee10ed8
Add matched API response support for capabilities tests
laevandus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
Sources/StreamFeeds/Extensions/Dictionary+Extensions.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // | ||
| // Copyright © 2025 Stream.io Inc. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| extension Dictionary { | ||
| func contains(_ key: Key?) -> Bool { | ||
| guard let key else { return false } | ||
| return self[key] != nil | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
Sources/StreamFeeds/Repositories/OwnCapabilitiesRepository.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // | ||
| // Copyright © 2025 Stream.io Inc. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| /// This is a repository which holds a shared state and manages | ||
| /// a map of feed id to capabilities. | ||
| final class OwnCapabilitiesRepository: Sendable { | ||
| private let apiClient: DefaultAPI | ||
| private let storage = AllocatedUnfairLock([FeedId: Set<FeedOwnCapability>]()) | ||
|
|
||
| init(apiClient: DefaultAPI) { | ||
| self.apiClient = apiClient | ||
| } | ||
|
|
||
| // MARK: - Get Capabilities | ||
|
|
||
| /// Returns locally cached capabilities if available. | ||
| func capabilities(for feed: FeedId) -> Set<FeedOwnCapability>? { | ||
| self.capabilities(for: Set(arrayLiteral: feed))?[feed] | ||
| } | ||
|
|
||
| /// Returns locally cached capabilities if available. | ||
| func capabilities(for feeds: Set<FeedId>) -> [FeedId: Set<FeedOwnCapability>]? { | ||
| let cached = storage.withLock { storage in | ||
| feeds.reduce(into: [FeedId: Set<FeedOwnCapability>](), { all, feedId in | ||
| guard let cached = storage[feedId] else { return } | ||
| all[feedId] = cached | ||
| }) | ||
| } | ||
| if cached.count == feeds.count { | ||
| return cached | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func getCapabilities(for feeds: Set<FeedId>) async throws -> [FeedId: Set<FeedOwnCapability>] { | ||
| let response = try await apiClient.ownCapabilitiesBatch(ownCapabilitiesBatchRequest: OwnCapabilitiesBatchRequest(feeds: feeds.map(\.rawValue))) | ||
| return Dictionary(uniqueKeysWithValues: response.capabilities.map { (FeedId(rawValue: $0), Set($1)) }) | ||
| } | ||
|
|
||
| // MARK: - Saving Capabilities | ||
|
|
||
| func saveCapabilities(_ newCapabilities: [FeedId: Set<FeedOwnCapability>]) -> [FeedId: Set<FeedOwnCapability>]? { | ||
| guard !newCapabilities.isEmpty else { return nil } | ||
| return storage.withLock { storage in | ||
| // Find only the ones which had a state before | ||
| let changed = newCapabilities.filter { storage[$0] != nil && storage[$0] != $1 } | ||
| storage.merge(newCapabilities, uniquingKeysWith: { _, new in new }) | ||
| return changed | ||
| } | ||
| } | ||
|
|
||
| func saveCapabilities(in feedDatas: [FeedData]) -> [FeedId: Set<FeedOwnCapability>]? { | ||
| guard !feedDatas.isEmpty else { return nil } | ||
| let all = feedDatas.reduce(into: [FeedId: Set<FeedOwnCapability>](), { all, feedData in | ||
| guard let capabilities = feedData.ownCapabilities, !capabilities.isEmpty else { return } | ||
| all[feedData.feed] = capabilities | ||
| }) | ||
| return saveCapabilities(all) | ||
| } | ||
|
|
||
| func saveCapabilities(in feedData: FeedData?) -> [FeedId: Set<FeedOwnCapability>]? { | ||
| saveCapabilities(in: [feedData].compactMap { $0 }) | ||
| } | ||
|
Comment on lines
+64
to
+66
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Convenience mostly for |
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For O(1) lookups