Skip to content

Commit

Permalink
feat: add interceptor hoc
Browse files Browse the repository at this point in the history
  • Loading branch information
geekact committed Apr 13, 2022
1 parent c55e752 commit 9ff090a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 57 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,47 @@ yarn add foca-taro-storage

# 使用

### 入口

```typescript
import { FocaProvider } from 'foca';
import { taroStorage } from 'foca-taro-storage';

class App extends Component {
render() {
return (
// 注意:是个匿名函数
<FocaProvider>{() => this.props.children}</FocaProvider>
);
}
}
```

### 路由

关注 taro 官方 issue https://github.com/NervJS/taro/issues/6548#issuecomment-717896033 ,需要在`首页`加入拦截器以保证数据的稳定性

```typescript
import { persistInterceptor } from 'foca-taro-storage';

// 写法1
@persistInterceptor
export class HomePage extends Component {}

// 写法2
class HomePage extends Component {}
export default persistInterceptor(HomePage);

// 写法3
const HomePage: FC<Props> = (props) => {};
export default persistInterceptor(HomePage);
```

如果你的某些页面需要分享给用户,则`分享入口`文件也需要搭配拦截器。

```typescript
import { persistInterceptor } from 'foca-taro-storage';

@persistInterceptor
export class MySharePage extends Component {}
```
17 changes: 17 additions & 0 deletions src/hoc.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { store } from 'foca';
import React, { ComponentType, useEffect, useState, forwardRef } from 'react';

// TODO: 支持ref的类型
export function persistInterceptor<T>(EntryComponent: ComponentType<T>) {
const [ready, setReady] = useState(false);

return forwardRef<unknown, T>((props, ref) => {
useEffect(() => {
store.onInitialized().then(() => {
setReady(true);
});
});

return ready ? <EntryComponent {...props} ref={ref} /> : null;
});
}
59 changes: 2 additions & 57 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,2 @@
import type { StorageEngine } from 'foca';
import {
getStorage,
getStorageSync,
setStorage,
removeStorage,
clearStorage,
} from '@tarojs/taro';

/**
* 持久化必须在小程序onLoad之前完成,否则页面报错
* @link https://github.com/NervJS/taro/issues/6548#issuecomment-717896033
*/
const isMiniProgram =
process.env.TARO_ENV !== 'rn' &&
process.env.TARO_ENV !== 'h5' &&
process.env.TARO_ENV !== 'quickapp';

export const taroStorage: StorageEngine = {
getItem(key) {
let promise: Promise<string | null>;

if (isMiniProgram) {
try {
promise = Promise.resolve(getStorageSync<string>(key));
} catch {
promise = Promise.resolve(null);
}
} else {
promise = getStorage<string>({ key }).then(({ data }) => data);
}

return promise.then(
(data) => {
return typeof data === 'string' ? data : null;
},
() => {
// 使用getStorage()找不到key时,Taro会抛出异常: getStorage:fail data not found
return null;
},
);
},
setItem(key, value) {
return setStorage({
key,
data: value,
});
},
removeItem(key) {
return removeStorage({
key,
});
},
clear() {
return clearStorage();
},
};
export { taroStorage } from './storage';
export { persistInterceptor } from './hoc';
37 changes: 37 additions & 0 deletions src/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { StorageEngine } from 'foca';
import {
getStorage,
setStorage,
removeStorage,
clearStorage,
} from '@tarojs/taro';

export const taroStorage: StorageEngine = {
getItem(key) {
return getStorage<string>({ key })
.then(({ data }) => data)
.then(
(data) => {
return typeof data === 'string' ? data : null;
},
() => {
// 使用getStorage()找不到key时,Taro会抛出异常: getStorage:fail data not found
return null;
},
);
},
setItem(key, value) {
return setStorage({
key,
data: value,
});
},
removeItem(key) {
return removeStorage({
key,
});
},
clear() {
return clearStorage();
},
};

0 comments on commit 9ff090a

Please sign in to comment.