-
Notifications
You must be signed in to change notification settings - Fork 1
Conversation
- Wrap `Storage` in `SafeStorage` so it fallbacks to an InMemory implementation - Handle `null` `getItem` and map to `NotFoundError` - Handle `setItem` errors and map to `FailedError` - Handle values/keys length mismatch and throw `InvalidArgumentError`
…rk with any `Storage` implementation (e.g. local or session)
I like the idea. But, is this safe-switch responsibility of the DataSource or is it responsibility of the Repository? Who's the one to decide that In Memory is an acceptable fallback? |
I don't know which is the correct approach here. My guess was that it's safer to do it implicit, so we've less bugs. Otherwise we need to be conscious and be explicit about these issues (which is a rare consideration, as these are edge cases). In the context in which these issues arise, I think it's OK to default to an in-memory |
Maybe we could get input from the mobile team. They might have similar cases. What do you think @franmontiel ? |
On mobile I think we do not have any particular case to highlight but here is my opinion. Storage is the framework-specific implementation and extending it to solve a framework-specific problem in my opinion is perfectly fine. DataSourceStorage can either use the normal Storage or SafeStorage version so that change is decoupled from the DataSource itself. With this approach, you can use another generic repository on top of DataSourceStorage. If you create a repository this won't be possible. From an architectural point of view, I think it is not problematic because even repositories responsibility is to be the "business logic" for the data layer (so it makes sense that any decision on where to fetch data belong to them) in this particular case I see this as a workaround for certain undesired platform behavior. One last thing that I would improve is to make super clear in the documentation that the SafeStorage does not guarantee that the data is really stored on local storage. |
Just to clarify:
Decouple/couple
|
@doup I think your suggestion of explicitly enabling the "safe mode" depending on a flag parameter solves the issue I've raised (the repository still can choose the behavior). Another minor suggestion: instead of naming the parameter |
This is a breaking change, the fix is easy though:
NotFoundError
instead of returningnull
LocalStorageDataSource
toStorageDataSource
Storage
implementation as first parameter, usuallylocalStorage
orsessionStorage
.From:
To:
Change List
LocalStorageDataSource
toStorageDataSource
, as this can be used with anyStorage
implementation (e.g.LocalStorage
,SessionStorage
…)SafeStorage
helper (which fallbacks to an in-memory implementation) to handle incognito issues, see: https://michalzalecki.com/why-using-localStorage-directly-is-a-bad-idea/null
getItem
value and throwNotFoundError
setItem
errors and throwFailedError
InvalidArgumentError