Add WebSocket RPC unset method for clearing a parameter

This commit is contained in:
Tobie Morgan Hitchcock 2022-10-25 06:23:18 -07:00
parent 70a263f799
commit df8f297221

View file

@ -256,6 +256,11 @@ impl Rpc {
"select" => match params.take_one() { "select" => match params.take_one() {
v if v.is_thing() => rpc.read().await.select(v).await, v if v.is_thing() => rpc.read().await.select(v).await,
v if v.is_strand() => rpc.read().await.select(v).await, v if v.is_strand() => rpc.read().await.select(v).await,
// Unset and clear a connection-wide parameter
"unset" => match params.needs_one() {
Ok(Value::Strand(s)) => rpc.write().await.unset(s).await,
_ => return res::failure(id, Failure::INVALID_PARAMS).send(out, chn).await,
},
// Select a value or values from the database // Select a value or values from the database
_ => return res::failure(id, Failure::INVALID_PARAMS).send(out, chn).await, _ => return res::failure(id, Failure::INVALID_PARAMS).send(out, chn).await,
}, },
@ -377,17 +382,17 @@ impl Rpc {
async fn set(&mut self, key: Strand, val: Value) -> Result<Value, Error> { async fn set(&mut self, key: Strand, val: Value) -> Result<Value, Error> {
match val { match val {
// Remove the variable if the value is NULL // Remove the variable if undefined
v if v.is_null() => { Value::None => self.vars.remove(&key.0),
self.vars.remove(&key.0); // Store the variable if defined
Ok(Value::Null) v => self.vars.insert(key.0, v),
} };
// Store the variable if not NULL Ok(Value::Null)
v => { }
self.vars.insert(key.0, v);
Ok(Value::Null) async fn unset(&mut self, key: Strand) -> Result<Value, Error> {
} self.vars.remove(&key.0);
} Ok(Value::Null)
} }
// ------------------------------ // ------------------------------