Skip to content

docs(core): 📝 add snapshot doc page #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2024
Merged
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
43 changes: 43 additions & 0 deletions docs/docs/state-snapshot.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
sidebar_position: 8
---

# State Snapshot

The snapshot feature enables you to capture and later restore the state of all stores currently in scope or a selected subset of stores within your application. This can be particularly useful for scenarios where you need to save and later restore application state, such as when performing a rollback or in response to user actions.

This proves specificaly invaluable for providing transactional guarantees for the application state. While the [history plugin](./plugins/history.md) effectively captures the history of one store, a snapshot allows you to **rollback multiple actions across various stores.**

## Usage

A snapshot is a representation of the application state at a specific point in time. It includes a timestamp indicating when the snapshot was created and a `restore` method to revert the application state to the captured snapshot.

### Snapshot Creation

The `createSnapshot` function is used to generate a state snapshot. It accepts a variable number of store instances or store classes as parameters. If no stores are provided, the snapshot will include all stores.

```typescript
import { createSnapshot } from 'signalstory';

// Capture all stores
const snapshot1 = createSnapshot();

// You can use the store Class Types
const snapshot2 = createSnapshot(BookStore, UserStore);

// You can use store instances
const store1 = new Store<string>({ initialState: '' });
const store2 = new Store<string>({ initialState: '' });
const snapshot3 = createSnapshot(store1, store2);

// Or mix
const snapshot4 = createSnapshot(store1, BookStore);
```

### Snapshot Restoration

To restore the application state to a specific snapshot, call the `restore` method on the created snapshot. All captured stores will rollback their respective state.

```typescript
snapshot.restore();
```