diff --git a/lib/ops/dm-operations-store-ops.js b/lib/ops/dm-operations-store-ops.js --- a/lib/ops/dm-operations-store-ops.js +++ b/lib/ops/dm-operations-store-ops.js @@ -77,7 +77,18 @@ }); } +function convertClientDBDMOperationToDMOperation( + operation: ClientDBDMOperation, +): Operation { + return { + id: operation.id, + type: operation.type, + operation: JSON.parse(operation.operation), + }; +} + export { convertDMOperationIntoClientDBDMOperation, convertDMOperationOpsToClientDBOps, + convertClientDBDMOperationToDMOperation, }; diff --git a/lib/shared/unshim-utils.js b/lib/shared/unshim-utils.js --- a/lib/shared/unshim-utils.js +++ b/lib/shared/unshim-utils.js @@ -3,11 +3,20 @@ import _mapValues from 'lodash/fp/mapValues.js'; import { messageSpecs } from './messages/message-specs.js'; +import { + type ClientDBDMOperation, + convertClientDBDMOperationToDMOperation, +} from '../ops/dm-operations-store-ops.js'; +import { type DMOperationType } from '../types/dm-ops.js'; import { type MessageType, messageTypes } from '../types/message-types-enum.js'; import { type MessageStore, type RawMessageInfo, } from '../types/message-types.js'; +import type { BaseNavInfo } from '../types/nav-types.js'; +import type { BaseAppState } from '../types/redux-types.js'; +import { getConfig } from '../utils/config.js'; +import type { MigrationResult } from '../utils/migration-utils.js'; function unshimFunc( messageInfo: RawMessageInfo, @@ -55,4 +64,49 @@ ); } -export { DEPRECATED_unshimMessageStore, unshimMessageInfos, unshimFunc }; +async function unshimDMOperations<N: BaseNavInfo, T: BaseAppState<N>>( + state: T, + type: DMOperationType, + handleMigrationFailure: T => T, +): Promise<MigrationResult<T>> { + const { fetchDMOperationsByType } = getConfig().sqliteAPI; + try { + const operations: Array<ClientDBDMOperation> = + await fetchDMOperationsByType(type); + if (operations.length === 0) { + return { + state, + ops: {}, + }; + } + + const shimmedOperations = operations + .map(convertClientDBDMOperationToDMOperation) + .map(op => ({ + id: op.id, + operation: op.operation, + })); + + return { + state: { + ...state, + queuedDMOperations: { + ...state.queuedDMOperations, + shimmedOperations, + }, + }, + ops: {}, + }; + } catch (e) { + console.log(e); + const newState = handleMigrationFailure(state); + return { state: newState, ops: {} }; + } +} + +export { + DEPRECATED_unshimMessageStore, + unshimMessageInfos, + unshimFunc, + unshimDMOperations, +};