Skip to content

Commit

Permalink
fix: cross-module initialisations
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Apr 15, 2016
1 parent 4d3130e commit 5c41c15
Showing 1 changed file with 78 additions and 14 deletions.
92 changes: 78 additions & 14 deletions lib/init.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,87 @@
module.exports = init

function init (hoodie) {
var account = hoodie.account
var store = hoodie.store

// glue code
account.on('signout', function () {
// TODO: prevent data loss on sign out:
// https://github.com/hoodiehq/hoodie-client/issues/22
store.reset({ name: hoodie.account.id })
// In order to prevent data loss, we want to move all data that has been
// created without an account (e.g. while offline) to the user’s account
// on signin. So before the signin happens, we temporarily store it in
// a variable (dataFromAccountBeforeSignin) and add it to the store again
// in the post:signin hook below
var dataFromAccountBeforeSignin
var accountIdBeforeSignIn
hoodie.account.on('pre:signin', function (options) {
options.hooks.push(function () {
accountIdBeforeSignIn = hoodie.account.id
return hoodie.store.findAll().then(function (objects) {
dataFromAccountBeforeSignin = objects
})
})
})

account.on('signin', function () {
store
.reset({ name: hoodie.account.id })
.then(store.connect)
hoodie.account.on('post:signin', function (options) {
options.hooks.push(function () {
// when signing in to a newly created account, the account.id
// does not change, so there is no need to clear the local
// store and to migrate data
if (accountIdBeforeSignIn === hoodie.account.id) {
dataFromAccountBeforeSignin = null
return hoodie.store.connect()
}

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

.catch(function (error) {
dataFromAccountBeforeSignin = null
throw error
})

.then(function () {
var migratedDataFromAccountBeforeSignIn = dataFromAccountBeforeSignin.map(function (object) {
object.createdBy = hoodie.account.id
delete object._rev
return object
})
dataFromAccountBeforeSignin = null
return hoodie.store.add(migratedDataFromAccountBeforeSignIn)
})

.then(function () {
return hoodie.store.connect()
})
})
})

// see https://github.com/hoodiehq/hoodie-client-account/issues/65
// for info on the internal pre:* & post:* events
hoodie.account.on('pre:signout', function (options) {
options.hooks.push(function () {
return hoodie.store.push()
})
})
hoodie.account.on('post:signout', function (options) {
options.hooks.push(function () {
return hoodie.store.reset({ name: 'user/' + hoodie.account.id })
})
})

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.connectionStatus.on('connect', function () {
if (!hoodie.account.isSignedIn()) {
return
}
hoodie.store.connect()
})

if (account.isSignedIn()) {
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 5c41c15

Please sign in to comment.