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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
use crate::core::GunCore;
use crate::error::GunResult;
use crate::state::Node;
use crate::valid::valid;
use serde_json::Value;
use std::collections::HashSet;
use std::sync::Arc;
/// Chain - the main API for interacting with Gun
/// Based on Gun.js chain.js and IGunChain interface
/// This provides the fluent API: gun.get('key').put(data).on(callback)
pub struct Chain {
pub core: Arc<GunCore>,
pub soul: Option<String>,
pub key: Option<String>,
pub parent: Option<Arc<Chain>>,
pub id: u64,
listener_ids: Arc<parking_lot::Mutex<HashSet<u64>>>, // Track listener IDs for off()
}
impl Chain {
pub fn new(core: Arc<GunCore>) -> Self {
let id = core.next_chain_id();
Self {
core,
soul: None,
key: None,
parent: None,
id,
listener_ids: Arc::new(parking_lot::Mutex::new(HashSet::new())),
}
}
pub fn with_soul(core: Arc<GunCore>, soul: String, parent: Option<Arc<Chain>>) -> Self {
let id = core.next_chain_id();
Self {
core,
soul: Some(soul),
key: None,
parent,
id,
listener_ids: Arc::new(parking_lot::Mutex::new(HashSet::new())),
}
}
pub fn with_key(core: Arc<GunCore>, key: String, parent: Arc<Chain>) -> Self {
let id = core.next_chain_id();
Self {
core,
soul: parent.soul.clone(),
key: Some(key),
parent: Some(parent),
id,
listener_ids: Arc::new(parking_lot::Mutex::new(HashSet::new())),
}
}
/// Get a property or node by key
/// Based on Gun.js chain.get()
pub fn get(&self, key: &str) -> Arc<Chain> {
Arc::new(Chain::with_key(
self.core.clone(),
key.to_string(),
Arc::new(self.clone()),
))
}
/// Put data into the current node/property
/// Based on Gun.js chain.put() - improved implementation
pub async fn put(&self, data: Value) -> GunResult<Arc<Chain>> {
// Handle function callback (deferred data)
// In Rust, this would be handled via async, so we'll skip this case for now
// Validate data
match valid(&data) {
Ok(true) => {} // Valid simple value
Err(Some(soul)) => {
// It's a soul reference, create link
let soul_chain = self.core.graph.get(&soul);
if soul_chain.is_none() {
// Soul doesn't exist yet, we'll need to request it
// For now, create the node
let node = Node::with_soul(soul.clone());
self.core.graph.put(&soul, node)?;
}
return Ok(Arc::new(Chain::with_soul(
self.core.clone(),
soul,
Some(Arc::new(self.clone())),
)));
}
_ => {
// Invalid or object - handle object case
if let Value::Object(map) = data {
return self.put_object(map).await;
}
return Err(crate::error::GunError::InvalidData(
"Invalid data type".to_string(),
));
}
}
let soul = match &self.soul {
Some(s) => s.clone(),
None => self.core.uuid(None),
};
// Create or update node
let mut node = self
.core
.graph
.get(&soul)
.unwrap_or_else(|| Node::with_soul(soul.clone()));
// Merge data into node
if let Some(key) = &self.key {
// Setting a property
let state = self.core.state.next();
node.data.insert(key.clone(), data.clone());
crate::state::State::ify(&mut node, Some(key), Some(state), Some(data), Some(&soul));
} else {
// Setting the whole node - but data is not an object here, so this shouldn't happen
// This case is handled above in put_object
}
// Store in graph
self.core.graph.put(&soul, node.clone())?;
// Emit update event
self.emit_update(&soul, &node.data);
// Store in persistent storage if available
if let Some(storage) = &self.core.storage {
storage.put(&soul, &node).await?;
}
Ok(Arc::new(Chain::with_soul(
self.core.clone(),
soul,
Some(Arc::new(self.clone())),
)))
}
/// Helper to put an object (node) with proper traversal
async fn put_object(&self, map: serde_json::Map<String, Value>) -> GunResult<Arc<Chain>> {
let soul = match &self.soul {
Some(s) => s.clone(),
None => self.core.uuid(None),
};
let mut node = self
.core
.graph
.get(&soul)
.unwrap_or_else(|| Node::with_soul(soul.clone()));
// Process each key-value pair
for (k, v) in map {
let state = self.core.state.next();
// Check if value is a soul reference
match valid(&v) {
Err(Some(ref_soul)) => {
// It's a reference to another node
let ref_node = self.core.graph.get(&ref_soul);
if ref_node.is_none() {
// Create placeholder node
let placeholder = Node::with_soul(ref_soul.clone());
self.core.graph.put(&ref_soul, placeholder)?;
}
// Store as soul reference
node.data
.insert(k.clone(), serde_json::json!({"#": ref_soul}));
}
_ => {
// Regular value
node.data.insert(k.clone(), v.clone());
}
}
crate::state::State::ify(&mut node, Some(&k), Some(state), Some(v), Some(&soul));
}
self.core.graph.put(&soul, node.clone())?;
self.emit_update(&soul, &node.data);
if let Some(storage) = &self.core.storage {
storage.put(&soul, &node).await?;
}
Ok(Arc::new(Chain::with_soul(
self.core.clone(),
soul,
Some(Arc::new(self.clone())),
)))
}
/// Emit update event for listeners (synchronous)
fn emit_update(&self, soul: &str, data: &serde_json::Map<String, Value>) {
let event_type = format!("node_update:{}", soul);
let event = crate::events::Event {
event_type: event_type.clone(),
data: serde_json::Value::Object(data.clone()),
};
self.core.events.emit(&event);
}
/// Subscribe to updates on this node/property
/// Based on Gun.js chain.on() - improved with proper listener tracking
pub fn on<F>(&self, callback: F) -> Arc<Chain>
where
F: Fn(Value, Option<String>) + Send + Sync + 'static,
{
let chain = Arc::new(self.clone());
let soul = self.soul.clone();
let key = self.key.clone();
let listener_ids = self.listener_ids.clone();
let cb = Box::new(move |event: &crate::events::Event| {
// Extract data and call callback
let value = if let Some(key) = &key {
event.data.get(key).cloned().unwrap_or(Value::Null)
} else {
event.data.clone()
};
callback(value, key.clone());
});
let event_type = if let Some(ref s) = soul {
format!("node_update:{}", s)
} else {
"graph_update".to_string()
};
let listener_id = self.core.events.on(&event_type, cb);
listener_ids.lock().insert(listener_id);
chain
}
/// Get data once without subscribing
/// Based on Gun.js chain.once() - improved with async waiting
pub async fn once<F>(&self, callback: F) -> GunResult<Arc<Chain>>
where
F: FnOnce(Value, Option<String>),
{
let soul = match &self.soul {
Some(s) => s.clone(),
None => {
// Return undefined/None if no soul
callback(Value::Null, self.key.clone());
return Ok(Arc::new(self.clone()));
}
};
// Try to get from graph immediately
if let Some(node) = self.core.graph.get(&soul) {
let value = if let Some(key) = &self.key {
node.data.get(key).cloned().unwrap_or(Value::Null)
} else {
serde_json::to_value(&node.data).unwrap_or(Value::Null)
};
callback(value, self.key.clone());
} else {
// Data not found - in full implementation would request from network
// For now, call with null
callback(Value::Null, self.key.clone());
}
Ok(Arc::new(self.clone()))
}
/// Map over properties of a node
/// Based on Gun.js chain.map() - complete implementation
pub fn map<F>(&self, callback: F) -> Arc<Chain>
where
F: Fn(Value, String) + Send + Sync + Clone + 'static,
{
let chain = Arc::new(self.clone());
let soul = self.soul.clone();
let listener_ids = self.listener_ids.clone();
// Clone callback for use in closure
let callback_clone = callback.clone();
// Subscribe to updates and call callback for each property
if let Some(ref s) = soul {
let event_type = format!("node_update:{}", s);
let cb = Box::new(move |event: &crate::events::Event| {
if let Some(data_obj) = event.data.as_object() {
for (key, value) in data_obj {
callback_clone(value.clone(), key.clone());
}
}
});
let listener_id = self.core.events.on(&event_type, cb);
listener_ids.lock().insert(listener_id);
// Also call for current data if available
if let Some(node) = self.core.graph.get(s) {
for (key, value) in node.data.iter() {
callback(value.clone(), key.clone());
}
}
}
chain
}
/// Add item to a set
/// Based on Gun.js chain.set() - proper set implementation
pub async fn set(&self, item: Value) -> GunResult<Arc<Chain>> {
// Check if item has a soul (is a node reference)
let soul = match valid(&item) {
Err(Some(ref_soul)) => Some(ref_soul.clone()),
_ => {
// Item doesn't have a soul, generate one
// set() expects objects (nodes) to be added to the set
if item.is_object() {
let new_soul = self.core.uuid(None);
// Create node for the item
if let Value::Object(ref map) = item {
let mut node = Node::with_soul(new_soul.clone());
for (k, v) in map {
let state = self.core.state.next();
node.data.insert(k.clone(), v.clone());
crate::state::State::ify(
&mut node,
Some(k),
Some(state),
Some(v.clone()),
Some(&new_soul),
);
}
self.core.graph.put(&new_soul, node.clone())?;
if let Some(storage) = &self.core.storage {
storage.put(&new_soul, &node).await?;
}
}
Some(new_soul)
} else {
// Non-object items can't be in sets
return Err(crate::error::GunError::InvalidData(
"set() only accepts objects/nodes".to_string(),
));
}
}
};
if let Some(ref_soul) = soul {
// Add reference to the set node
let set_soul = self.soul.clone().unwrap_or_else(|| self.core.uuid(None));
let mut set_node = self
.core
.graph
.get(&set_soul)
.unwrap_or_else(|| Node::with_soul(set_soul.clone()));
// Store reference to the item
let key = ref_soul.clone();
let state = self.core.state.next();
set_node
.data
.insert(key.clone(), serde_json::json!({"#": ref_soul}));
crate::state::State::ify(
&mut set_node,
Some(&key),
Some(state),
Some(serde_json::json!({"#": ref_soul})),
Some(&set_soul),
);
self.core.graph.put(&set_soul, set_node.clone())?;
self.emit_update(&set_soul, &set_node.data);
if let Some(storage) = &self.core.storage {
storage.put(&set_soul, &set_node).await?;
}
Ok(Arc::new(Chain::with_soul(
self.core.clone(),
set_soul,
Some(Arc::new(self.clone())),
)))
} else {
self.put(item).await
}
}
/// Go back up the chain
/// Based on Gun.js chain.back()
pub fn back(&self, amount: Option<usize>) -> Option<Arc<Chain>> {
match amount {
Some(0) | None => self.parent.clone(),
Some(1) => self.parent.clone(),
Some(n) => {
// Go back n levels
let mut current = self.parent.clone();
for _ in 1..n {
current = current.and_then(|c| c.parent.clone());
}
current
}
}
}
/// Remove all listeners for this chain
/// Based on Gun.js chain.off() - properly removes listeners
pub fn off(&self) -> Arc<Chain> {
let listener_ids = self.listener_ids.lock();
let ids: Vec<u64> = listener_ids.iter().cloned().collect();
drop(listener_ids); // Release lock
let event_type = if let Some(ref s) = self.soul {
format!("node_update:{}", s)
} else {
"graph_update".to_string()
};
for id in ids {
self.core.events.off(&event_type, id);
}
self.listener_ids.lock().clear();
Arc::new(self.clone())
}
}
impl Clone for Chain {
fn clone(&self) -> Self {
Self {
core: self.core.clone(),
soul: self.soul.clone(),
key: self.key.clone(),
parent: self.parent.clone(),
id: self.id,
listener_ids: self.listener_ids.clone(),
}
}
}