Skip to content

Commit 50be93d

Browse files
committed
chore: adapt for latest `@hoodie/{account,store}-client
1 parent d9e791b commit 50be93d

File tree

3 files changed

+65
-85
lines changed

3 files changed

+65
-85
lines changed

lib/get-api.js

+19-60
Original file line numberDiff line numberDiff line change
@@ -28,73 +28,30 @@ function getApi (state) {
2828
}, state.connectionStatus)
2929
var log = mergeOptionsAndCreate(internals.Log, { prefix: 'hoodie' }, state.log)
3030

31-
var hoodieStore
32-
var api = {
33-
get url () {
34-
return state.url + '/hoodie'
35-
},
36-
get ready () {
37-
if (state.isReady) {
38-
return Promise.resolve(api)
39-
}
40-
41-
return Promise.all([hoodieAccount.ready, hoodieConnectionStatus.ready])
42-
43-
.then(function () {
44-
if (state.isReady) {
45-
return api
46-
}
47-
48-
state.isReady = true
49-
var CustomPouchDB = state.PouchDB.defaults({
31+
var hoodieStore = new internals.Store('store', {
32+
PouchDB: state.PouchDB,
33+
get remote () {
34+
return hoodieAccount.get(['id', 'session.id']).then(function (properties) {
35+
return new state.PouchDB(url + '/store/api/' + encodeURIComponent('user/' + properties.id), {
5036
ajax: {
5137
headers: {
52-
get authorization () {
53-
var session = api.account.get('session')
54-
if (!session) {
55-
return
56-
}
57-
58-
return 'Session ' + session.id
59-
}
38+
authorization: 'Session ' + properties.session.id
6039
}
6140
}
6241
})
63-
var CustomStore = internals.Store.defaults({
64-
PouchDB: CustomPouchDB,
65-
remoteBaseUrl: url + '/store/api'
66-
})
67-
var dbName = 'user/' + hoodieAccount.id
68-
hoodieStore = new CustomStore(dbName)
69-
70-
internals.init(api)
71-
72-
return api
7342
})
74-
},
75-
76-
// core modules
77-
get account () {
78-
if (!state.isReady) {
79-
throw new Error('hoodie.account not yet accessible, wait for hoodie.ready to resolve')
80-
}
43+
}
44+
})
8145

82-
return hoodieAccount
83-
},
84-
get store () {
85-
if (!state.isReady) {
86-
throw new Error('hoodie.store not yet accessible, wait for hoodie.ready to resolve')
87-
}
88-
89-
return hoodieStore
46+
var api = {
47+
get url () {
48+
return state.url + '/hoodie'
9049
},
91-
get connectionStatus () {
92-
if (!state.isReady) {
93-
throw new Error('hoodie.connectionStatus not yet accessible, wait for hoodie.ready to resolve')
94-
}
9550

96-
return hoodieConnectionStatus
97-
},
51+
// core modules
52+
account: hoodieAccount,
53+
store: hoodieStore,
54+
connectionStatus: hoodieConnectionStatus,
9855

9956
// helpers
10057
request: require('./request').bind(this, state),
@@ -108,10 +65,12 @@ function getApi (state) {
10865
trigger: require('./events').trigger.bind(this, state)
10966
}
11067

68+
internals.init(api)
69+
11170
return api
11271
}
11372

114-
function mergeOptionsAndCreate (objectConstructor, defaultOptions, stateOptions) {
73+
function mergeOptionsAndCreate (ObjectConstructor, defaultOptions, stateOptions) {
11574
var options = defaultsDeep(defaultOptions, stateOptions || {})
116-
return objectConstructor(options)
75+
return new ObjectConstructor(options)
11776
}

lib/get-state.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,5 @@ function getState (options) {
1818
emitter: options && options.emitter || new EventEmitter()
1919
}
2020

21-
var state = defaultsDeep(requiredProperties, options)
22-
23-
state.isReady = false
24-
25-
return state
21+
return defaultsDeep(requiredProperties, options)
2622
}

lib/init.js

+45-20
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,30 @@ function init (hoodie) {
66
// on signin. So before the signin happens, we store the user account’s id
77
// and data and store it again after the signin
88
hoodie.account.hook.before('signin', function (options) {
9-
options.beforeSignin = {
10-
accountId: hoodie.account.id
11-
}
12-
return hoodie.store.findAll().then(function (docs) {
13-
options.beforeSignin.docs = docs
9+
return Promise.all([
10+
hoodie.account.get('id'),
11+
hoodie.store.findAll()
12+
]).then(function (results) {
13+
options.beforeSignin = {
14+
accountId: results[0],
15+
docs: results[1]
16+
}
1417
})
1518
})
1619

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

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

2730
.then(function () {
2831
function migrate (doc) {
29-
doc.createdBy = hoodie.account.id
32+
doc.createdBy = account.id
3033
delete doc._rev
3134
return doc
3235
}
@@ -50,28 +53,50 @@ function init (hoodie) {
5053
})
5154
})
5255
hoodie.account.hook.after('signout', function (options) {
53-
return hoodie.store.reset({ name: 'user/' + hoodie.account.id })
56+
return hoodie.store.reset()
5457
})
5558

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

5962
// handle connection status changes
6063
hoodie.connectionStatus.on('disconnect', function () {
61-
if (!hoodie.account.isSignedIn()) {
62-
return
63-
}
64-
hoodie.store.disconnect()
64+
hoodie.account.get('session')
65+
66+
.then(function (session) {
67+
if (session) {
68+
hoodie.store.disconnect()
69+
}
70+
})
6571
})
6672
hoodie.connectionStatus.on('connect', function () {
67-
if (!hoodie.account.isSignedIn()) {
73+
hoodie.account.get('session')
74+
75+
.then(function (session) {
76+
if (session) {
77+
hoodie.store.connect()
78+
}
79+
})
80+
})
81+
82+
hoodie.account.get('session')
83+
84+
.then(function (session) {
85+
// signed out
86+
if (!session) {
87+
return
88+
}
89+
90+
// signed in, but session was invalid
91+
if (session.invalid) {
92+
return
93+
}
94+
95+
// hoodie.connectionStatus.ok is false if there is a connection issue
96+
if (hoodie.connectionStatus.ok === false) {
6897
return
6998
}
70-
hoodie.store.connect()
71-
})
7299

73-
// hoodie.connectionStatus.ok is false if there is a connection issue
74-
if (hoodie.account.isSignedIn() && hoodie.connectionStatus.ok !== false) {
75100
hoodie.store.connect()
76-
}
101+
})
77102
}

0 commit comments

Comments
 (0)