hoodiehq / hoodie-account-client

:dog: Account client API for the browser
https://hoodiehq.github.io/hoodie-account-client
Apache License 2.0
11 stars 24 forks source link

async store #129

Closed gr2m closed 7 years ago

gr2m commented 7 years ago

rebased on #128, closes #127, part of #126

BREAKING CHANGE:

As part of our effort to make Hoodie Client compatible with Service Worker and other environments that do not have access to localStorage, we have to make the Account Client compatible with async store APIs. That means we can’t load the current account state synchronously, so this is no longer be possible:

var account = new Account({
  url: /api
})
if (account.isSignedIn()) {
  sayHi(account.username)
}

Starting with this release, you have to wrap the synchronous methods and properties into account.ready.then()

account.ready.then(function () {
  if (account.isSignedIn()) {
    sayHi(account.username)
  }
})

By default, the account will still use localStorage (via humble-localstorage) to persist its state, but it will now be made asynchronous. In order to use another storage a new options.store argument can be passed to the Account constructor:

var account = new Account({
  url: /api,
  store: {
    set: writeAccountState
    get: getAccountState,
    unset: clearAccountState,
  }
})

All three options.store methods must return promises. store.get must resolve with the persisted account properties or an empty object.

gr2m commented 7 years ago

🛎 ping @hoodiehq/maintainers this PR is ready for review 👀

I know it’s pretty big, but the changes are pretty repetitive. Because of the new asynchronous initialisation nearly all methods had to be touched to either check if the instance is initiated or to make sure the setup promise is resolved before doing the actual task. And nearly all tests had to be adjusted accordingly

Free kitten hugging gifs for you if you give it a look <3