Coder Social home page Coder Social logo

Comments (10)

smalluban avatar smalluban commented on June 26, 2024

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.

cyruseuros avatar cyruseuros commented on June 26, 2024

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.

smalluban avatar smalluban commented on June 26, 2024

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.

cyruseuros avatar cyruseuros commented on June 26, 2024

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.

smalluban avatar smalluban commented on June 26, 2024

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.

cyruseuros avatar cyruseuros commented on June 26, 2024

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.

smalluban avatar smalluban commented on June 26, 2024

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.

cyruseuros avatar cyruseuros commented on June 26, 2024

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.

smalluban avatar smalluban commented on June 26, 2024

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.

cyruseuros avatar cyruseuros commented on June 26, 2024

got it, thanks!

from hybrids.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.