Batch restore database

This commit is contained in:
Jonathan Staab 2023-02-13 19:44:49 -06:00
parent d1cc9e4299
commit 233d754258
4 changed files with 19 additions and 15 deletions

View File

@ -89,6 +89,7 @@ const removeItems = (storeName, ...args) => callLocalforage(storeName, 'removeIt
const length = storeName => callLocalforage(storeName, 'length')
const clear = storeName => callLocalforage(storeName, 'clear')
const keys = storeName => callLocalforage(storeName, 'keys')
const dump = storeName => callLocalforage(storeName, 'dump')
const iterate = (storeName, where = {}) => ({
[Symbol.asyncIterator]() {
@ -211,12 +212,7 @@ const defineTable = (name: string, pk: string, opts: TableOpts = {}): Table => {
// Sync from storage initially
;(async () => {
const t = Date.now()
const initialData = {}
for await (const {k, v} of iterate(name)) {
if (isValid(v)) {
initialData[k] = v
}
}
const initialData = filter(isValid, await dump(name))
if (resetOnInit) {
await clear(name)
@ -310,6 +306,6 @@ const ready = derived(pluck('ready', Object.values(registry)), all(identity))
export default {
getItem, setItem, setItems, removeItem, removeItems, length, clear, keys,
iterate, watch, getPersonWithFallback, clearAll, people, rooms, messages,
dump, iterate, watch, getPersonWithFallback, clearAll, people, rooms, messages,
alerts, relays, routes, ready,
}

View File

@ -27,14 +27,22 @@ addEventListener('message', async ({data: {topic, payload, channel}}) => {
const instance = getStore(storeName)
const result = await switcherFn(method, {
dump: () => new Promise(resolve => {
const result = {}
instance.iterate(
(v, k, i) => { result[k] = v },
() => resolve(result),
)
}),
setItems: () => {
for (const [k, v] of Object.entries(args[0])) {
instance.setItem(k, v)
}
},
removeItems: () => {
for (const [k, v] of Object.entries(args[0])) {
instance.removeItem(k, v)
for (const k of args[0]) {
instance.removeItem(k)
}
},
default: () => instance[method](...args),

View File

@ -125,10 +125,10 @@ export const renderNote = (note, {showEntire = false}) => {
}
export const asDisplayEvent = event =>
({children: [], replies: [], reactions: [], ...event})
({children: [], replies: [], reactions: [], ...event}) as DisplayEvent
export const mergeParents = (notes: Array<DisplayEvent>) => {
const notesById = createMap('id', notes)
const notesById = createMap('id', notes) as Record<string, DisplayEvent>
const childIds = []
for (const note of Object.values(notesById)) {
@ -140,7 +140,7 @@ export const mergeParents = (notes: Array<DisplayEvent>) => {
// Add the current note to its parents children, but only if we found a parent
if (notesById[parentId]) {
notesById[parentId].children = notesById[parentId].children.concat(note)
notesById[parentId].children = notesById[parentId].children.concat([note])
}
}

View File

@ -18,7 +18,7 @@ export type MyEvent = Event & {
}
export type DisplayEvent = MyEvent & {
replies: []
reactions: []
children: []
replies: Array<MyEvent>
reactions: Array<MyEvent>
children: Array<DisplayEvent>
}