Skip to content

Commit

Permalink
Fix TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyboyko0791 committed Feb 24, 2023
1 parent 4deacf5 commit 3b4a6e0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 42 deletions.
24 changes: 11 additions & 13 deletions mm2src/mm2_db/src/indexed_db/drivers/cursor/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,33 +168,31 @@ impl CursorBoundValue {
}
}

/// TODO rename to `CursorAction`.
#[derive(Debug, PartialEq)]
pub enum CollectCursorAction {
pub enum CursorAction {
Continue,
ContinueWithValue(JsValue),
Stop,
}

/// TODO rename to `IterItemAction`.
#[derive(Debug, PartialEq)]
pub enum CollectItemAction {
pub enum CursorItemAction {
Include,
Skip,
}

pub trait CursorDriverImpl: Sized {
fn key_range(&self) -> CursorResult<Option<IdbKeyRange>>;

fn on_iteration(&mut self, key: JsValue) -> CursorResult<(CollectItemAction, CollectCursorAction)>;
fn on_iteration(&mut self, key: JsValue) -> CursorResult<(CursorItemAction, CursorAction)>;
}

pub(crate) struct CursorDriver {
/// An actual cursor implementation.
inner: IdbCursorEnum,
cursor_request: IdbRequest,
cursor_item_rx: mpsc::Receiver<Result<JsValue, JsValue>>,
/// Whether we got `CollectCursorAction::Stop` at the last iteration or not.
/// Whether we got `CursorAction::Stop` at the last iteration or not.
stopped: bool,
/// We need to hold the closures in memory till `cursor` exists.
_onsuccess_closure: Closure<dyn FnMut(JsValue)>,
Expand Down Expand Up @@ -244,7 +242,7 @@ impl CursorDriver {

pub(crate) async fn next(&mut self) -> CursorResult<Option<(ItemId, Json)>> {
loop {
// Check if we got `CollectCursorAction::Stop` at the last iteration.
// Check if we got `CursorAction::Stop` at the last iteration.
if self.stopped {
return Ok(None);
}
Expand Down Expand Up @@ -285,10 +283,10 @@ impl CursorDriver {
let (item_action, cursor_action) = self.inner.on_iteration(key)?;

match cursor_action {
CollectCursorAction::Continue => cursor.continue_().map_to_mm(|e| CursorError::AdvanceError {
CursorAction::Continue => cursor.continue_().map_to_mm(|e| CursorError::AdvanceError {
description: stringify_js_error(&e),
})?,
CollectCursorAction::ContinueWithValue(next_value) => {
CursorAction::ContinueWithValue(next_value) => {
cursor
.continue_with_key(&next_value)
.map_to_mm(|e| CursorError::AdvanceError {
Expand All @@ -299,13 +297,13 @@ impl CursorDriver {
// Here we set the `stopped` flag so we return `Ok(None)` at the next iteration immediately.
// This is required because `item_action` can be `CollectItemAction::Include`,
// and at this iteration we will return `Ok(Some)`.
CollectCursorAction::Stop => self.stopped = true,
CursorAction::Stop => self.stopped = true,
}

match item_action {
CollectItemAction::Include => return Ok(Some(item.into_pair())),
CursorItemAction::Include => return Ok(Some(item.into_pair())),
// Try to fetch the next item.
CollectItemAction::Skip => (),
CursorItemAction::Skip => (),
}
}
}
Expand Down Expand Up @@ -366,7 +364,7 @@ impl CursorDriverImpl for IdbCursorEnum {
}
}

fn on_iteration(&mut self, key: JsValue) -> CursorResult<(CollectItemAction, CollectCursorAction)> {
fn on_iteration(&mut self, key: JsValue) -> CursorResult<(CursorItemAction, CursorAction)> {
match self {
IdbCursorEnum::Empty(empty) => empty.on_iteration(key),
IdbCursorEnum::SingleKey(single) => single.on_iteration(key),
Expand Down
6 changes: 3 additions & 3 deletions mm2src/mm2_db/src/indexed_db/drivers/cursor/empty_cursor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{CollectCursorAction, CollectItemAction, CursorDriverImpl, CursorResult};
use super::{CursorAction, CursorDriverImpl, CursorItemAction, CursorResult};
use wasm_bindgen::prelude::*;
use web_sys::IdbKeyRange;

Expand All @@ -8,7 +8,7 @@ pub struct IdbEmptyCursor;
impl CursorDriverImpl for IdbEmptyCursor {
fn key_range(&self) -> CursorResult<Option<IdbKeyRange>> { Ok(None) }

fn on_iteration(&mut self, _key: JsValue) -> CursorResult<(CollectItemAction, CollectCursorAction)> {
Ok((CollectItemAction::Include, CollectCursorAction::Continue))
fn on_iteration(&mut self, _key: JsValue) -> CursorResult<(CursorItemAction, CursorAction)> {
Ok((CursorItemAction::Include, CursorAction::Continue))
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{index_key_as_array, CollectCursorAction, CollectItemAction, CursorBoundValue, CursorDriverImpl,
CursorError, CursorResult};
use super::{index_key_as_array, CursorAction, CursorBoundValue, CursorDriverImpl, CursorError, CursorItemAction,
CursorResult};
use common::{deserialize_from_js, serialize_to_js, stringify_js_error};
use js_sys::Array;
use mm2_err_handle::prelude::*;
Expand Down Expand Up @@ -108,7 +108,7 @@ impl CursorDriverImpl for IdbMultiKeyBoundCursor {
/// The range `IDBKeyRange.bound([2,2], [4,4])` includes values like `[3,0]` and `[3,5]` as `[2,2] < [3,0] < [3,5] < [4,4]`,
/// so we need to do additional filtering.
/// For more information on why it's required, see https://stackoverflow.com/a/32976384.
fn on_iteration(&mut self, index_key: JsValue) -> CursorResult<(CollectItemAction, CollectCursorAction)> {
fn on_iteration(&mut self, index_key: JsValue) -> CursorResult<(CursorItemAction, CursorAction)> {
let index_keys_js_array = index_key_as_array(index_key)?;
let index_keys: Vec<Json> = index_keys_js_array
.iter()
Expand Down Expand Up @@ -157,8 +157,8 @@ impl CursorDriverImpl for IdbMultiKeyBoundCursor {

return Ok((
// the `actual_index_value` is not in our expected bounds
CollectItemAction::Skip,
CollectCursorAction::ContinueWithValue(new_index.into()),
CursorItemAction::Skip,
CursorAction::ContinueWithValue(new_index.into()),
));
}
if &actual_index_value > upper_bound {
Expand All @@ -177,13 +177,13 @@ impl CursorDriverImpl for IdbMultiKeyBoundCursor {

return Ok((
// the `actual_index_value` is not in our expected bounds
CollectItemAction::Skip,
CollectCursorAction::ContinueWithValue(new_index.into()),
CursorItemAction::Skip,
CursorAction::ContinueWithValue(new_index.into()),
));
}

// otherwise there is no an index greater than actual `index`, stop the cursor
return Ok((CollectItemAction::Skip, CollectCursorAction::Stop));
return Ok((CursorItemAction::Skip, CursorAction::Stop));
}

let increased_index_key = actual_index_value.next();
Expand All @@ -194,7 +194,7 @@ impl CursorDriverImpl for IdbMultiKeyBoundCursor {
idx_in_index += 1;
idx_in_bounds += 1;
}
Ok((CollectItemAction::Include, CollectCursorAction::Continue))
Ok((CursorItemAction::Include, CursorAction::Continue))
}
}

Expand All @@ -212,7 +212,7 @@ mod tests {
let result = cursor.on_iteration(input_index_js_value);
assert_eq!(
result,
Ok((CollectItemAction::Include, CollectCursorAction::Continue)),
Ok((CursorItemAction::Include, CursorAction::Continue)),
"'{}' index is expected to be in a bound",
input_index
);
Expand All @@ -230,15 +230,12 @@ mod tests {
.expect(&format!("Error due to the index '{:?}'", input_index));

let actual_next: Json = match cursor_action {
CollectCursorAction::ContinueWithValue(next_index_js_value) => {
CursorAction::ContinueWithValue(next_index_js_value) => {
deserialize_from_js(next_index_js_value).expect("Error deserializing next index}")
},
action => panic!(
"Expected 'CollectCursorAction::ContinueWithValue', found '{:?}'",
action
),
action => panic!("Expected 'CursorAction::ContinueWithValue', found '{:?}'", action),
};
assert_eq!(item_action, CollectItemAction::Skip);
assert_eq!(item_action, CursorItemAction::Skip);
assert_eq!(actual_next, expected_next);
}
}
Expand All @@ -250,7 +247,7 @@ mod tests {
let result = cursor.on_iteration(input_index_js_value);
assert_eq!(
result,
Ok((CollectItemAction::Skip, CollectCursorAction::Stop)),
Ok((CursorItemAction::Skip, CursorAction::Stop)),
"'{}' index is expected to be out of bound",
input_index
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{CollectCursorAction, CollectItemAction, CursorDriverImpl, CursorError, CursorResult};
use super::{CursorAction, CursorDriverImpl, CursorError, CursorItemAction, CursorResult};
use common::{serialize_to_js, stringify_js_error};
use js_sys::Array;
use mm2_err_handle::prelude::*;
Expand Down Expand Up @@ -36,7 +36,7 @@ impl CursorDriverImpl for IdbMultiKeyCursor {
Ok(Some(key_range))
}

fn on_iteration(&mut self, _key: JsValue) -> CursorResult<(CollectItemAction, CollectCursorAction)> {
Ok((CollectItemAction::Include, CollectCursorAction::Continue))
fn on_iteration(&mut self, _key: JsValue) -> CursorResult<(CursorItemAction, CursorAction)> {
Ok((CursorItemAction::Include, CursorAction::Continue))
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{CollectCursorAction, CollectItemAction, CursorBoundValue, CursorDriverImpl, CursorError, CursorResult};
use super::{CursorAction, CursorBoundValue, CursorDriverImpl, CursorError, CursorItemAction, CursorResult};
use common::{log::warn, stringify_js_error};
use mm2_err_handle::prelude::*;
use wasm_bindgen::prelude::*;
Expand Down Expand Up @@ -56,7 +56,7 @@ impl CursorDriverImpl for IdbSingleKeyBoundCursor {
Ok(Some(key_range))
}

fn on_iteration(&mut self, _key: JsValue) -> CursorResult<(CollectItemAction, CollectCursorAction)> {
Ok((CollectItemAction::Include, CollectCursorAction::Continue))
fn on_iteration(&mut self, _key: JsValue) -> CursorResult<(CursorItemAction, CursorAction)> {
Ok((CursorItemAction::Include, CursorAction::Continue))
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{CollectCursorAction, CollectItemAction, CursorDriverImpl, CursorError, CursorResult};
use super::{CursorAction, CursorDriverImpl, CursorError, CursorItemAction, CursorResult};
use common::{serialize_to_js, stringify_js_error};
use mm2_err_handle::prelude::*;
use serde_json::Value as Json;
Expand Down Expand Up @@ -38,7 +38,7 @@ impl CursorDriverImpl for IdbSingleKeyCursor {
Ok(Some(key_range))
}

fn on_iteration(&mut self, _key: JsValue) -> CursorResult<(CollectItemAction, CollectCursorAction)> {
Ok((CollectItemAction::Include, CollectCursorAction::Continue))
fn on_iteration(&mut self, _key: JsValue) -> CursorResult<(CursorItemAction, CursorAction)> {
Ok((CursorItemAction::Include, CursorAction::Continue))
}
}

0 comments on commit 3b4a6e0

Please sign in to comment.