The failed operations queue.
export function getFailedOperations(): FailedOperationsQueue {
try {
const stored = storage.getItem(STORAGE_KEYS.FAILED_OPERATIONS);
if (!stored) {
// Return a deep clone of DEFAULT_FAILED_OPERATIONS_QUEUE
// to avoid mutating the constant
return {
operations: [],
lastUpdated: Date.now(),
version: DEFAULT_FAILED_OPERATIONS_QUEUE.version,
};
}
const parsed = JSON.parse(stored) as FailedOperationsQueue;
// Validate structure
if (
!Array.isArray(parsed.operations) ||
typeof parsed.lastUpdated !== "number" ||
typeof parsed.version !== "number"
) {
console.warn(
"[Storage] Invalid failed operations structure, using defaults",
);
// Return a fresh copy instead of the constant
return {
operations: [],
lastUpdated: Date.now(),
version: DEFAULT_FAILED_OPERATIONS_QUEUE.version,
};
}
// Filter out expired operations
const now = Date.now();
const expiryMs = FAILED_OPERATION_EXPIRY_DAYS * 24 * 60 * 60 * 1000;
const validOperations = parsed.operations.filter(
(op) => now - op.timestamp < expiryMs,
);
// If we filtered out any operations, save the updated queue
if (validOperations.length < parsed.operations.length) {
const updated: FailedOperationsQueue = {
operations: validOperations,
lastUpdated: now,
version: parsed.version,
};
storage.setItem(STORAGE_KEYS.FAILED_OPERATIONS, JSON.stringify(updated));
console.debug(
`[Storage] Removed ${parsed.operations.length - validOperations.length} expired operations`,
);
return updated;
}
return parsed;
} catch (error) {
console.error("[Storage] Failed to load failed operations:", error);
// Return a fresh copy instead of the constant
return {
operations: [],
lastUpdated: Date.now(),
version: DEFAULT_FAILED_OPERATIONS_QUEUE.version,
};
}
}
Retrieves failed operations queue, filtering out expired ones.