hoodiehq / hoodie-client

:dog: Client API for the Hoodie server
Apache License 2.0
34 stars 25 forks source link

[WIP] hoodie.reset #32

Closed danreeves closed 8 years ago

danreeves commented 8 years ago

Probably dependant on https://github.com/hoodiehq/hoodie-client-store/issues/53

gr2m commented 8 years ago

I think we can make this much simpler. I wouldn’t do anything with self.account or self.store, I’d only emit the event (and allow event handlers to hook into it) and then unset the hoodie.id at the end.

I’d then listen to the reset event when initialsing the store and account module as part of the glue code.

So for the reset method, something in this direction:

module.exports = reset

var unsetId = require('./id').unset

function reset (state) {
  var promise = Promise.resolve()

  this.trigger('reset', {promise: promise})

  return promise.then(function () {
    unsetId(state)
  })
}

And then in the constructor, we would do things like this

// init store module ...
hoodie.on('reset', function (options) {
  options.promise = options.promise.then(hoodie.store.clear)
})
// init account module ...
hoodie.on('reset', function (options) {
  options.promise = options.promise.then(hoodie.account.signOut)
})

Does that make sense?

danreeves commented 8 years ago

Updated with your suggestions and also added tests and docs.

Doesn't work 100% though. There's a race condition between resolving the reset and the store being cleared (and any other plugin doing async resets). e.g.

hoodie.store.add({
    test: 'store'
})
.then(hoodie.store.findAll)
.then(all => console.log('1', all)) // 1 [Object]
.then(hoodie.reset)
.then(hoodie.store.findAll)
.then(all => console.log('2', all)) // 2 [Object]
.then(hoodie.store.findAll)
.then(all => console.log('3', all)) // 3 []

I'll sleep on the issue and see if I can think of anything

gr2m commented 8 years ago

put on hold for now, see #12. I’ll reopen later