-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
geekact
committed
Apr 13, 2022
1 parent
c55e752
commit 9ff090a
Showing
4 changed files
with
97 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}, | ||
}; |