getting there

This commit is contained in:
Nolan Lawson 2020-05-08 15:14:25 -07:00
parent e55797ae9b
commit bf4db5953b
7 changed files with 41 additions and 18 deletions

View File

@ -2,8 +2,8 @@ import { dbPromise, openDatabase } from './databaseLifecycle'
import {
DATA_VERSION_CURRENT,
DB_NAME,
INDEX_GROUP_AND_ORDER,
KEY_VERSION,
INDEX_GROUP_AND_ORDER, INDEX_TOKENS,
KEY_VERSION, MODE_READONLY, MODE_READWRITE,
STORE_EMOJI,
STORE_META
} from './constants'
@ -19,11 +19,11 @@ export class IndexedDBEngine {
async loadData (emojiBaseData) {
const transformedData = transformEmojiBaseData(emojiBaseData)
const dataVersion = await dbPromise(this._db, STORE_META, 'readonly', (metaStore, cb) => {
const dataVersion = await dbPromise(this._db, STORE_META, MODE_READONLY, (metaStore, cb) => {
metaStore.get(KEY_VERSION).onsuccess = e => cb(e.target.result)
})
if (dataVersion < DATA_VERSION_CURRENT) {
await dbPromise(this._db, [STORE_EMOJI, STORE_META], 'readwrite', ([emojiStore, metaStore]) => {
await dbPromise(this._db, [STORE_EMOJI, STORE_META], MODE_READWRITE, ([emojiStore, metaStore]) => {
metaStore.get(KEY_VERSION).onsuccess = e => {
const dataVersion = e.target.result
// check again within the transaction to guard against concurrency, e.g. multiple browser tabs
@ -38,11 +38,21 @@ export class IndexedDBEngine {
}
getEmojiByGroup (group) {
return dbPromise(this._db, STORE_EMOJI, 'readonly', (emojiStore, cb) => {
return dbPromise(this._db, STORE_EMOJI, MODE_READONLY, (emojiStore, cb) => {
const range = IDBKeyRange.bound([group, 0], [group + 1, 0], false, true)
emojiStore.index(INDEX_GROUP_AND_ORDER).getAll(range).onsuccess = e => {
cb(e.target.result)
}
})
}
searchEmojiByPrefix(prefix) {
prefix = prefix.toLowerCase()
return dbPromise(this._db, STORE_EMOJI, MODE_READONLY, (emojiStore, cb) => {
const range = IDBKeyRange.bound(prefix, prefix + '\uffff', false, true)
emojiStore.index(INDEX_TOKENS).getAll(range).onsuccess = e => {
cb(e.target.result)
}
})
}
}

View File

@ -6,8 +6,11 @@ export const DATA_VERSION_CURRENT = 2
export const STORE_EMOJI = 'emoji'
export const STORE_META = 'meta'
export const FIELD_TOKENS = 'tokens'
export const INDEX_TOKENS = 'tokens'
export const FIELD_UNICODE = 'unicode'
export const FIELD_GROUP = 'group'
export const FIELD_ORDER = 'order'
export const INDEX_GROUP_AND_ORDER = 'group-order'
export const KEY_VERSION = 'version'
export const MODE_READONLY = 'readonly'
export const MODE_READWRITE= 'readwrite'

View File

@ -1,4 +1,5 @@
import { DB_VERSION_CURRENT, migrations } from './migrations'
import { migrations } from './migrations'
import { DB_VERSION_CURRENT } from './constants'
const openReqs = {}
const databaseCache = {}

View File

@ -1,6 +1,7 @@
import { loadData, getEmojiByGroup } from './loadData'
import { loadData, getEmojiByGroup, searchEmojiByPrefix } from './loadData'
export {
loadData,
getEmojiByGroup
getEmojiByGroup,
searchEmojiByPrefix
}

View File

@ -21,3 +21,11 @@ export async function getEmojiByGroup (group) {
await init()
return idbEngine.getEmojiByGroup(group)
}
export async function searchEmojiByPrefix (prefix) {
if (typeof prefix !== 'string' || !prefix) {
throw new Error('expected a non-empty string, got: ' + prefix)
}
await init()
return idbEngine.searchEmojiByPrefix(prefix)
}

View File

@ -5,7 +5,7 @@ import {
INDEX_GROUP_AND_ORDER,
KEY_VERSION,
STORE_EMOJI,
STORE_META
STORE_META, INDEX_TOKENS
} from './constants'
function initialMigration (db, tx, done) {
@ -14,19 +14,19 @@ function initialMigration (db, tx, done) {
? db.createObjectStore(name, init)
: db.createObjectStore(name)
if (indexes) {
Object.keys(indexes).forEach(indexKey => {
store.createIndex(indexKey, indexes[indexKey])
})
for (const { indexName, keyPath, multiEntry } of indexes) {
store.createIndex(indexName, keyPath, { multiEntry })
}
}
return store
}
const metaStore = createObjectStore(STORE_META)
metaStore.put(DATA_VERSION_INITIAL, KEY_VERSION)
createObjectStore(STORE_EMOJI, { keyPath: FIELD_UNICODE }, {
[FIELD_TOKENS]: FIELD_TOKENS,
[INDEX_GROUP_AND_ORDER]: [FIELD_GROUP, FIELD_ORDER]
})
createObjectStore(STORE_EMOJI, { keyPath: FIELD_UNICODE }, [
{ indexName: INDEX_TOKENS, keyPath: FIELD_TOKENS, multiEntry: true },
{ indexName: INDEX_GROUP_AND_ORDER, keyPath: [FIELD_GROUP, FIELD_ORDER] }
])
done()
}

View File

@ -7,12 +7,12 @@
<body>
<h1>Ad-hoc test</h1>
<script type="module">
import { loadData, getEmojiByGroup } from '../pkg/dist-web/index.js'
import { loadData, getEmojiByGroup, searchEmojiByPrefix } from '../pkg/dist-web/index.js'
(async () => {
const emojiBaseData = await (await fetch('../node_modules/emojibase-data/en/compact.json')).json()
Object.assign(window, { loadData, getEmojiByGroup, emojiBaseData })
Object.assign(window, { loadData, getEmojiByGroup, emojiBaseData, searchEmojiByPrefix })
})()
</script>
</body>