|
1 | 1 | use crate::{ |
2 | 2 | transaction::transaction_request, |
3 | 3 | utils::{ |
4 | | - map_cursor_advance_err, map_cursor_advance_until_err, |
| 4 | + make_key_range, map_cursor_advance_err, map_cursor_advance_until_err, |
5 | 5 | map_cursor_advance_until_primary_key_err, map_cursor_delete_err, map_cursor_update_err, |
| 6 | + map_open_cursor_err, |
6 | 7 | }, |
7 | 8 | }; |
8 | | -use std::marker::PhantomData; |
| 9 | +use futures_util::future::Either; |
| 10 | +use std::{future::Future, marker::PhantomData, ops::RangeBounds}; |
9 | 11 | use web_sys::{ |
10 | 12 | wasm_bindgen::{JsCast, JsValue}, |
11 | | - IdbCursor, IdbCursorDirection, IdbCursorWithValue, IdbRequest, |
| 13 | + IdbCursor, IdbCursorDirection, IdbCursorWithValue, IdbIndex, IdbObjectStore, IdbRequest, |
12 | 14 | }; |
13 | 15 |
|
14 | 16 | #[cfg(doc)] |
15 | | -use crate::Index; |
| 17 | +use crate::{Index, ObjectStore}; |
16 | 18 | #[cfg(doc)] |
17 | 19 | use web_sys::js_sys::Array; |
18 | 20 |
|
@@ -42,6 +44,86 @@ impl CursorDirection { |
42 | 44 | } |
43 | 45 | } |
44 | 46 |
|
| 47 | +/// Helper to build cursors over [`ObjectStore`]s |
| 48 | +pub struct CursorBuilder<Err> { |
| 49 | + source: Either<IdbObjectStore, IdbIndex>, |
| 50 | + query: JsValue, |
| 51 | + direction: IdbCursorDirection, |
| 52 | + _phantom: PhantomData<Err>, |
| 53 | +} |
| 54 | + |
| 55 | +impl<Err> CursorBuilder<Err> { |
| 56 | + pub(crate) fn from_store(store: IdbObjectStore) -> CursorBuilder<Err> { |
| 57 | + CursorBuilder { |
| 58 | + source: Either::Left(store), |
| 59 | + query: JsValue::UNDEFINED, |
| 60 | + direction: IdbCursorDirection::Next, |
| 61 | + _phantom: PhantomData, |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + pub(crate) fn from_index(index: IdbIndex) -> CursorBuilder<Err> { |
| 66 | + CursorBuilder { |
| 67 | + source: Either::Right(index), |
| 68 | + query: JsValue::UNDEFINED, |
| 69 | + direction: IdbCursorDirection::Next, |
| 70 | + _phantom: PhantomData, |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /// Open the cursor |
| 75 | + /// |
| 76 | + /// Internally, this uses [`IDBObjectStore::openCursor`](https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/openCursor). |
| 77 | + pub fn open(self) -> impl Future<Output = crate::Result<Cursor<Err>, Err>> { |
| 78 | + let req = match self.source { |
| 79 | + Either::Left(store) => { |
| 80 | + store.open_cursor_with_range_and_direction(&self.query, self.direction) |
| 81 | + } |
| 82 | + Either::Right(index) => { |
| 83 | + index.open_cursor_with_range_and_direction(&self.query, self.direction) |
| 84 | + } |
| 85 | + }; |
| 86 | + match req { |
| 87 | + Ok(open_req) => Either::Right(Cursor::from(open_req)), |
| 88 | + Err(err) => Either::Left(std::future::ready(Err(map_open_cursor_err(err)))), |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /// Open the cursor as a key-only cursor |
| 93 | + /// |
| 94 | + /// Internally, this uses [`IDBObjectStore::openKeyCursor`](https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/openKeyCursor). |
| 95 | + pub fn open_key(self) -> impl Future<Output = crate::Result<Cursor<Err>, Err>> { |
| 96 | + let req = match self.source { |
| 97 | + Either::Left(store) => { |
| 98 | + store.open_key_cursor_with_range_and_direction(&self.query, self.direction) |
| 99 | + } |
| 100 | + Either::Right(index) => { |
| 101 | + index.open_key_cursor_with_range_and_direction(&self.query, self.direction) |
| 102 | + } |
| 103 | + }; |
| 104 | + match req { |
| 105 | + Ok(open_req) => Either::Right(Cursor::from(open_req)), |
| 106 | + Err(err) => Either::Left(std::future::ready(Err(map_open_cursor_err(err)))), |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /// Limit the range of the cursor |
| 111 | + /// |
| 112 | + /// Internally, this sets [this property](https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/openCursor#range). |
| 113 | + pub fn range(mut self, range: impl RangeBounds<JsValue>) -> crate::Result<Self, Err> { |
| 114 | + self.query = make_key_range(range)?; |
| 115 | + Ok(self) |
| 116 | + } |
| 117 | + |
| 118 | + /// Define the direction of the cursor |
| 119 | + /// |
| 120 | + /// Internally, this sets [this property](https://developer.mozilla.org/en-US/docs/Web/API/IDBIndex/openCursor#direction). |
| 121 | + pub fn direction(mut self, direction: CursorDirection) -> Self { |
| 122 | + self.direction = direction.to_sys(); |
| 123 | + self |
| 124 | + } |
| 125 | +} |
| 126 | + |
45 | 127 | /// Wrapper for [`IDBCursorWithValue`](https://developer.mozilla.org/en-US/docs/Web/API/IDBCursorWithValue) |
46 | 128 | pub struct Cursor<Err> { |
47 | 129 | sys: Option<IdbCursor>, |
|
0 commit comments