Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc47523a

Browse files
committed
(types): be more specific in various places
- IStateTreeNode is the type passed to onSnapshot and applySnapshot- snapshot from onSnapshot _should_ always have string keys- Storage.getItem should always return a string or object - add a typeguard here to properly ensure behavior similar to pattern-matching - my first typeguard woo! - be more general than just checking `jsonify` as we can't have a string snapshot anyway (though checking `jsonify` might be more efficient than `isString`)
1 parent2b63093 commitc47523a

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

‎src/index.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
import{onSnapshot,applySnapshot}from'mobx-state-tree'
1+
import{onSnapshot,applySnapshot,IStateTreeNode}from'mobx-state-tree'
22

33
importAsyncLocalStoragefrom'./asyncLocalStorage'
44

55
exportinterfaceIArgs{
6-
(name:string,store:object,options?:IOptions):Promise<void>
6+
(name:string,store:IStateTreeNode,options?:IOptions):Promise<void>
77
}
88
exportinterfaceIOptions{
99
storage?:any,
1010
jsonify?:boolean,
1111
readonlywhitelist?:Array<string>,
1212
readonlyblacklist?:Array<string>
1313
}
14+
typeStrToAnyMap={[key:string]:any}
1415

1516
exportconstpersist:IArgs=(name,store,options={})=>{
1617
let{storage, jsonify, whitelist, blacklist}=options
@@ -22,7 +23,7 @@ export const persist: IArgs = (name, store, options = {}) => {
2223
constwhitelistDict=arrToDict(whitelist)
2324
constblacklistDict=arrToDict(blacklist)
2425

25-
onSnapshot(store,(_snapshot:any)=>{
26+
onSnapshot(store,(_snapshot:StrToAnyMap)=>{
2627
constsnapshot={ ..._snapshot}
2728
Object.keys(snapshot).forEach((key)=>{
2829
if(whitelist&&!whitelistDict[key]){
@@ -38,8 +39,8 @@ export const persist: IArgs = (name, store, options = {}) => {
3839
})
3940

4041
returnstorage.getItem(name)
41-
.then((data:any)=>{
42-
constsnapshot=!jsonify ?data :JSON.parse(data)
42+
.then((data:object|string)=>{
43+
constsnapshot=!isString(data) ?data :JSON.parse(data)
4344
// don't apply falsey (which will error), leave store in initial state
4445
if(!snapshot){return}
4546
applySnapshot(store,snapshot)
@@ -56,4 +57,8 @@ function arrToDict (arr?: Array<string>): StrToBoolMap {
5657
},{})
5758
}
5859

60+
functionisString(value:any):value isstring{
61+
returntypeofvalue==='string'
62+
}
63+
5964
exportdefaultpersist

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp