hoodiehq / camp

:circus_tent: Welcome to Hoodie Camp!
https://hoodie.camp
Apache License 2.0
99 stars 55 forks source link

drop localStorage usage in Hoodie Client #101

Closed gr2m closed 7 years ago

gr2m commented 7 years ago

❓ The Motivation

When thinking about Hoodie’s APIs, we always tried to come up with the most simple ones without worrying about complexities in implementation, we call this Dreamcode. One aspect of that simplicity was that some of the API is synchronous, for example

We want to keep these APIs, but in order to make that possible, we have to introduce an asynchronous initialisation, similar to jQuery’s ready or serviceWorker.ready, as we realised that we no longer can rely on localStorage, which is the only synchronous API to read/write data.

Our problem with localStorage is that it is not accessible in ServiceWorker. But as we want Hoodie to be a great solution for Offline First applications, we have to make ServiceWorker support a priority.

So instead of this:

<script src="hoodie/client.js"></script>
<script>
if (hoodie.account.isSignedIn()) {
  renderDashboard()
} else {
  renderWelcome()
}
</script>

We will from now on have to do

<script src="hoodie/client.js"></script>
<script>
hoodie.ready.then(function () {
  if (hoodie.account.isSignedIn()) {
    renderDashboard()
  } else {
    renderWelcome()
  }
})
</script>

:clipboard: Steps

Later