-
-
Notifications
You must be signed in to change notification settings - Fork 219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(#9544): add offline freetext search indexes #9661
base: master
Are you sure you want to change the base?
Changes from 7 commits
e270148
710fdb0
c2e3122
def8409
eab8896
24f4869
54981cc
8c01ca1
6ee1020
cb6abd9
02878be
471bf18
c0b3081
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This bootstrapper code gets run during the main initialization of the app (before any of the Angular code spins up. This bootstrapping gets run during initial login and also when reloading the app (e.g. when the webapp-code/app-settings/ddocs change). So, any time in the future when we make updates/additions/removals of views here, we can be confident that everything will be initialized properly in the local db when the app reloads.
jkuester marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
const utils = require('./utils'); | ||
const purger = require('./purger'); | ||
const initialReplicationLib = require('./initial-replication'); | ||
const offlineDdocs = require('./offline-ddocs'); | ||
|
||
const ONLINE_ROLE = 'mm-online'; | ||
|
||
|
@@ -84,7 +85,7 @@ | |
}; | ||
|
||
/* pouch db set up function */ | ||
module.exports = (POUCHDB_OPTIONS) => { | ||
module.exports = async (POUCHDB_OPTIONS) => { | ||
|
||
const dbInfo = getDbInfo(); | ||
const userCtx = getUserCtx(); | ||
|
@@ -108,58 +109,50 @@ | |
|
||
const localMetaDb = window.PouchDB(getLocalMetaDbName(dbInfo, userCtx.name), POUCHDB_OPTIONS.local); | ||
|
||
return Promise | ||
.all([ | ||
initialReplicationLib.isReplicationNeeded(localDb, userCtx), | ||
swRegistration, | ||
setReplicationId(POUCHDB_OPTIONS, localDb) | ||
]) | ||
.then(([isInitialReplicationNeeded]) => { | ||
utils.setOptions(POUCHDB_OPTIONS); | ||
|
||
if (isInitialReplicationNeeded) { | ||
const replicationStarted = performance.now(); | ||
// Polling the document count from the db. | ||
return initialReplicationLib | ||
.replicate(remoteDb, localDb) | ||
.then(() => initialReplicationLib.isReplicationNeeded(localDb, userCtx)) | ||
.then(isReplicationStillNeeded => { | ||
if (isReplicationStillNeeded) { | ||
throw new Error('Initial replication failed'); | ||
} | ||
}) | ||
.then(() => window.startupTimes.replication = performance.now() - replicationStarted); | ||
} | ||
}) | ||
.then(() => { | ||
const purgeMetaStarted = performance.now(); | ||
return purger | ||
.purgeMeta(localMetaDb) | ||
.on('should-purge', shouldPurge => window.startupTimes.purgingMeta = shouldPurge) | ||
.on('start', () => setUiStatus('PURGE_META')) | ||
.on('done', () => window.startupTimes.purgeMeta = performance.now() - purgeMetaStarted) | ||
.catch(err => { | ||
console.error('Error attempting to purge meta db - continuing', err); | ||
window.startupTimes.purgingMetaFailed = err.message; | ||
}); | ||
}) | ||
.then(() => setUiStatus('STARTING_APP')) | ||
.catch(err => err) | ||
.then(err => { | ||
localDb.close(); | ||
remoteDb.close(); | ||
localMetaDb.close(); | ||
|
||
if (err) { | ||
const errorCode = err.status || err.code; | ||
if (errorCode === 401) { | ||
return redirectToLogin(dbInfo); | ||
} | ||
setUiError(err); | ||
throw (err); | ||
try { | ||
const [isInitialReplicationNeeded] = await Promise | ||
.all([ | ||
initialReplicationLib.isReplicationNeeded(localDb, userCtx), | ||
swRegistration, | ||
setReplicationId(POUCHDB_OPTIONS, localDb), | ||
offlineDdocs.init(localDb) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the only real change here. The rest is just a quick re-factor to use async/await. |
||
]); | ||
|
||
utils.setOptions(POUCHDB_OPTIONS); | ||
|
||
if (isInitialReplicationNeeded) { | ||
const replicationStarted = performance.now(); | ||
// Polling the document count from the db. | ||
await initialReplicationLib.replicate(remoteDb, localDb); | ||
if (await initialReplicationLib.isReplicationNeeded(localDb, userCtx)) { | ||
throw new Error('Initial replication failed'); | ||
} | ||
}); | ||
window.startupTimes.replication = performance.now() - replicationStarted; | ||
} | ||
|
||
const purgeMetaStarted = performance.now(); | ||
await purger | ||
.purgeMeta(localMetaDb) | ||
.on('should-purge', shouldPurge => window.startupTimes.purgingMeta = shouldPurge) | ||
.on('start', () => setUiStatus('PURGE_META')) | ||
.on('done', () => window.startupTimes.purgeMeta = performance.now() - purgeMetaStarted) | ||
.catch(err => { | ||
console.error('Error attempting to purge meta db - continuing', err); | ||
window.startupTimes.purgingMetaFailed = err.message; | ||
}); | ||
|
||
setUiStatus('STARTING_APP'); | ||
} catch (err) { | ||
const errorCode = err.status || err.code; | ||
if (errorCode === 401) { | ||
return redirectToLogin(dbInfo); | ||
} | ||
setUiError(err); | ||
throw (err); | ||
} finally { | ||
localDb.close(); | ||
remoteDb.close(); | ||
localMetaDb.close(); | ||
} | ||
}; | ||
|
||
}()); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"globals": { | ||
"emit": true | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const contactsByFreetext = require('./medic-offline-freetext'); | ||
|
||
const getRev = async (db, id) => db | ||
.get(id) | ||
.then(({ _rev }) => _rev) | ||
.catch((e) => { | ||
if (e.status === 404) { | ||
return undefined; | ||
} | ||
throw e; | ||
}); | ||
|
||
const initDdoc = async (db, ddoc) => db.put({ | ||
...ddoc, | ||
_rev: await getRev(db, ddoc._id), | ||
}); | ||
|
||
module.exports.init = async (db) => initDdoc(db, contactsByFreetext); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes in
shared-libs/search
are just temporary. The proper changes will need to be made against the cht-datasource code once we re-base on top of that.This is why I have not added any additional unit tests to cover this logic (or worried too much about code structure).