The failed operation to add.
The created failed operation with ID and timestamp.
export function addFailedOperation(
operation: Omit<
FailedOperation,
"id" | "timestamp" | "retryCount" | "lastRetryTimestamp"
>,
): FailedOperation {
try {
const queue = getFailedOperations();
const now = Date.now();
// Create full operation object
const fullOperation: FailedOperation = {
id: `${now}_${Math.random().toString(36).substring(2, 11)}`,
timestamp: now,
retryCount: 0,
lastRetryTimestamp: null,
...operation,
};
// Add to queue
queue.operations.push(fullOperation);
// Enforce size limit (remove oldest if exceeded)
if (queue.operations.length > MAX_FAILED_OPERATIONS) {
const toRemove = queue.operations.length - MAX_FAILED_OPERATIONS;
queue.operations = queue.operations
.slice()
.sort((a, b) => a.timestamp - b.timestamp)
.slice(toRemove);
console.debug(
`[Storage] Removed ${toRemove} oldest failed operations to stay within limit`,
);
}
queue.lastUpdated = now;
// Save updated queue
storage.setItem(STORAGE_KEYS.FAILED_OPERATIONS, JSON.stringify(queue));
console.info(`[Storage] Added failed operation: ${fullOperation.id}`);
return fullOperation;
} catch (error) {
console.error("[Storage] Failed to add operation to queue:", error);
throw error;
}
}
Adds a failed operation to the queue, enforcing size limits.