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
//! The core `DurableObject` trait.
use Future;
use ;
use DurableContext;
use DurableObjectError;
use ;
use crateRouter;
/// A Durable Object with hibernation-first WebSocket support.
///
/// # Design
///
/// - **Struct IS the state**: Your struct must be `Serialize + DeserializeOwned + Default`.
/// On first creation, `Default::default()` produces the initial state.
/// On subsequent activations, the struct is deserialized from storage.
/// After each event, it is re-serialized.
/// See [`PERSIST`](Self::PERSIST) for where that model stops scaling and how to opt out.
///
/// - **`&mut self` everywhere**: Durable Objects have single-threaded serial execution.
/// No locks needed.
///
/// - **Two methods**: `fetch` for HTTP (returns a [`Router`]),
/// `websocket` for all hibernation WS events.
///
/// # Example
///
/// ```ignore
/// use serde::{Serialize, Deserialize};
/// use skyzen::durable::*;
/// use skyzen::routing::{CreateRouteNode, Route, Router};
///
/// #[derive(Serialize, Deserialize, Default)]
/// struct Counter {
/// count: u64,
/// }
///
/// impl DurableObject for Counter {
/// fn fetch(&mut self) -> Router {
/// Route::new((
/// "/increment".at(increment),
/// ))
/// .build()
/// }
/// }
///
/// async fn increment() -> skyzen::Result<String> {
/// Ok("incremented".to_string())
/// }
/// ```