Skip to content

Commit

Permalink
feat: persist support sync storage
Browse files Browse the repository at this point in the history
  • Loading branch information
geekact committed Oct 10, 2023
1 parent 9c1de39 commit 083155c
Show file tree
Hide file tree
Showing 18 changed files with 3,361 additions and 389 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.1

- 持久化引擎可直接使用浏览器内置的 localStorage 和 sessionStorage 接口

## [3.0.0](https://github.com/foca-js/foca/compare/v2.0.1...v3.0.0)  (2023-10-08)

### 破坏性更新
Expand Down
38 changes: 20 additions & 18 deletions docs/persist.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

```typescript
// File: store.ts
import { store, engines } from 'foca';
import { store } from 'foca';
import { userModel } from './userModel';
import { agentModel } from './agentModel';

Expand All @@ -19,7 +19,7 @@ store.init({
{
key: '$PROJECT_$ENV',
version: 1,
engine: engines.localStorage,
engine: localStorage,
// 模型白名单列表
models: [userModel, agentModel],
},
Expand All @@ -33,15 +33,11 @@ store.init({

# 存储引擎

不同的引擎会把数据存储到不同的空间,使用哪个引擎取决于项目跑在什么环境。为了统一操作,引擎操作都是异步的,目前内置的引擎有:

- localStorage
- sessionStorage
- memoryStorage

如果内置引擎无法满足,那么安装下面列举的第三方库也可以**直接当作**存储引擎:
不同的引擎会把数据存储到不同的位置,使用哪个引擎取决于项目跑在什么环境。引擎操作可以是同步的也可以是异步的。下面列举的第三方库也可以**直接当作**存储引擎:

- [localforage](https://www.npmjs.com/package/localforage) (localStorage, IndexedDB, WebSQL) - 浏览器专用
- window.localStorage - 浏览器自带
- window.sessionStorage - 浏览器自带
- [localforage](https://www.npmjs.com/package/localforage) (IndexedDB, WebSQL) - 浏览器专用
- [@react-native-async-storage/async-storage](https://www.npmjs.com/package/@react-native-async-storage/async-storage) - React-Native专用
- [foca-taro-storage](https://github.com/foca-js/foca-taro-storage) - Taro专用
- [foca-electron-storage](https://github.com/foca-js/foca-taro-storage) - Electron专用
Expand All @@ -50,14 +46,20 @@ store.init({
如果有必要,你也可以自己实现一个引擎:

```typescript
import { StorageEngine } from 'foca';

export const customEngine: StorageEngine = {
getItem(key) {},
setItem(key, value) {},
removeItem(key) {},
clear() {},
};
// import { StorageEngine } from 'foca';

interface StorageEngine {
getItem(key: string): string | null | Promise<string | null>;
setItem(key: string, value: string): any;
removeItem(key: string): any;
clear(): any;
}
```

如果是在测试环境,也可以尝试使用内置的内存引擎

```typescript
import { memoryStorage } from 'foca';
```

# 设置版本号
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"devDependencies": {
"@commitlint/cli": "^17.7.1",
"@commitlint/config-conventional": "^17.7.0",
"@react-native-async-storage/async-storage": "^1.19.3",
"@redux-devtools/extension": "^3.2.5",
"@swc/core": "1.3.83",
"@testing-library/react": "^14.0.0",
Expand All @@ -78,8 +79,10 @@
"@vitest/coverage-v8": "^0.34.3",
"browserslist": "^4.21.10",
"docsify-cli": "^4.4.4",
"fake-indexeddb": "^4.0.2",
"husky": "^8.0.3",
"jsdom": "^22.1.0",
"localforage": "^1.10.0",
"prettier": "^3.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
Loading

0 comments on commit 083155c

Please sign in to comment.