Skip to content

Commit

Permalink
chore: adapt for latest `@hoodie/{account,store}-client
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Mar 9, 2017
1 parent d9e791b commit 50be93d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 85 deletions.
79 changes: 19 additions & 60 deletions lib/get-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,73 +28,30 @@ function getApi (state) {
}, state.connectionStatus)
var log = mergeOptionsAndCreate(internals.Log, { prefix: 'hoodie' }, state.log)

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

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

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

state.isReady = true
var CustomPouchDB = state.PouchDB.defaults({
var hoodieStore = new internals.Store('store', {
PouchDB: state.PouchDB,
get remote () {
return hoodieAccount.get(['id', 'session.id']).then(function (properties) {
return new state.PouchDB(url + '/store/api/' + encodeURIComponent('user/' + properties.id), {
ajax: {
headers: {
get authorization () {
var session = api.account.get('session')
if (!session) {
return
}

return 'Session ' + session.id
}
authorization: 'Session ' + properties.session.id
}
}
})
var CustomStore = internals.Store.defaults({
PouchDB: CustomPouchDB,
remoteBaseUrl: url + '/store/api'
})
var dbName = 'user/' + hoodieAccount.id
hoodieStore = new CustomStore(dbName)

internals.init(api)

return api
})
},

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

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

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

return hoodieConnectionStatus
},
// core modules
account: hoodieAccount,
store: hoodieStore,
connectionStatus: hoodieConnectionStatus,

// helpers
request: require('./request').bind(this, state),
Expand All @@ -108,10 +65,12 @@ function getApi (state) {
trigger: require('./events').trigger.bind(this, state)
}

internals.init(api)

return api
}

function mergeOptionsAndCreate (objectConstructor, defaultOptions, stateOptions) {
function mergeOptionsAndCreate (ObjectConstructor, defaultOptions, stateOptions) {
var options = defaultsDeep(defaultOptions, stateOptions || {})
return objectConstructor(options)
return new ObjectConstructor(options)
}
6 changes: 1 addition & 5 deletions lib/get-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,5 @@ function getState (options) {
emitter: options && options.emitter || new EventEmitter()
}

var state = defaultsDeep(requiredProperties, options)

state.isReady = false

return state
return defaultsDeep(requiredProperties, options)
}
65 changes: 45 additions & 20 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@ function init (hoodie) {
// on signin. So before the signin happens, we store the user account’s id
// and data and store it again after the signin
hoodie.account.hook.before('signin', function (options) {
options.beforeSignin = {
accountId: hoodie.account.id
}
return hoodie.store.findAll().then(function (docs) {
options.beforeSignin.docs = docs
return Promise.all([
hoodie.account.get('id'),
hoodie.store.findAll()
]).then(function (results) {
options.beforeSignin = {
accountId: results[0],
docs: results[1]
}
})
})

hoodie.account.hook.after('signin', function (session, options) {
hoodie.account.hook.after('signin', function (account, options) {
// when signing in to a newly created account, the account.id does not
// change. The same is true when the user changed their username. In both
// cases there is no need to migrate local data
if (options.beforeSignin.accountId === hoodie.account.id) {
if (options.beforeSignin.accountId === account.id) {
return hoodie.store.connect()
}

return hoodie.store.reset({ name: 'user/' + hoodie.account.id })
return hoodie.store.reset()

.then(function () {
function migrate (doc) {
doc.createdBy = hoodie.account.id
doc.createdBy = account.id
delete doc._rev
return doc
}
Expand All @@ -50,28 +53,50 @@ function init (hoodie) {
})
})
hoodie.account.hook.after('signout', function (options) {
return hoodie.store.reset({ name: 'user/' + hoodie.account.id })
return hoodie.store.reset()
})

hoodie.account.on('unauthenticate', hoodie.store.disconnect)
hoodie.account.on('reauthenticate', hoodie.store.connect)

// handle connection status changes
hoodie.connectionStatus.on('disconnect', function () {
if (!hoodie.account.isSignedIn()) {
return
}
hoodie.store.disconnect()
hoodie.account.get('session')

.then(function (session) {
if (session) {
hoodie.store.disconnect()
}
})
})
hoodie.connectionStatus.on('connect', function () {
if (!hoodie.account.isSignedIn()) {
hoodie.account.get('session')

.then(function (session) {
if (session) {
hoodie.store.connect()
}
})
})

hoodie.account.get('session')

.then(function (session) {
// signed out
if (!session) {
return
}

// signed in, but session was invalid
if (session.invalid) {
return
}

// hoodie.connectionStatus.ok is false if there is a connection issue
if (hoodie.connectionStatus.ok === false) {
return
}
hoodie.store.connect()
})

// hoodie.connectionStatus.ok is false if there is a connection issue
if (hoodie.account.isSignedIn() && hoodie.connectionStatus.ok !== false) {
hoodie.store.connect()
}
})
}

0 comments on commit 50be93d

Please sign in to comment.