From 626771d2c52f887cf873a38e23c40f6ce212ccd9 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Fri, 7 Aug 2020 18:21:44 +0300 Subject: [PATCH] Store: implement updating store in bulk. --- src/store/actions.ts | 17 +++++++++++++++++ src/store/reducers.ts | 5 ++++- src/sync/SyncManager.ts | 10 ++-------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/store/actions.ts b/src/store/actions.ts index 54121cc..33e50c2 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -107,6 +107,23 @@ export const unsetCacheItem = createAction( } ); +export const setCacheItemMulti = createAction( + "SET_CACHE_ITEM_MULTI", + async (_colUid: string, itemMgr: Etebase.CollectionItemManager, items: Etebase.CollectionItem[]) => { + const ret = []; + for (const item of items) { + ret.push(Etebase.toBase64(await itemMgr.cacheSave(item))); + } + return ret; + }, + (colUid: string, _itemMgr: Etebase.CollectionItemManager, items: Etebase.CollectionItem[], _deps?: Etebase.CollectionItem[]) => { + return { + colUid, + items: items, + }; + } +); + export const itemBatch = createAction( "ITEM_BATCH", async (_col: Etebase.Collection, itemMgr: Etebase.CollectionItemManager, items: Etebase.CollectionItem[], deps?: Etebase.CollectionItem[]) => { diff --git a/src/store/reducers.ts b/src/store/reducers.ts index 1647210..75f7b11 100644 --- a/src/store/reducers.ts +++ b/src/store/reducers.ts @@ -129,7 +129,10 @@ export const items = handleActions( } return state; }, - [actions.itemBatch.toString()]: (state: CacheItemsData, action_: any) => { + [combineActions( + actions.itemBatch, + actions.setCacheItemMulti + ).toString()]: (state: CacheItemsData, action_: any) => { // Fails without it for some reason const action = action_ as ActionMeta; if (action.payload !== undefined) { diff --git a/src/sync/SyncManager.ts b/src/sync/SyncManager.ts index 4bb94f3..c489b83 100644 --- a/src/sync/SyncManager.ts +++ b/src/sync/SyncManager.ts @@ -6,7 +6,7 @@ import * as Etebase from "etebase"; import { store, StoreState } from "../store"; import { credentialsSelector } from "../credentials"; -import { setSyncCollection, setSyncGeneral, setCacheItem, setCacheCollection, unsetCacheCollection, unsetCacheItem } from "../store/actions"; +import { setSyncCollection, setSyncGeneral, setCacheCollection, unsetCacheCollection, setCacheItemMulti } from "../store/actions"; const cachedSyncManager = new Map(); export class SyncManager { @@ -44,13 +44,7 @@ export class SyncManager { let done = false; while (!done) { const items = await itemMgr.list({ stoken, limit }); - for (const item of items.data) { - if (item.isDeleted) { - store.dispatch(unsetCacheItem(col.uid, itemMgr, item.uid)); - } else { - store.dispatch(setCacheItem(col, itemMgr, item)); - } - } + store.dispatch(setCacheItemMulti(col.uid, itemMgr, items.data)); done = items.done; stoken = items.stoken; }