-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create DataStoreQuery type to perform query
- Loading branch information
1 parent
80797bc
commit 75f15dc
Showing
6 changed files
with
76 additions
and
66 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,33 +1,2 @@ | ||
import Foundation | ||
import Combine | ||
|
||
public actor InMemoryUserDataStore: UserDataStore, InMemoryDataStore { | ||
public typealias T = DisplayUser | ||
|
||
public var storage: [T.ID: T] = [:] | ||
public let updates: PassthroughSubject<Set<T.ID>, Never> = .init() | ||
|
||
deinit { | ||
updates.send(completion: .finished) | ||
} | ||
|
||
public func list(query: Query) throws -> [T] { | ||
switch query { | ||
case .all: | ||
return Array(storage.values) | ||
case let .id(ids): | ||
return storage.reduce(into: []) { | ||
if ids.contains($1.key) { | ||
$0.append($1.value) | ||
} | ||
} | ||
case let .search(keyword): | ||
let theKeyword = keyword.trimmingCharacters(in: .whitespacesAndNewlines) | ||
if theKeyword.isEmpty { | ||
return Array(storage.values) | ||
} else { | ||
return storage.values.search(theKeyword, using: \.searchString) | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,16 +1,60 @@ | ||
import Foundation | ||
import WordPressShared | ||
|
||
/// An abstraction of local data storage, with CRUD operations. | ||
public protocol DataStore: Actor { | ||
associatedtype T: Identifiable & Sendable | ||
associatedtype Query | ||
public protocol DataStore<T>: Actor { | ||
associatedtype T: Identifiable & Sendable where T.ID: Sendable | ||
|
||
func list(query: Query) async throws -> [T] | ||
func delete(query: Query) async throws | ||
func list(query: DataStoreQuery<T>) async throws -> [T] | ||
func delete(query: DataStoreQuery<T>) async throws | ||
func store(_ data: [T]) async throws | ||
|
||
/// An AsyncStream that produces up-to-date results for the given query. | ||
/// | ||
/// The `AsyncStream` should not finish as long as the `DataStore` remains alive and valid. | ||
func listStream(query: Query) -> AsyncStream<Result<[T], Error>> | ||
func listStream(query: DataStoreQuery<T>) -> AsyncStream<Result<[T], Error>> | ||
} | ||
|
||
public struct DataStoreQuery<T: Identifiable & Sendable>: Sendable where T.ID: Sendable { | ||
public indirect enum Filter: Sendable { | ||
case identifier(Set<T.ID>) | ||
case closure(@Sendable (T) -> Bool) | ||
case and(lhs: Filter, rhs: Filter) | ||
case or(lhs: Filter, rhs: Filter) | ||
|
||
func evaluate(on value: T) -> Bool { | ||
switch self { | ||
case let .identifier(ids): | ||
ids.contains(value.id) | ||
case let .closure(closure): | ||
closure(value) | ||
case let .and(lhs, rhs): | ||
lhs.evaluate(on: value) && rhs.evaluate(on: value) | ||
case let .or(lhs, rhs): | ||
lhs.evaluate(on: value) || rhs.evaluate(on: value) | ||
} | ||
} | ||
} | ||
|
||
var filter: Filter? | ||
var sortBy: [SortDescriptor<T>] = [] | ||
|
||
public func perform(on data: any Sequence<T>) -> [T] { | ||
var result: any Sequence<T> = data | ||
if let filter { | ||
result = result.filter { filter.evaluate(on: $0) } | ||
} | ||
return result.sorted(using: sortBy) | ||
} | ||
|
||
public static var all: Self { .init() } | ||
|
||
public static func identifier(in ids: Set<T.ID>) -> Self { | ||
.init(filter: .identifier(ids)) | ||
} | ||
|
||
public static func search(_ query: String, minScore: Double = 0.7, transform: @escaping (T) -> String) -> Self { | ||
let term = StringRankedSearch(searchTerm: query) | ||
return .init(filter: .closure { term.score(for: transform($0)) >= minScore }) | ||
} | ||
} |
This file contains 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 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