恢复存储 API 决定未保存内容的副本放在哪里。把一个存储传给 createDocumentEngine 的 recovery 配置项即可。恢复的具体行为见恢复指南。
内置存储
function createIndexedDbRecovery(options?: IndexedDbRecoveryOptions): RecoveryStoreIndexedDB copies while the user edits, plus a synchronous localStorage copy when the page closes.
function createMemoryRecovery(): RecoveryStoreAn 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* | (key: string): Promise<unknown> | — | Reads a copy. |
| set* | (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* | (key: string): Promise<void> | — | Deletes a copy. |
| keys* | (): Promise<string[]> | — | Lists the stored keys. |
setNow 是可选的。页面关闭时浏览器不会等待异步写入,所以如果有 setNow,引擎会在那一刻调用它。createIndexedDbRecovery 用 localStorage 来实现它。
配置项
| Name | Type | Default | Description |
|---|---|---|---|
| store* | 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 页面。