surrealdb/api/engine/remote/ws/
mod.rs1#[cfg(not(target_family = "wasm"))]
4pub(crate) mod native;
5#[cfg(target_family = "wasm")]
6pub(crate) mod wasm;
7
8use std::collections::HashMap;
9use std::marker::PhantomData;
10use std::time::Duration;
11
12use async_channel::Sender;
13use indexmap::IndexMap;
14use trice::Instant;
15use uuid::Uuid;
16
17use crate::api::conn::{Command, DbResponse};
18use crate::api::{Connect, Result, Surreal};
19use crate::core::dbs::Notification;
20use crate::core::val::Value as CoreValue;
21use crate::opt::IntoEndpoint;
22
23pub(crate) const PATH: &str = "rpc";
24const PING_INTERVAL: Duration = Duration::from_secs(5);
25const REVISION_HEADER: &str = "revision";
26
27enum RequestEffect {
28 Set {
30 key: String,
31 value: CoreValue,
32 },
33 Clear {
35 key: String,
36 },
37 Insert,
39 None,
41}
42
43#[derive(Clone, Copy, Eq, PartialEq, Hash)]
44enum ReplayMethod {
45 Use,
46 Signup,
47 Signin,
48 Invalidate,
49 Authenticate,
50}
51
52struct PendingRequest {
53 effect: RequestEffect,
55 response_channel: Sender<Result<DbResponse>>,
57}
58
59struct RouterState<Sink, Stream> {
60 vars: IndexMap<String, CoreValue>,
62 replay: IndexMap<ReplayMethod, Command>,
64 live_queries: HashMap<Uuid, async_channel::Sender<Notification>>,
66 pending_requests: HashMap<i64, PendingRequest>,
68 last_activity: Instant,
70 sink: Sink,
72 stream: Stream,
74}
75
76impl<Sink, Stream> RouterState<Sink, Stream> {
77 pub fn new(sink: Sink, stream: Stream) -> Self {
78 RouterState {
79 vars: IndexMap::new(),
80 replay: IndexMap::new(),
81 live_queries: HashMap::new(),
82 pending_requests: HashMap::new(),
83 last_activity: Instant::now(),
84 sink,
85 stream,
86 }
87 }
88}
89
90enum HandleResult {
91 Disconnected,
93 Ok,
95}
96
97#[derive(Debug)]
99pub struct Ws;
100
101#[derive(Debug)]
103pub struct Wss;
104
105#[derive(Debug, Clone)]
107pub struct Client(());
108
109impl Surreal<Client> {
110 pub fn connect<P>(
130 &self,
131 address: impl IntoEndpoint<P, Client = Client>,
132 ) -> Connect<Client, ()> {
133 Connect {
134 surreal: self.inner.clone().into(),
135 address: address.into_endpoint(),
136 capacity: 0,
137 response_type: PhantomData,
138 }
139 }
140}