Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .changeset/legal-lines-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@xstate/store': minor
---

Add `snapshot` parameter to `getTransactionId` function.

```ts
const store = createStore(
undo(
{
// ...
},
{
getTransactionId: (event, snapshot) =>
snapshot.context.currentTransactionId
}
)
);
```
22 changes: 22 additions & 0 deletions .changeset/tangy-spies-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
'@xstate/store': minor
---

Add `skipEvent` option to `undoRedo()` to exclude certain events from undo/redo history.

```ts
const store = createStore(
undoRedo(
{
context: { count: 0 },
on: {
inc: (ctx) => ({ count: ctx.count + 1 }),
log: (ctx) => ctx // No state change
}
},
{
skipEvent: (event, snapshot) => event.type === 'log'
}
)
);
```
71 changes: 55 additions & 16 deletions packages/xstate-store/src/undo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,31 @@ type UndoEvent<TEvent extends EventObject> = {
* store.trigger.undo(); // count = 0 (undoes both inc events)
* ```
*
* @example
*
* ```ts
* // Skip certain events from undo/redo
* const store = createStore(
* undoRedo(
* {
* context: { count: 0 },
* on: {
* inc: (ctx) => ({ count: ctx.count + 1 }),
* log: (ctx) => ctx // No state change, just logging
* }
* },
* {
* skipEvent: (event) => event.type === 'log'
* }
* )
* );
*
* store.send({ type: 'inc' }); // count = 1
* store.send({ type: 'log' }); // count = 1 (logged but not undoable)
* store.send({ type: 'inc' }); // count = 2
* store.trigger.undo(); // count = 1 (skips log event)
* ```
*
* @returns Store logic with additional `undo` and `redo` event handlers
*/
export function undoRedo<
Expand All @@ -79,7 +104,20 @@ export function undoRedo<
>(
storeConfig: StoreConfig<TContext, TEventPayloadMap, TEmittedPayloadMap>,
options?: {
getTransactionId?: (event: ExtractEvents<TEventPayloadMap>) => string;
/** A function that returns the transaction ID of an event. */
getTransactionId?: (
event: ExtractEvents<TEventPayloadMap>,
snapshot: StoreSnapshot<TContext>
) => string | null | undefined;
/**
* A function that returns whether an event should be skipped during
* undo/redo. Skipped events are not stored in history and are not replayed
* during undo/redo.
*/
skipEvent?: (
event: ExtractEvents<TEventPayloadMap>,
snapshot: StoreSnapshot<TContext>
) => boolean;
Comment on lines +108 to +120
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: event actions API has order ctx, event whereas these two functions have the reverse order. They primarily work based on events which makes sense for event to be the first parameter but for consistency, we might want to consider unifying the order.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That'd be a breaking change

}
): StoreLogic<
StoreSnapshot<TContext>,
Expand Down Expand Up @@ -110,15 +148,8 @@ export function undoRedo<
if (event.type === 'undo') {
const events = snapshot.events.slice();
const undoStack = snapshot.undoStack.slice();
if (!events.length) {
return [
{
...snapshot,
events,
undoStack
},
[]
];
if (!snapshot.events.length) {
return [snapshot, []];
}

// Get the transaction ID of the last event
Expand Down Expand Up @@ -148,14 +179,17 @@ export function undoRedo<
}
}

// Filter out events that should be skipped during undo
const eventsToReplay = events;

// Replay remaining events to get to the new state
let state = {
...logic.getInitialSnapshot(),
events,
undoStack
};

for (const { event } of events) {
for (const { event } of eventsToReplay) {
const [newState, _effects] = logic.transition(state, event);
state = {
...newState,
Expand Down Expand Up @@ -194,6 +228,7 @@ export function undoRedo<
undoStack[undoStack.length - 1].transactionId === lastTransactionId
) {
const undoEvent = undoStack.pop()!;

events.push(undoEvent);
const [newState, effects] = logic.transition(state, undoEvent.event);
state = {
Expand All @@ -207,15 +242,19 @@ export function undoRedo<
return [state, allEffects];
}

const events = snapshot.events.slice();
const [state, effects] = logic.transition(snapshot, event);
const isEventSkipped = options?.skipEvent?.(event, snapshot);
const events = isEventSkipped
? snapshot.events
: snapshot.events.concat({
event,
transactionId: options?.getTransactionId?.(event, snapshot)
});

return [
{
...state,
events: events.concat({
event,
transactionId: options?.getTransactionId?.(event)
}),
events,
// Clear the undo stack when new events occur
undoStack: []
},
Expand Down
Loading