Skip to content

Commit

Permalink
fix: hoodie.ready race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Jan 10, 2017
1 parent 71bb607 commit 67d2efb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
17 changes: 12 additions & 5 deletions lib/get-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,23 @@ function getApi (state) {
var log = mergeOptionsAndCreate(internals.Log, { prefix: 'hoodie' }, state.log)

var hoodieStore
var isReady = false
var api = {
get url () {
return state.url + '/hoodie'
},
get ready () {
if (state.isReady) {
return api
}

return Promise.all([hoodieAccount.ready, hoodieConnectionStatus.ready])

.then(function () {
isReady = true
if (state.isReady) {
return api
}

state.isReady = true
var CustomPouchDB = state.PouchDB.defaults({
ajax: {
headers: {
Expand Down Expand Up @@ -68,21 +75,21 @@ function getApi (state) {

// core modules
get account () {
if (!isReady) {
if (!state.isReady) {
throw new Error('hoodie.account not yet accessible, wait for hoodie.ready to resolve')
}

return hoodieAccount
},
get store () {
if (!isReady) {
if (!state.isReady) {
throw new Error('hoodie.store not yet accessible, wait for hoodie.ready to resolve')
}

return hoodieStore
},
get connectionStatus () {
if (!isReady) {
if (!state.isReady) {
throw new Error('hoodie.connectionStatus not yet accessible, wait for hoodie.ready to resolve')
}

Expand Down
6 changes: 5 additions & 1 deletion lib/get-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,9 @@ function getState (options) {
emitter: options && options.emitter || new EventEmitter()
}

return defaultsDeep(requiredProperties, options)
var state = defaultsDeep(requiredProperties, options)

state.isReady = false

return state
}

0 comments on commit 67d2efb

Please sign in to comment.