UNPKG

mcard-js

Version:

A JavaScript implementation of MCard - A data model for persistently storing content with cryptographic hashing and timestamping

45 lines (40 loc) 1.68 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mcardPersistenceMiddleware = exports.default = void 0; // Middleware for Redux persistence of MCard data const createMcardPersistenceMiddleware = storageService => store => next => async action => { // Call the next middleware in the chain first const result = next(action); try { // Handle persistence based on action type if (action && action.type) { // Check if this is a persistable action (e.g., todo actions) const isPersistable = ['todo/addTask', 'ADD_CARD', 'test/action' // For testing purposes ].some(type => action.type === type); if (isPersistable && storageService) { // Get the current state from the store const state = typeof store.getState === 'function' ? store.getState() : store.state || {}; // Create a copy of the action with the current state snapshot const actionWithState = { ...action, meta: { ...(action.meta || {}), stateSnapshot: state } }; // Persist the action using the storage service await storageService.createAndStoreMCard(actionWithState); } } } catch (error) { console.error('Error in mcardPersistenceMiddleware:', error); } return result; }; // Create a named export for backward compatibility const mcardPersistenceMiddleware = exports.mcardPersistenceMiddleware = createMcardPersistenceMiddleware; // Export both the named and default exports for flexibility var _default = exports.default = createMcardPersistenceMiddleware; //# sourceMappingURL=mcardPersistenceMiddleware.js.map