|
1 | 1 | import Foundation
|
| 2 | +import WordPressShared |
2 | 3 |
|
3 | 4 | /// An abstraction of local data storage, with CRUD operations.
|
4 |
| -public protocol DataStore: Actor { |
5 |
| - associatedtype T: Identifiable & Sendable |
6 |
| - associatedtype Query |
| 5 | +public protocol DataStore<T>: Actor { |
| 6 | + associatedtype T: Identifiable & Sendable where T.ID: Sendable |
7 | 7 |
|
8 |
| - func list(query: Query) async throws -> [T] |
9 |
| - func delete(query: Query) async throws |
| 8 | + func list(query: DataStoreQuery<T>) async throws -> [T] |
| 9 | + func delete(query: DataStoreQuery<T>) async throws |
10 | 10 | func store(_ data: [T]) async throws
|
11 | 11 |
|
12 | 12 | /// An AsyncStream that produces up-to-date results for the given query.
|
13 | 13 | ///
|
14 | 14 | /// The `AsyncStream` should not finish as long as the `DataStore` remains alive and valid.
|
15 |
| - func listStream(query: Query) -> AsyncStream<Result<[T], Error>> |
| 15 | + func listStream(query: DataStoreQuery<T>) -> AsyncStream<Result<[T], Error>> |
| 16 | +} |
| 17 | + |
| 18 | +public struct DataStoreQuery<T: Identifiable & Sendable>: Sendable where T.ID: Sendable { |
| 19 | + public indirect enum Filter: Sendable { |
| 20 | + case identifier(Set<T.ID>) |
| 21 | + case closure(@Sendable (T) -> Bool) |
| 22 | + case and(lhs: Filter, rhs: Filter) |
| 23 | + case or(lhs: Filter, rhs: Filter) |
| 24 | + |
| 25 | + func evaluate(on value: T) -> Bool { |
| 26 | + switch self { |
| 27 | + case let .identifier(ids): |
| 28 | + ids.contains(value.id) |
| 29 | + case let .closure(closure): |
| 30 | + closure(value) |
| 31 | + case let .and(lhs, rhs): |
| 32 | + lhs.evaluate(on: value) && rhs.evaluate(on: value) |
| 33 | + case let .or(lhs, rhs): |
| 34 | + lhs.evaluate(on: value) || rhs.evaluate(on: value) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + var filter: Filter? |
| 40 | + var sortBy: [SortDescriptor<T>] = [] |
| 41 | + |
| 42 | + public func perform(on data: any Sequence<T>) -> [T] { |
| 43 | + var result: any Sequence<T> = data |
| 44 | + if let filter { |
| 45 | + result = result.filter { filter.evaluate(on: $0) } |
| 46 | + } |
| 47 | + return result.sorted(using: sortBy) |
| 48 | + } |
| 49 | + |
| 50 | + public static var all: Self { .init() } |
| 51 | + |
| 52 | + public static func identifier(in ids: Set<T.ID>) -> Self { |
| 53 | + .init(filter: .identifier(ids)) |
| 54 | + } |
| 55 | + |
| 56 | + public static func search(_ query: String, minScore: Double = 0.7, transform: @escaping (T) -> String) -> Self { |
| 57 | + let term = StringRankedSearch(searchTerm: query) |
| 58 | + return .init(filter: .closure { term.score(for: transform($0)) >= minScore }) |
| 59 | + } |
16 | 60 | }
|
0 commit comments