Page MenuHomePhabricator

D8621.id29047.diff
No OneTemporary

D8621.id29047.diff

diff --git a/lib/ops/report-store-ops.js b/lib/ops/report-store-ops.js
--- a/lib/ops/report-store-ops.js
+++ b/lib/ops/report-store-ops.js
@@ -1,5 +1,6 @@
// @flow
+import { type BaseStoreOpsHandlers } from './base-ops.js';
import type { ClientReportCreationRequest } from '../types/report-types.js';
export type ReplaceQueuedReportOperation = {
@@ -33,31 +34,6 @@
export type ClientDBReport = { +id: string, +report: string };
-function processReportStoreOperations(
- queuedReports: $ReadOnlyArray<ClientReportCreationRequest>,
- reportStoreOps: $ReadOnlyArray<ReportStoreOperation>,
-): $ReadOnlyArray<ClientReportCreationRequest> {
- if (reportStoreOps.length === 0) {
- return queuedReports;
- }
- let processedReports = [...queuedReports];
- for (const operation of reportStoreOps) {
- if (operation.type === 'replace_report') {
- const filteredReports = processedReports.filter(
- report => report.id !== operation.payload.report.id,
- );
- processedReports = [...filteredReports, { ...operation.payload.report }];
- } else if (operation.type === 'remove_reports') {
- processedReports = processedReports.filter(
- report => !operation.payload.ids.includes(report.id),
- );
- } else if (operation.type === 'remove_all_reports') {
- processedReports = [];
- }
- }
- return processedReports;
-}
-
function convertReportsToReplaceReportOps(
reports: $ReadOnlyArray<ClientReportCreationRequest>,
): $ReadOnlyArray<ReplaceQueuedReportOperation> {
@@ -76,36 +52,68 @@
};
}
-function convertReportStoreOperationToClientDBReportStoreOperation(
- reportStoreOperations: $ReadOnlyArray<ReportStoreOperation>,
-): $ReadOnlyArray<ClientDBReportStoreOperation> {
- return reportStoreOperations.map(operation => {
- if (
- operation.type === 'remove_reports' ||
- operation.type === 'remove_all_reports'
- ) {
- return operation;
+export const reportStoreOpsHandlers: BaseStoreOpsHandlers<
+ $ReadOnlyArray<ClientReportCreationRequest>,
+ ReportStoreOperation,
+ ClientDBReportStoreOperation,
+ $ReadOnlyArray<ClientReportCreationRequest>,
+ ClientDBReport,
+> = {
+ processStoreOperations(
+ queuedReports: $ReadOnlyArray<ClientReportCreationRequest>,
+ ops: $ReadOnlyArray<ReportStoreOperation>,
+ ): $ReadOnlyArray<ClientReportCreationRequest> {
+ if (ops.length === 0) {
+ return queuedReports;
}
- return {
- type: 'replace_report',
- payload: {
- id: operation.payload.report.id,
- report: JSON.stringify(operation.payload.report),
- },
- };
- });
-}
+ let processedReports = [...queuedReports];
+ for (const operation of ops) {
+ if (operation.type === 'replace_report') {
+ const filteredReports = processedReports.filter(
+ report => report.id !== operation.payload.report.id,
+ );
+ processedReports = [
+ ...filteredReports,
+ { ...operation.payload.report },
+ ];
+ } else if (operation.type === 'remove_reports') {
+ processedReports = processedReports.filter(
+ report => !operation.payload.ids.includes(report.id),
+ );
+ } else if (operation.type === 'remove_all_reports') {
+ processedReports = [];
+ }
+ }
+ return processedReports;
+ },
-function convertClientDBReportToClientReportCreationRequest(
- reports: $ReadOnlyArray<ClientDBReport>,
-): $ReadOnlyArray<ClientReportCreationRequest> {
- return reports.map(reportRecord => JSON.parse(reportRecord.report));
-}
+ convertOpsToClientDBOps(
+ ops: $ReadOnlyArray<ReportStoreOperation>,
+ ): $ReadOnlyArray<ClientDBReportStoreOperation> {
+ return ops.map(operation => {
+ if (
+ operation.type === 'remove_reports' ||
+ operation.type === 'remove_all_reports'
+ ) {
+ return operation;
+ }
+ return {
+ type: 'replace_report',
+ payload: {
+ id: operation.payload.report.id,
+ report: JSON.stringify(operation.payload.report),
+ },
+ };
+ });
+ },
+ translateClientDBData(
+ data: $ReadOnlyArray<ClientDBReport>,
+ ): $ReadOnlyArray<ClientReportCreationRequest> {
+ return data.map(reportRecord => JSON.parse(reportRecord.report));
+ },
+};
export {
- processReportStoreOperations,
convertReportsToReplaceReportOps,
convertReportsToRemoveReportsOperation,
- convertReportStoreOperationToClientDBReportStoreOperation,
- convertClientDBReportToClientReportCreationRequest,
};
diff --git a/lib/ops/report-store-ops.test.js b/lib/ops/report-store-ops.test.js
--- a/lib/ops/report-store-ops.test.js
+++ b/lib/ops/report-store-ops.test.js
@@ -1,6 +1,6 @@
// @flow
-import { processReportStoreOperations } from './report-store-ops.js';
+import { reportStoreOpsHandlers } from './report-store-ops.js';
import {
type ClientReportCreationRequest,
reportTypes,
@@ -51,6 +51,9 @@
);
};
+const { processStoreOperations: processReportStoreOperations } =
+ reportStoreOpsHandlers;
+
describe('processReportStoreOperations', () => {
it('should return the original reports if no operations are processed', () => {
const reportStoreOps = [];
diff --git a/lib/reducers/report-store-reducer.js b/lib/reducers/report-store-reducer.js
--- a/lib/reducers/report-store-reducer.js
+++ b/lib/reducers/report-store-reducer.js
@@ -16,7 +16,7 @@
import {
convertReportsToRemoveReportsOperation,
convertReportsToReplaceReportOps,
- processReportStoreOperations,
+ reportStoreOpsHandlers,
} from '../ops/report-store-ops.js';
import { isStaff } from '../shared/staff-utils.js';
import type { BaseAction } from '../types/redux-types.js';
@@ -32,6 +32,9 @@
export const updateReportsEnabledActionType = 'UPDATE_REPORTS_ENABLED';
+const { processStoreOperations: processReportStoreOperations } =
+ reportStoreOpsHandlers;
+
export default function reduceReportStore(
state: ReportStore,
action: BaseAction,
diff --git a/native/data/sqlite-data-handler.js b/native/data/sqlite-data-handler.js
--- a/native/data/sqlite-data-handler.js
+++ b/native/data/sqlite-data-handler.js
@@ -6,7 +6,7 @@
import { setClientDBStoreActionType } from 'lib/actions/client-db-store-actions.js';
import { MediaCacheContext } from 'lib/components/media-cache-provider.react.js';
-import { convertClientDBReportToClientReportCreationRequest } from 'lib/ops/report-store-ops.js';
+import { reportStoreOpsHandlers } from 'lib/ops/report-store-ops.js';
import { threadStoreOpsHandlers } from 'lib/ops/thread-store-ops.js';
import { cookieSelector } from 'lib/selectors/keyserver-selectors.js';
import { isLoggedIn } from 'lib/selectors/user-selectors.js';
@@ -174,7 +174,7 @@
const threadInfosFromDB =
threadStoreOpsHandlers.translateClientDBData(threads);
const reportsFromDb =
- convertClientDBReportToClientReportCreationRequest(reports);
+ reportStoreOpsHandlers.translateClientDBData(reports);
dispatch({
type: setClientDBStoreActionType,
diff --git a/native/redux/persist.js b/native/redux/persist.js
--- a/native/redux/persist.js
+++ b/native/redux/persist.js
@@ -19,8 +19,8 @@
import {
type ReportStoreOperation,
type ClientDBReportStoreOperation,
- convertReportStoreOperationToClientDBReportStoreOperation,
convertReportsToReplaceReportOps,
+ reportStoreOpsHandlers,
} from 'lib/ops/report-store-ops.js';
import type { ClientDBThreadStoreOperation } from 'lib/ops/thread-store-ops.js';
import { threadStoreOpsHandlers } from 'lib/ops/thread-store-ops.js';
@@ -580,9 +580,7 @@
...convertReportsToReplaceReportOps(state.reportStore.queuedReports),
];
const dbOperations: $ReadOnlyArray<ClientDBReportStoreOperation> =
- convertReportStoreOperationToClientDBReportStoreOperation(
- reportStoreOperations,
- );
+ reportStoreOpsHandlers.convertOpsToClientDBOps(reportStoreOperations);
try {
commCoreModule.processReportStoreOperationsSync(dbOperations);
diff --git a/native/redux/redux-utils.js b/native/redux/redux-utils.js
--- a/native/redux/redux-utils.js
+++ b/native/redux/redux-utils.js
@@ -2,7 +2,7 @@
import { useSelector as reactReduxUseSelector } from 'react-redux';
-import { convertReportStoreOperationToClientDBReportStoreOperation } from 'lib/ops/report-store-ops.js';
+import { reportStoreOpsHandlers } from 'lib/ops/report-store-ops.js';
import { threadStoreOpsHandlers } from 'lib/ops/thread-store-ops.js';
import type { StoreOperations } from 'lib/types/store-ops-types.js';
import { convertMessageStoreOperationsToClientDBOperations } from 'lib/utils/message-ops-utils.js';
@@ -33,9 +33,7 @@
const convertedMessageStoreOperations =
convertMessageStoreOperationsToClientDBOperations(messageStoreOperations);
const convertedReportStoreOperations =
- convertReportStoreOperationToClientDBReportStoreOperation(
- reportStoreOperations,
- );
+ reportStoreOpsHandlers.convertOpsToClientDBOps(reportStoreOperations);
try {
const promises = [];
diff --git a/web/database/sqlite-data-handler.js b/web/database/sqlite-data-handler.js
--- a/web/database/sqlite-data-handler.js
+++ b/web/database/sqlite-data-handler.js
@@ -5,7 +5,7 @@
import { useDispatch } from 'react-redux';
import { setClientDBStoreActionType } from 'lib/actions/client-db-store-actions.js';
-import { convertClientDBReportToClientReportCreationRequest } from 'lib/ops/report-store-ops.js';
+import { reportStoreOpsHandlers } from 'lib/ops/report-store-ops.js';
import { databaseModule } from './database-module-provider.js';
import { SQLITE_ENCRYPTION_KEY } from './utils/constants.js';
@@ -95,7 +95,7 @@
if (!data?.store?.drafts && !data?.store?.reports) {
return;
}
- const reports = convertClientDBReportToClientReportCreationRequest(
+ const reports = reportStoreOpsHandlers.translateClientDBData(
data.store.reports,
);
dispatch({
diff --git a/web/redux/redux-setup.js b/web/redux/redux-setup.js
--- a/web/redux/redux-setup.js
+++ b/web/redux/redux-setup.js
@@ -7,7 +7,7 @@
logOutActionTypes,
deleteAccountActionTypes,
} from 'lib/actions/user-actions.js';
-import { convertReportStoreOperationToClientDBReportStoreOperation } from 'lib/ops/report-store-ops.js';
+import { reportStoreOpsHandlers } from 'lib/ops/report-store-ops.js';
import baseReducer from 'lib/reducers/master-reducer.js';
import { mostRecentlyReadThreadSelector } from 'lib/selectors/thread-selectors.js';
import { isLoggedIn } from 'lib/selectors/user-selectors.js';
@@ -189,9 +189,7 @@
return;
}
const convertedReportStoreOperations =
- convertReportStoreOperationToClientDBReportStoreOperation(
- reportStoreOperations,
- );
+ reportStoreOpsHandlers.convertOpsToClientDBOps(reportStoreOperations);
await databaseModule.schedule({
type: workerRequestMessageTypes.PROCESS_STORE_OPERATIONS,
storeOperations: {

File Metadata

Mime Type
text/plain
Expires
Tue, Nov 26, 1:54 AM (21 h, 34 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2581936
Default Alt Text
D8621.id29047.diff (10 KB)

Event Timeline