Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F3528995
D12400.id41230.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Referenced Files
None
Subscribers
None
D12400.id41230.diff
View Options
diff --git a/lib/shared/redux/client-db-utils.js b/lib/shared/redux/client-db-utils.js
new file mode 100644
--- /dev/null
+++ b/lib/shared/redux/client-db-utils.js
@@ -0,0 +1,57 @@
+// @flow
+
+import _keyBy from 'lodash/fp/keyBy.js';
+
+import {
+ threadStoreOpsHandlers,
+ type ClientDBThreadStoreOperation,
+ type ReplaceThreadOperation,
+ type ThreadStoreOperation,
+} from '../../ops/thread-store-ops.js';
+import type { RawThreadInfos, ThreadStore } from '../../types/thread-types.js';
+import { entries } from '../../utils/objects.js';
+
+function updateThreadStoreThreadInfos(
+ threadStore: ThreadStore,
+ migrationFunc: RawThreadInfos => RawThreadInfos,
+): {
+ +newThreadStore: ThreadStore,
+ +dbOperations: $ReadOnlyArray<ClientDBThreadStoreOperation>,
+} {
+ const rawThreadInfos = threadStore.threadInfos;
+
+ // 1. Convert `RawThreadInfo`s to a map of `threadID` => `threadInfo`.
+ const keyedRawThreadInfos = _keyBy('id')(rawThreadInfos);
+
+ // 2. Apply `migrationFunc` to `ThreadInfo`s.
+ const updatedKeyedRawThreadInfos = migrationFunc(keyedRawThreadInfos);
+
+ // 3. Construct `replace` `ThreadStoreOperation`s.
+ const replaceOps: $ReadOnlyArray<ReplaceThreadOperation> = entries(
+ updatedKeyedRawThreadInfos,
+ ).map(([id, threadInfo]) => ({
+ type: 'replace',
+ payload: { id, threadInfo },
+ }));
+
+ // 4. Prepend `replaceOps` with `remove_all` ops
+ const operations: $ReadOnlyArray<ThreadStoreOperation> = [
+ { type: 'remove_all' },
+ ...replaceOps,
+ ];
+
+ // 5. Process `ThreadStoreOperation`s to get new `ThreadStore`.
+ const newThreadStore: ThreadStore =
+ threadStoreOpsHandlers.processStoreOperations(threadStore, operations);
+
+ // 6. Convert `ThreadStoreOperation`s to `ClientDBThreadStoreOperation`s.
+ const dbOperations: $ReadOnlyArray<ClientDBThreadStoreOperation> =
+ threadStoreOpsHandlers.convertOpsToClientDBOps(operations);
+
+ return {
+ newThreadStore,
+ dbOperations,
+ };
+}
+
+export { updateThreadStoreThreadInfos };
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Dec 25, 11:40 AM (10 h, 40 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2700975
Default Alt Text
D12400.id41230.diff (1 KB)
Attached To
Mode
D12400: [lib] introduce updateThreadStoreThreadInfos
Attached
Detach File
Event Timeline
Log In to Comment