Refactor [github] token persistence, again (#1906)

Instead of saving tokens on a timer, save them when they change. Use EventEmitter to keep the components loosely coupled.

This is easier to reason about, much easier to test, and better supports adapting to backends which support atomic operations.

This replaces json-autosave, which was a bit difficult to read and also hard to test, with fsos, the lower-level utility it’s built on.

Ref: #1848
This commit is contained in:
Paul Melnikow
2018-08-18 23:54:53 -04:00
committed by GitHub
parent a252239018
commit b10a6a4aa7
6 changed files with 91 additions and 87 deletions

View File

@@ -39,10 +39,12 @@ class GithubConstellation {
try {
await this.persistence.initialize()
} catch (e) {
// TODO Send to sentry.
console.error(e)
log.error(e)
}
githubAuth.emitter.on('token-added', this.persistence.noteTokenAdded)
githubAuth.emitter.on('token-removed', this.persistence.noteTokenRemoved)
setAdminRoutes(server)
if (serverSecrets && serverSecrets.gh_client_id) {
@@ -56,8 +58,14 @@ class GithubConstellation {
this.debugInterval = undefined
}
await this.persistence.stop()
this.persistence = undefined
githubAuth.emitter.removeListener(
'token-added',
this.persistence.noteTokenAdded
)
githubAuth.emitter.removeListener(
'token-removed',
this.persistence.noteTokenRemoved
)
}
}