Batch synchronization to worker

This commit is contained in:
Jonathan Staab 2023-02-13 18:04:26 -06:00
parent 574c889875
commit 675db25af8
2 changed files with 21 additions and 12 deletions

View File

@ -82,7 +82,9 @@ const callLocalforage = async (storeName, method, ...args) => {
const getItem = (storeName, ...args) => callLocalforage(storeName, 'getItem', ...args)
const setItem = (storeName, ...args) => callLocalforage(storeName, 'setItem', ...args)
const setItems = (storeName, ...args) => callLocalforage(storeName, 'setItems', ...args)
const removeItem = (storeName, ...args) => callLocalforage(storeName, 'removeItem', ...args)
const removeItems = (storeName, ...args) => callLocalforage(storeName, 'removeItems', ...args)
const length = storeName => callLocalforage(storeName, 'length')
const clear = storeName => callLocalforage(storeName, 'clear')
@ -181,14 +183,7 @@ const defineTable = (name: string, pk: string, opts: TableOpts = {}): Table => {
setAndNotify({...data, ...newData})
// Sync to storage, keeping updates in order
p = p.then(() => {
const updates = []
for (const [k, v] of Object.entries(newData)) {
updates.push(setItem(name, k, v))
}
return Promise.all(updates)
}) as Promise<void>
p = p.then(() => setItems(name, newData)) as Promise<void>
}
const bulkPatch = (updates: Record<string, object>): void => {
@ -314,7 +309,7 @@ const clearAll = () => Promise.all(Object.keys(registry).map(clear))
const ready = derived(pluck('ready', Object.values(registry)), all(identity))
export default {
getItem, setItem, removeItem, length, clear, keys, iterate, watch,
getPersonWithFallback, clearAll, people, rooms, messages, alerts, relays,
routes, ready,
getItem, setItem, setItems, removeItem, removeItems, length, clear, keys,
iterate, watch, getPersonWithFallback, clearAll, people, rooms, messages,
alerts, relays, routes, ready,
}

View File

@ -24,7 +24,21 @@ addEventListener('message', async ({data: {topic, payload, channel}}) => {
switcherFn(topic, {
'localforage.call': async () => {
const {storeName, method, args} = payload
const result = await getStore(storeName)[method](...args)
const instance = getStore(storeName)
const result = await switcherFn(method, {
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)
}
},
default: () => instance[method](...args),
})
reply('localforage.return', result)
},