UndoDBProvider.undo()method
Restore the wrapped provider to the state the log first observed, undoing this provider's writes.
undo(): Promise<void>
new UndoDBProvider<I, T>()
| Return | |
|---|---|
UndoDBProvider<I, T> | Database provider that records every operation and can undo its own writes. |
Database provider that records every operation and can undo its own writes.
RecordingDBProvider, additionally reading each item before the first write that touches it — so the log always contains every touched item's original state.undo() to restore the wrapped provider to the state the log first observed, e.g. rolling back optimistic local updates after a failed server call.MemoryDBProvider the reads are effectively free anyway.A recording provider whose writes can be undone. UndoDBProvider extends RecordingDBProvider and reads each item before the first write that touches it, so the operations log always contains every touched item's original state — which UndoDBProvider.undo() restores by applying the log's earliest observations in reverse.
Apply changes to a local copy immediately so the UI updates instantly, push to the server, and roll back if the push fails:
import { UndoDBProvider } from "shelving/db";
const local = new UndoDBProvider(memory);
await runServiceLogic(local); // Applies locally right away.
try {
await api.push(local.writes); // Send the writes to the server.
} catch {
await local.undo(); // Server failed — restore the local copy.
}Things to know:
MemoryDBProvider the reads are effectively free anyway.UndoDBProvider.undo() restores touched items unconditionally, so concurrent writes made to those items since the recording are overwritten.UndoDBProvider.undo() writes directly to source without recording, so the log still describes the original operations afterwards.const local = new UndoDBProvider(memory);
await runServiceLogic(local); // Applies locally right away.
try {
await api.push(local.writes); // Send the writes to the server.
} catch {
await local.undo(); // Server failed — restore the local copy.
}Restore the wrapped provider to the state the log first observed, undoing this provider's writes.
undo(): Promise<void>