-
Notifications
You must be signed in to change notification settings - Fork 86
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
Storage-first/storage-only option for [store.connect] #210
Comments
The By the idea, it is not there to give you another source, but this is only another cache layer, which holds data automatically. As a bonus, it can help even when a user is online, as the data is returned immediately (and it's fetched in the same time). |
No doubt that I just feel like there is a lot of nice logic that can be re-used for another localStorage-only source which I think would be exceptionally frequently used. It would have unique challenges (i.e. how to store nested models - likely by id-based refs) that probably shouldn't be implemented by every application. I guess my question is does this belong in hybrids core or should someone (probably me) write it as a plugin? |
Ok, I get it. The I think it should be straightforward to make a simplified example of LC source API "plugin" (I assume that It would be in the wild if hybrids had a bigger community): function localStorage(key) {
const models = JSON.parse(localStorage.getItem(key) || "{}");
return {
get: (id) => models[id],
set(id, values) {
models[id] = values;
localStorage.setItem(key, JSON.strinfigy(models));
return values;
},
};
}
const Model = {
...,
[store.connect]: localStorage("models"),
}; |
I see - thanks for the help! Will probably hack something like this together with a bit of indexedDB flair once i'm done building my first app in hybrids to figure it out a bit more. Out of curiosity, how would this example handle an instance when the |
Here you have what the docs say:
More info here https://hybrids.js.org/#/store/model?id=singleton-amp-enumerables |
Understood - but what i meant to clarify is that the implementation above would break if you actually tried to pass in an object right? I.e. there is no magic the framework does to translate said object to an id it might be in localStorage under? |
Yes, it only supports flat records. If you pass a "real" object, which does not meet the requirements, it should throw an error. |
Amazing, thanks for the clarification! Closing the issue but just to check my understanding: to make |
The |
got it, thanks! |
The
offline:
key basically implements a network-first strategy. Looking at the code though, it feels like a substantial chunk can be re-used to implement a more generic/robust version of this suggestion.Worth exploring or out of scope for the library?
The text was updated successfully, but these errors were encountered: