myko/core/query/
traits.rs1use std::{fmt::Debug, sync::Arc};
4
5use hyphae::CellImmutable;
6#[cfg(not(target_arch = "wasm32"))]
7use hyphae::MapQuery;
8use serde::{Serialize, de::DeserializeOwned};
9use serde_json::Value;
10
11use super::{
12 super::item::{AnyItem, Eventable},
13 context::QueryContext,
14 request::QueryRequest,
15};
16#[cfg(not(target_arch = "wasm32"))]
17use crate::core::query::QueryCellContext;
18#[cfg(not(target_arch = "wasm32"))]
19use crate::core::query::cell::FilteredCellMap;
20use crate::{
21 cache::CacheKey,
22 client::MykoClient,
23 common::{with_id::WithId, with_transaction::WithTransaction},
24 prelude::WithTypedId,
25 wire::WrappedQuery,
26};
27
28pub trait QueryId {
33 fn query_id(&self) -> Arc<str>;
34}
35
36pub trait QueryIdStatic {
37 fn query_id_static() -> Arc<str>;
38}
39
40pub trait QueryItemType {
41 type Item: WithTypedId + std::fmt::Debug + PartialEq + Send + Sync;
42 fn query_item_type(&self) -> Arc<str>;
43 fn query_item_type_static() -> Arc<str>;
44}
45
46pub trait QueryHandler: QueryItemType + Sized {
57 #[cfg(not(target_arch = "wasm32"))]
61 fn test_entity(ctx: QueryTestCtx<Self>) -> bool
62 where
63 Self: Send + Sync + 'static;
64
65 #[cfg(target_arch = "wasm32")]
72 fn test_entity(_ctx: QueryTestCtx<Self>) -> bool
73 where
74 Self: Send + Sync + 'static,
75 {
76 false
77 }
78
79 #[cfg(not(target_arch = "wasm32"))]
90 fn build_view(
91 _ctx: QueryBuildCellCtx<Self>,
92 ) -> Option<impl MapQuery<Arc<str>, Arc<dyn AnyItem>>>
93 where
94 Self: Send + Sync + 'static,
95 {
96 None::<FilteredCellMap>
97 }
98}
99
100pub struct QueryTestCtx<TQuery: QueryItemType> {
101 pub item: Arc<TQuery::Item>,
102 pub query: Arc<TQuery>,
103 pub query_context: Arc<QueryContext>,
104}
105
106impl<TQuery: QueryItemType> QueryTestCtx<TQuery> {
107 pub fn map_bool<F>(self, predicate: F) -> bool
108 where
109 F: Fn(QueryTestCtx<TQuery>) -> bool,
110 {
111 predicate(self)
112 }
113}
114
115#[cfg(not(target_arch = "wasm32"))]
116pub struct QueryBuildCellCtx<TQuery: QueryItemType> {
117 pub query: Arc<TQuery>,
118 pub query_context: QueryCellContext,
119}
120
121#[derive(Debug)]
122#[allow(dead_code)]
123pub struct QueryHandlerCtxAny {
124 pub item: Arc<dyn AnyItem>,
125 pub query: Arc<dyn AnyQuery>,
126 pub ctx: Arc<QueryContext>,
127}
128
129pub trait QueryParams:
140 CacheKey
141 + Serialize
142 + DeserializeOwned
143 + Clone
144 + Send
145 + Sync
146 + QueryId
147 + QueryIdStatic
148 + QueryItemType
149 + QueryHandler
150 + std::fmt::Debug
151 + 'static
152{
153}
154
155impl<T> QueryParams for T where
157 T: Serialize
158 + CacheKey
159 + DeserializeOwned
160 + Clone
161 + Send
162 + Sync
163 + QueryId
164 + QueryIdStatic
165 + QueryItemType
166 + QueryHandler
167 + std::fmt::Debug
168 + 'static
169{
170}
171
172pub trait Query:
181 Serialize
182 + DeserializeOwned
183 + Send
184 + Sync
185 + QueryId
186 + QueryIdStatic
187 + QueryItemType
188 + QueryHandler
189 + WithTransaction
190 + AnyQuery
191 + 'static
192{
193 type Params: QueryParams;
195
196 fn watch(
197 &self,
198 client: &MykoClient,
199 ) -> hyphae::Cell<Vec<Arc<<Self as QueryItemType>::Item>>, CellImmutable>;
200}
201
202impl<Q: QueryParams + Clone> Query for QueryRequest<Q>
204where
205 Q::Item:
206 Eventable + WithId + DeserializeOwned + Clone + std::fmt::Debug + Send + Sync + 'static,
207{
208 type Params = Q;
209
210 fn watch(
211 &self,
212 client: &MykoClient,
213 ) -> hyphae::Cell<Vec<Arc<<Self as QueryItemType>::Item>>, CellImmutable> {
214 client.watch_query::<Q>(self)
215 }
216}
217
218pub trait AnyQuery: WithTransaction + QueryId + Debug + Send + Sync + 'static {
225 fn query_item_type(&self) -> Arc<str>;
227
228 fn to_value(&self) -> Value;
230}
231
232impl From<&dyn AnyQuery> for WrappedQuery {
234 fn from(query: &dyn AnyQuery) -> Self {
235 WrappedQuery {
236 query: query.to_value(),
237 query_id: query.query_id(),
238 query_item_type: query.query_item_type(),
239 window: None,
240 }
241 }
242}
243
244impl From<Arc<dyn AnyQuery>> for WrappedQuery {
245 fn from(query: Arc<dyn AnyQuery>) -> Self {
246 WrappedQuery::from(query.as_ref())
247 }
248}
249
250impl From<&Arc<dyn AnyQuery>> for WrappedQuery {
251 fn from(query: &Arc<dyn AnyQuery>) -> Self {
252 WrappedQuery::from(query.as_ref())
253 }
254}