Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions test/src/alarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ impl DurableObject for AlarmObject {
.storage()
.set_alarm(Duration::from_millis(100))
.await?;
let alarmed: bool = match self.state.storage().get("alarmed").await {
Ok(alarmed) => alarmed,
Err(e) if e.to_string() == "No such value in storage." => false,
Err(e) => return Err(e),
};
let alarmed: bool = self.state.storage().get("alarmed").await?.unwrap_or(false);
Response::ok(alarmed.to_string())
}

Expand Down
4 changes: 2 additions & 2 deletions test/src/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl DurableObject for Counter {
async fn fetch(&self, req: Request) -> Result<Response> {
if !*self.initialized.borrow() {
*self.initialized.borrow_mut() = true;
*self.count.borrow_mut() = self.state.storage().get("count").await.unwrap_or(0);
*self.count.borrow_mut() = self.state.storage().get("count").await?.unwrap_or(0);
}

if req.path().eq("/ws") {
Expand Down Expand Up @@ -71,7 +71,7 @@ impl DurableObject for Counter {
.deserialize_attachment()?
.expect("websockets should have an attachment");
// get and increment storage by 10
let mut count: usize = self.state.storage().get("count").await.unwrap_or(0);
let mut count: usize = self.state.storage().get("count").await?.unwrap_or(0);
count += 10;
self.state.storage().put("count", count).await?;
// send value to client
Expand Down
22 changes: 17 additions & 5 deletions test/src/durable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ impl DurableObject for MyClass {
let bytes = Uint8Array::new_with_length(3);
bytes.copy_from(b"123");
storage.put_raw("bytes", bytes).await?;
let bytes = storage.get::<Vec<u8>>("bytes").await?;
let bytes = storage
.get::<Vec<u8>>("bytes")
.await?
.expect("get after put yielded nothing");
storage.delete("bytes").await?;
assert_eq!(
bytes, b"123",
Expand All @@ -117,12 +120,18 @@ impl DurableObject for MyClass {
.await?;

assert_eq!(
storage.get::<String>("thing").await?,
storage
.get::<String>("thing")
.await?
.expect("get('thing') yielded nothing"),
"Hello there",
"Didn't put the right thing with put_multiple"
);
assert_eq!(
storage.get::<i32>("other").await?,
storage
.get::<i32>("other")
.await?
.expect("get('other') yielded nothing"),
56,
"Didn't put the right thing with put_multiple"
);
Expand All @@ -137,13 +146,16 @@ impl DurableObject for MyClass {
js_sys::Reflect::set(&obj, &JsValue::from_str("foo"), &value.into())?;
storage.put_multiple_raw(obj).await?;
assert_eq!(
storage.get::<Vec<u8>>("foo").await?,
storage
.get::<Vec<u8>>("foo")
.await?
.expect("get('foo') yielded nothing"),
BAR,
"Didn't the right thing with put_multiple_raw"
);
}

*self.number.borrow_mut() = storage.get("count").await.unwrap_or(0) + 1;
*self.number.borrow_mut() = storage.get("count").await?.unwrap_or(0) + 1;

storage.delete_all().await?;

Expand Down
10 changes: 8 additions & 2 deletions test/src/put_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ impl PutRawTestObject {
let bytes = Uint8Array::new_with_length(3);
bytes.copy_from(b"123");
storage.put_raw("bytes", bytes).await?;
let bytes = storage.get::<Vec<u8>>("bytes").await?;
let bytes = storage
.get::<Vec<u8>>("bytes")
.await?
.expect("get after put yielded nothing");
storage.delete("bytes").await?;
assert_eq!(
bytes, b"123",
Expand All @@ -36,7 +39,10 @@ impl PutRawTestObject {
storage.put_multiple_raw(obj).await?;

assert_eq!(
storage.get::<Vec<u8>>("foo").await?,
storage
.get::<Vec<u8>>("foo")
.await?
.expect("get('foo') yielded nothing"),
BAR,
"Didn't get the right thing with put_multiple_raw"
);
Expand Down
6 changes: 3 additions & 3 deletions worker-build/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ fn generate_handlers() -> Result<String> {
let mut handlers = String::new();
for func_name in func_names {
if func_name == "fetch" && env::var("RUN_TO_COMPLETION").is_ok() {
handlers += "Entrypoint.prototype.fetch = async function fetch(request) {{
handlers += "Entrypoint.prototype.fetch = async function fetch(request) {
let response = exports.fetch(request, this.env, this.ctx);
this.ctx.waitUntil(response);
return response;
}}
"
}
";
} else if func_name == "fetch" || func_name == "queue" || func_name == "scheduled" {
// TODO: Switch these over to https://github.com/wasm-bindgen/wasm-bindgen/pull/4757
// once that lands.
Expand Down
26 changes: 14 additions & 12 deletions worker/src/durable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,18 +344,20 @@ impl Storage {
/// Retrieves the value associated with the given key. The type of the returned value will be
/// whatever was previously written for the key.
///
/// Returns [Err] if the key does not exist.
pub async fn get<T: serde::de::DeserializeOwned>(&self, key: &str) -> Result<T> {
JsFuture::from(self.inner.get(key)?)
.await
.and_then(|val| {
if val.is_undefined() {
Err(JsValue::from("No such value in storage."))
} else {
serde_wasm_bindgen::from_value(val).map_err(|e| JsValue::from(e.to_string()))
}
})
.map_err(Error::from)
/// Returns `Ok(None)` if the key does not exist.
pub async fn get<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>> {
let res = match JsFuture::from(self.inner.get(key)?).await {
// If we successfully retrived `undefined`, that means the key doesn't exist
Ok(val) if val.is_undefined() => Ok(None),
// Otherwise deserialize whatever we successfully received
Ok(val) => {
serde_wasm_bindgen::from_value(val).map_err(|e| JsValue::from(e.to_string()))
}
// Forward any error, rewrap to make the typechecker happy
Err(e) => Err(e),
};

res.map_err(Error::from)
}

/// Retrieves the values associated with each of the provided keys.
Expand Down