mirror of
https://github.com/coracle-social/coracle.git
synced 2024-09-18 19:23:40 +00:00
Batch synchronization to worker
This commit is contained in:
parent
574c889875
commit
675db25af8
@ -82,7 +82,9 @@ const callLocalforage = async (storeName, method, ...args) => {
|
|||||||
|
|
||||||
const getItem = (storeName, ...args) => callLocalforage(storeName, 'getItem', ...args)
|
const getItem = (storeName, ...args) => callLocalforage(storeName, 'getItem', ...args)
|
||||||
const setItem = (storeName, ...args) => callLocalforage(storeName, 'setItem', ...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 removeItem = (storeName, ...args) => callLocalforage(storeName, 'removeItem', ...args)
|
||||||
|
const removeItems = (storeName, ...args) => callLocalforage(storeName, 'removeItems', ...args)
|
||||||
|
|
||||||
const length = storeName => callLocalforage(storeName, 'length')
|
const length = storeName => callLocalforage(storeName, 'length')
|
||||||
const clear = storeName => callLocalforage(storeName, 'clear')
|
const clear = storeName => callLocalforage(storeName, 'clear')
|
||||||
@ -181,14 +183,7 @@ const defineTable = (name: string, pk: string, opts: TableOpts = {}): Table => {
|
|||||||
setAndNotify({...data, ...newData})
|
setAndNotify({...data, ...newData})
|
||||||
|
|
||||||
// Sync to storage, keeping updates in order
|
// Sync to storage, keeping updates in order
|
||||||
p = p.then(() => {
|
p = p.then(() => setItems(name, newData)) as Promise<void>
|
||||||
const updates = []
|
|
||||||
for (const [k, v] of Object.entries(newData)) {
|
|
||||||
updates.push(setItem(name, k, v))
|
|
||||||
}
|
|
||||||
|
|
||||||
return Promise.all(updates)
|
|
||||||
}) as Promise<void>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const bulkPatch = (updates: Record<string, object>): 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))
|
const ready = derived(pluck('ready', Object.values(registry)), all(identity))
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
getItem, setItem, removeItem, length, clear, keys, iterate, watch,
|
getItem, setItem, setItems, removeItem, removeItems, length, clear, keys,
|
||||||
getPersonWithFallback, clearAll, people, rooms, messages, alerts, relays,
|
iterate, watch, getPersonWithFallback, clearAll, people, rooms, messages,
|
||||||
routes, ready,
|
alerts, relays, routes, ready,
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,21 @@ addEventListener('message', async ({data: {topic, payload, channel}}) => {
|
|||||||
switcherFn(topic, {
|
switcherFn(topic, {
|
||||||
'localforage.call': async () => {
|
'localforage.call': async () => {
|
||||||
const {storeName, method, args} = payload
|
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)
|
reply('localforage.return', result)
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user