1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use crate::{HashSet, QueryStore};
use artemis::{exchange::Extension, GraphQLQuery};
use fnv::FnvBuildHasher;
use std::{any::Any, collections::HashMap, sync::Arc};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::{JsCast, JsValue};
#[derive(Default)]
pub struct NormalizedCacheOptions {
pub custom_keys: Option<HashMap<&'static str, String, FnvBuildHasher>>
}
#[allow(clippy::type_complexity)]
#[derive(Default, Clone)]
pub struct NormalizedCacheExtension {
pub(crate) optimistic_result:
Option<Arc<dyn (Fn() -> Option<Box<dyn Any + Send>>) + Send + Sync>>,
pub(crate) update:
Option<Arc<dyn Fn(&(dyn Any + Send), QueryStore, &mut HashSet<String>) + Send + Sync>>,
#[cfg(target_arch = "wasm32")]
pub(crate) update_js: Option<js_sys::Function>
}
impl Extension for NormalizedCacheExtension {
#[cfg(target_arch = "wasm32")]
fn from_js(value: JsValue) -> Option<Self> {
let update: JsValue = "update".into();
let update = js_sys::Reflect::get(&value, &update).ok();
let optimistic: JsValue = "optimistic".into();
let optimistic = js_sys::Reflect::get(&value, &optimistic).ok();
let mut result = NormalizedCacheExtension::new();
if let Some(update) = update {
let update: js_sys::Function = update.dyn_into().unwrap();
result = NormalizedCacheExtension {
update_js: Some(update),
..result
}
}
if let Some(optimistic) = optimistic {
let optimistic: js_sys::Function = optimistic.dyn_into().unwrap();
let optimistic = artemis::wasm::JsFunction(optimistic);
result = result.optimistic_result(move || {
let this = JsValue::NULL;
let result = optimistic.0.call0(&this);
serde_wasm_bindgen::from_value::<Option<serde_json::Value>>(result).unwrap()
});
}
Some(result)
}
}
impl NormalizedCacheExtension {
pub fn new() -> Self {
Self::default()
}
pub fn update<Q: GraphQLQuery, F>(mut self, update: F) -> Self
where
F: Fn(&Option<Q::ResponseData>, QueryStore, &mut HashSet<String>) + Send + Sync + 'static
{
self.update = Some(Arc::new(move |data, store, dependencies| {
let data = data.downcast_ref::<Option<Q::ResponseData>>().unwrap();
update(data, store, dependencies);
}));
self
}
pub fn optimistic_result<
Q: GraphQLQuery,
F: (Fn() -> Option<Q::ResponseData>) + 'static + Send + Sync
>(
mut self,
optimistic_result: F
) -> Self {
self.optimistic_result = Some(Arc::new(move || {
optimistic_result().map(|res| -> Box<dyn Any + Send> { Box::new(res) })
}));
self
}
}