mirror of
https://github.com/nolanlawson/emoji-picker-element.git
synced 2024-06-06 19:01:25 +02:00
refactor
This commit is contained in:
parent
0f24809e77
commit
58e1667770
|
@ -1,15 +1,12 @@
|
|||
import { IndexedDBEngine } from './IndexedDBEngine'
|
||||
import { assertNonEmptyString } from './utils/assertNonEmptyString'
|
||||
import { assertETag } from './utils/assertETag'
|
||||
import { assertEmojiBaseData } from './utils/assertEmojiBaseData'
|
||||
import { assertNumber } from './utils/assertNumber'
|
||||
|
||||
const DEFAULT_DATA_SOURCE = 'https://cdn.jsdelivr.net/npm/emojibase-data@5/en/compact.json'
|
||||
const DEFAULT_LOCALE = 'en'
|
||||
|
||||
function checkETag (eTag) {
|
||||
if (!eTag) {
|
||||
throw new Error('lite-emoji-picker expects the dataSource server to return an eTag header')
|
||||
}
|
||||
}
|
||||
|
||||
export class Database {
|
||||
constructor ({ dataSource = DEFAULT_DATA_SOURCE, locale = DEFAULT_LOCALE } = {}) {
|
||||
this._dataSource = dataSource
|
||||
|
@ -32,26 +29,22 @@ export class Database {
|
|||
return
|
||||
}
|
||||
const eTag = headResponse.headers.get('etag')
|
||||
checkETag(eTag)
|
||||
assertETag(eTag)
|
||||
if (await this._idbEngine.hasData(this._dataSource, eTag)) {
|
||||
return // fast init, data is already loaded
|
||||
}
|
||||
}
|
||||
const response = await fetch(this._dataSource)
|
||||
const emojiBaseData = await response.json()
|
||||
if (!emojiBaseData || !Array.isArray(emojiBaseData)) {
|
||||
throw new Error('Expected emojibase data, but data was in wrong format: ' + emojiBaseData)
|
||||
}
|
||||
assertEmojiBaseData(emojiBaseData)
|
||||
const eTag = response.headers.get('etag')
|
||||
checkETag(eTag)
|
||||
assertETag(eTag)
|
||||
|
||||
await this._idbEngine.loadData(emojiBaseData, this._dataSource, eTag)
|
||||
}
|
||||
|
||||
async getEmojiByGroup (group) {
|
||||
if (typeof group !== 'number') {
|
||||
throw new Error('group must be a number, got: ' + group)
|
||||
}
|
||||
assertNumber(group)
|
||||
await this._readyPromise
|
||||
return this._idbEngine.getEmojiByGroup(group)
|
||||
}
|
||||
|
|
5
src/database/utils/assertETag.js
Normal file
5
src/database/utils/assertETag.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
export function assertETag (eTag) {
|
||||
if (!eTag) {
|
||||
throw new Error('lite-emoji-picker expects the dataSource server to return an eTag header')
|
||||
}
|
||||
}
|
5
src/database/utils/assertEmojiBaseData.js
Normal file
5
src/database/utils/assertEmojiBaseData.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
export function assertEmojiBaseData (emojiBaseData) {
|
||||
if (!emojiBaseData || !Array.isArray(emojiBaseData) || !emojiBaseData.length || !emojiBaseData[0].unicode) {
|
||||
throw new Error('Expected emojibase data, but data is in wrong format')
|
||||
}
|
||||
}
|
5
src/database/utils/assertNumber.js
Normal file
5
src/database/utils/assertNumber.js
Normal file
|
@ -0,0 +1,5 @@
|
|||
export function assertNumber (number) {
|
||||
if (typeof number !== 'number') {
|
||||
throw new Error('expected a number, got: ' + number)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue