Skip to content

Commit 848cf8f

Browse files
committed
finish implementing Cursor API
1 parent 56698d5 commit 848cf8f

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

src/cursor.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use crate::{
22
transaction::transaction_request,
33
utils::{
44
map_cursor_advance_err, map_cursor_advance_until_err,
5-
map_cursor_advance_until_primary_key_err, map_cursor_delete_err, slice_to_array,
5+
map_cursor_advance_until_primary_key_err, map_cursor_delete_err, map_cursor_update_err,
6+
slice_to_array,
67
},
78
};
89
use std::marker::PhantomData;
@@ -151,12 +152,26 @@ impl<Err> Cursor<Err> {
151152
/// Note that this method does not work on key-only cursors over indexes.
152153
///
153154
/// Internally, this uses [`IDBCursor::delete`](https://developer.mozilla.org/en-US/docs/Web/API/IDBCursor/delete).
154-
pub async fn delete(&mut self) -> crate::Result<(), Err> {
155+
pub async fn delete(&self) -> crate::Result<(), Err> {
155156
let Some(sys) = &self.sys else {
156157
return Err(crate::Error::CursorCompleted);
157158
};
158159
let req = sys.delete().map_err(map_cursor_delete_err)?;
159160
transaction_request(req).await?;
160161
Ok(())
161162
}
163+
164+
/// Update the value currently pointed by this [`Cursor`] to `value`
165+
///
166+
/// Note that this method does not work on key-only cursors over indexes.
167+
///
168+
/// Internally, this uses [`IDBCursor::update`](https://developer.mozilla.org/en-US/docs/Web/API/IDBCursor/update).
169+
pub async fn update(&self, value: &JsValue) -> crate::Result<(), Err> {
170+
let Some(sys) = &self.sys else {
171+
return Err(crate::Error::CursorCompleted);
172+
};
173+
let req = sys.update(value).map_err(map_cursor_update_err)?;
174+
transaction_request(req).await?;
175+
Ok(())
176+
}
162177
}

src/utils.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,19 @@ pub(crate) fn map_cursor_delete_err<Err>(err: JsValue) -> crate::Error<Err> {
174174
}
175175
}
176176

177+
pub(crate) fn map_cursor_update_err<Err>(err: JsValue) -> crate::Error<Err> {
178+
match error_name!(&err) {
179+
Some("InvalidStateError") => crate::Error::CursorCompleted,
180+
Some("TransactionInactiveError") => {
181+
panic!("Tried advancing a Cursor on an ObjectStore while the transaction was inactive")
182+
}
183+
Some("ReadOnlyError") => crate::Error::ReadOnly,
184+
Some("DataError") => crate::Error::InvalidKey,
185+
Some("DataCloneError") => crate::Error::FailedClone,
186+
_ => crate::Error::from_js_value(err),
187+
}
188+
}
189+
177190
fn bound_map<T, U>(b: Bound<T>, f: impl FnOnce(T) -> U) -> Bound<U> {
178191
// TODO: replace with Bound::map once https://github.com/rust-lang/rust/issues/86026 is stable
179192
match b {

tests/js.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ async fn smoke_test() {
197197
let mut cursor = stuffs.cursor().open().await.unwrap();
198198
while let Some(val) = cursor.value() {
199199
all.push(val);
200+
cursor.delete().await.unwrap();
200201
cursor.advance(1).await.unwrap();
201202
}
202203
assert_eq!(
@@ -207,6 +208,7 @@ async fn smoke_test() {
207208
(**JsString::from("value1")).clone()
208209
]
209210
);
211+
assert_eq!(stuffs.count().await.unwrap(), 0);
210212

211213
Ok(())
212214
})

0 commit comments

Comments
 (0)