refactor: refactor

This commit is contained in:
Nolan Lawson 2024-03-18 22:00:57 -07:00
parent 0714a24905
commit efafbf1e55
3 changed files with 13 additions and 15 deletions

View File

@ -1,7 +1,7 @@
import { getETag, getETagAndData } from './utils/ajax'
import { jsonChecksum } from './utils/jsonChecksum'
import { hasData, loadData } from './idbInterface'
import { AbortError, abortOpportunity } from './utils/abortSignalUtils.js'
import { abortOpportunity, throwIfAborted } from './utils/abortSignalUtils.js'
export async function checkForUpdates (db, dataSource, signal) {
// just do a simple HEAD request first to see if the eTags match
@ -19,9 +19,7 @@ export async function checkForUpdates (db, dataSource, signal) {
if (import.meta.env.MODE === 'test') {
await abortOpportunity()
}
if (signal.aborted) {
throw new AbortError()
}
throwIfAborted(signal)
}
}
const doesHaveData = await hasData(db, dataSource, eTag)
@ -29,9 +27,7 @@ export async function checkForUpdates (db, dataSource, signal) {
if (import.meta.env.MODE === 'test') {
await abortOpportunity()
}
if (signal.aborted) {
throw new AbortError()
}
throwIfAborted(signal)
if (doesHaveData) {
console.log('Database already populated')

View File

@ -1,4 +1,4 @@
import { AbortError, abortOpportunity } from './utils/abortSignalUtils.js'
import { abortOpportunity, throwIfAborted } from './utils/abortSignalUtils.js'
import { isEmpty } from './idbInterface.js'
import { checkForUpdates, loadDataForFirstTime } from './dataLoading.js'
import { addOnCloseListener, openDatabase } from './databaseLifecycle.js'
@ -10,18 +10,14 @@ export async function initializeDatabase (dbName, dataSource, onClear, signal) {
if (import.meta.env.MODE === 'test') {
await abortOpportunity()
}
if (signal.aborted) {
throw new AbortError()
}
throwIfAborted(signal)
const empty = await isEmpty(db)
/* istanbul ignore else */
if (import.meta.env.MODE === 'test') {
await abortOpportunity()
}
if (signal.aborted) {
throw new AbortError()
}
throwIfAborted(signal)
let lazyUpdate
if (empty) {

View File

@ -8,9 +8,15 @@ export async function abortOpportunity () {
await Promise.resolve()
}
export class AbortError extends Error {
class AbortError extends Error {
constructor () {
super('The operation was aborted')
this.name = 'AbortError'
}
}
export function throwIfAborted (signal) {
if (signal.aborted) {
throw new AbortError()
}
}