Comments (10)
The offline
feature of the store allows to keep data in local storage for outage access. However, it does not force you to set your data source as an external API endpoint. You can combine whatever you want defining get
and set
methods, even another local storage key (in this case it is pointless).
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).
from hybrids.
No doubt that offline:
is useful - it basically gives you stale-while-revalidate without a service worker (if you don't really need one) which is awesome.
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?
from hybrids.
Ok, I get it. The offline
is indeed a localStorage implementation, but the main reason why I put it inside the library, is that it needs internal APIs to work best. It is connected with functions that are not meant to be public.
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"),
};
from hybrids.
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 id
is an object identifier like e.g. {foo: "bar"}
?
from hybrids.
Here you have what the docs say:
The value for the identifier can be a string, or an object record (a map of primitive values, which is normalized and serialized to string representation). The object record allows identifying models by multiple parameters rather than on string id.
More info here https://hybrids.js.org/#/store/model?id=singleton-amp-enumerables
from hybrids.
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?
from hybrids.
Yes, it only supports flat records. If you pass a "real" object, which does not meet the requirements, it should throw an error.
from hybrids.
Amazing, thanks for the clarification! Closing the issue but just to check my understanding: to make store.set(instanceObj, values)
work, one needs to implement a custom set
function under [store.connect]
that maps object instances to IDs (at the very least by getting the instanceObj.id
property). I.e. inside the set
function, you need to check whether you got an object or string ID at runtime?
from hybrids.
The store.set
serialize id (to a flat record) before calling [store.connect].set
. If you pass object record, you don't have to worry, the set method will take a record, but the memory cache will use strigified version.
from hybrids.
got it, thanks!
from hybrids.
Related Issues (20)
- I can't write a polymorphic model definition for GeoJson objects HOT 7
- Content is not updated when creating a model HOT 2
- Using "store([Model])" causes a TypeScript error HOT 18
- Clearing a draft causes a TypeScript error. HOT 7
- "children" fabric causes TypeScript error HOT 4
- using 'host.render()' and 'host.content()' causes a TypeScript error HOT 4
- The "store(Model, { id: value })" descriptor does not resolve "id" correctly to the model instance. HOT 3
- The "store(Model, { id:value })" descriptor does not work correctly. HOT 2
- Add a helper for assigning component properties HOT 3
- Dynamic columns not showing when render a html table HOT 4
- '"hybrids"' has no exported member named 'property'. Did you mean 'Property'? HOT 8
- Add support for "TypeScript v5" HOT 3
- I can't create an arbitrary number of nested forms: HOT 6
- Small Type Inferencing Issue with Property<E, V> HOT 4
- Cannot pass an object to the API via the "list" method HOT 7
- version 8.2.17 backwards compatibility is broken! HOT 9
- Expand the functionality of drafts
- sync() with nested models is broken HOT 4
- Allow storing an object in a component property HOT 4
- Add partial loading of model data HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hybrids.