-
Notifications
You must be signed in to change notification settings - Fork 157
Updating the local storage interface #386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating the local storage interface #386
Conversation
e8b2694
to
fa0de25
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first code review round complete :)
src/browser/service/storage.rs
Outdated
.local_storage() | ||
.ok() | ||
.flatten() | ||
web_sys::window()?.local_storage().ok().flatten() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use Seed's window()
:
Lines 27 to 29 in 4552e30
pub fn window() -> web_sys::Window { | |
web_sys::window().expect("Can't find the global Window") | |
} |
prelude
.
src/browser/service/storage.rs
Outdated
} | ||
|
||
/// Create a new store, from a serializable data structure. | ||
pub fn store_data<T>(storage: &Storage, name: &str, data: &T) | ||
/// Clear all data in local storage |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to rename the file and methods + types to local_storage
/ LocalStorage
where it makes sense?
It looks like this module only handle local storage
, but Storage
is general interface for both local storage
and session storage
. So it will become mess once we add module for session storage
and want to write abstraction into module storage
...
Or feel free to resolve the "session storage - local storage - storage problem" in another way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you decide to choose renaming storage.rs
to local_storage.rs
, include also the old storage.rs
with deprecated members so we don't introduce breaking changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm 100% on the rename. Will do
@@ -12,33 +12,75 @@ pub type Storage = web_sys::Storage; | |||
|
|||
#[allow(clippy::module_name_repetitions)] | |||
pub fn get_storage() -> Option<Storage> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When it fails? - Is Option
ok or should we use Result
or panic
?
src/browser/service/storage.rs
Outdated
pub fn store_data<T>(storage: &Storage, name: &str, data: &T) | ||
/// Clear all data in local storage | ||
pub fn clear(storage: &Storage) { | ||
let _ = storage.clear(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please don't add silent fails - results or panics
src/browser/service/storage.rs
Outdated
/// All of the keys in local storage | ||
pub fn keys(storage: &Storage) -> Vec<String> { | ||
let mut keys = vec![]; | ||
let length = storage.length().unwrap_or(0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it make sense to continue when you can't get storage length? - Isn't unwrap_or(0)
another silent fail? Let's fail fast. Or if it does make sense, we can use also .unwrap_or_default()
.
src/browser/service/storage.rs
Outdated
} | ||
|
||
/// All of the keys in local storage | ||
pub fn keys(storage: &Storage) -> Vec<String> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to return Iterator
instead?
src/browser/service/storage.rs
Outdated
let mut keys = vec![]; | ||
let length = storage.length().unwrap_or(0); | ||
for index in 0..length { | ||
if let Ok(Some(key)) = storage.key(index) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another silent fail?
src/browser/service/storage.rs
Outdated
/// Things that can go wrong when trying to load data from local storage. | ||
pub enum LoadError { | ||
/// Could not connect to local storage. | ||
CouldNotConnect, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about ConnectionError
to make it consistent with DecodeError
?
src/browser/service/storage.rs
Outdated
|
||
/// Removes key and associated data from local storage | ||
pub fn remove_item(storage: &Storage, key: &str) { | ||
let _ = storage.remove_item(key); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Silent fail.
src/browser/service/storage.rs
Outdated
pub enum SaveError { | ||
/// The browser denied saving to local storage. Usually because the storage is full. | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/API/Storage/setItem#Exceptions | ||
CouldNotSave, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SaveError
/ SetError
? Also we can save also exception content - SetError(JsValue)
(applicable to all errors).
And please run |
src/browser/service/storage.rs
Outdated
/// Things that can go wrong when trying to save data to local storage. | ||
pub enum SaveError { | ||
/// The browser denied saving to local storage. Usually because the storage is full. | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/API/Storage/setItem#Exceptions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change it to something like [MDN - Storage/setItem Exceptions](..
please
fa0de25
to
7ebd6a3
Compare
Thinking about this a bit more, it doesn't seem like there's much to offer on top of the existing |
I try to use
Good example is Fetch API (#353) - it's de-facto only a slim wrapper but the difference for DX and reliability is significant. (And it's also hard to write it well - it's 3rd complete refactor). |
Righteous, thanks for taking the time to explain that! All good reasons 👍🤙 |
1b4f2b6
to
ea86a7d
Compare
* remove panics * support both local and session storage * provide detailed error conditions * provide a fuller interface
ea86a7d
to
412236d
Compare
@itsgreggreg Is it ready for code review? |
Closing in favor of #423. |
name
tokey
in function parametersHey guys this was something I ran into when writing an application that used local storage and I figured I'd take a stab at fixing it and cleaning up / extending the API a little. This does change the API so examples should probably be updated.
Anyway I figured I'd post what I've got here and let y'all comment or close this PR as you see fit. Happy to get it working up to the correct standards if you point me in the right direction!