# 恢复 API

> 恢复存储 API 参考：内置的 IndexedDB 和内存存储，以及 RecoveryStore 接口和 setNow。

Source: https://fabricjs-document-engine.jscrate.dev/zh/docs/api/recovery-api
Last updated: 2026-09-28

恢复存储 API 决定未保存内容的副本放在哪里。把一个存储传给 `createDocumentEngine` 的 `recovery` 配置项即可。恢复的具体行为见[恢复指南](https://fabricjs-document-engine.jscrate.dev/zh/docs/guides/recovery)。

## 内置存储

```ts
function createIndexedDbRecovery(options?: IndexedDbRecoveryOptions): RecoveryStore
```

IndexedDB copies while the user edits, plus a synchronous localStorage copy when the page closes.

```ts
function createMemoryRecovery(): RecoveryStore
```

An in-memory recovery store for tests.

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `databaseName` | `string` | — | The IndexedDB database name. |
| `storeName` | `string` | — | The object store name. |

## RecoveryStore 接口

实现这个恢复存储 API，就能把副本放到别的地方：

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `get` (required) | `(key: string): Promise<unknown>` | — | Reads a copy. |
| `set` (required) | `(key: string, value: unknown): Promise<void>` | — | Writes a copy. |
| `setNow` | `(key: string, value: unknown): void` | — | Writes a copy synchronously, for the moment the page closes. |
| `delete` (required) | `(key: string): Promise<void>` | — | Deletes a copy. |
| `keys` (required) | `(): Promise<string[]>` | — | Lists the stored keys. |

`setNow` 是可选的。页面关闭时浏览器不会等待异步写入，所以如果有 `setNow`，引擎会在那一刻调用它。`createIndexedDbRecovery` 用 localStorage 来实现它。

## 配置项

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `store` (required) | `RecoveryStore` | — | Where copies are kept, such as `createIndexedDbRecovery()`. |
| `interval` | `number` | `2000` | Write a copy at most once every this many milliseconds while there are unsaved changes. |

使用恢复存储 API 的引擎方法，比如 `getRecoverableDocuments` 和 `restoreRecovery`，列在 [DocumentEngine](https://fabricjs-document-engine.jscrate.dev/zh/docs/api/document-engine) 页面。
