From 67d2efb9491f0b57d229c43eed326388ffe40c9e Mon Sep 17 00:00:00 2001 From: Gregor Date: Mon, 9 Jan 2017 16:06:04 -0800 Subject: [PATCH] fix: `hoodie.ready` race condition --- lib/get-api.js | 17 ++++++++++++----- lib/get-state.js | 6 +++++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/get-api.js b/lib/get-api.js index 4a5fbbb..3ea0612 100644 --- a/lib/get-api.js +++ b/lib/get-api.js @@ -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: { @@ -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') } diff --git a/lib/get-state.js b/lib/get-state.js index adffb55..888cb34 100644 --- a/lib/get-state.js +++ b/lib/get-state.js @@ -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 }