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

Commit4223a23

Browse files
committed
initial commit
- just the source code itself
0 parents  commit4223a23

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

‎localStorageAdaptor.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
exportfunctionclear(){
2+
returncallWithPromise(window.localStorage.clear)
3+
}
4+
5+
exportfunctiongetItem(key){
6+
returncallWithPromise(window.localStorage.getItem,key)
7+
}
8+
9+
exportfunctionremoveItem(key){
10+
returncallWithPromise(window.localStorage.removeItem,key)
11+
}
12+
13+
exportfunctionsetItem(key,value){
14+
returncallWithPromise(window.localStorage.setItem,key,value)
15+
}
16+
17+
functioncallWithPromise(func, ...args){
18+
try{
19+
returnPromise.resolve(func(...args))
20+
}catch(err){
21+
returnPromise.reject(err)
22+
}
23+
}

‎persist.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import{onSnapshot,applySnapshot}from'mobx-state-tree'
2+
3+
import*asLocalStoragefrom'./localStorageAdaptor.js'
4+
5+
exportconstpersist=(name,store,options={})=>{
6+
let{storage, whitelist, blacklist, jsonify}=options
7+
8+
if(typeofwindow.localStorage!=='undefined'&&window.localStorage===storage){
9+
storage=LocalStorage
10+
}
11+
constwhitelistDict=arrToDict(whitelist)
12+
constblacklistDict=arrToDict(blacklist)
13+
if(!jsonify){jsonify=true}// default to true like mobx-persist
14+
15+
onSnapshot(store,(_snapshot)=>{
16+
constsnapshot={ ..._snapshot}
17+
Object.keys(snapshot).forEach((key)=>{
18+
if(whitelist&&!whitelistDict[key]){
19+
deletesnapshot[key]
20+
}
21+
if(blacklist&&blacklistDict[key]){
22+
deletesnapshot[key]
23+
}
24+
})
25+
26+
constdata=!jsonify ?snapshot :JSON.stringify(snapshot)
27+
storage.setItem(name,data)
28+
})
29+
30+
returnstorage.getItem(name)
31+
.then((data)=>{
32+
constsnapshot=!jsonify ?data :JSON.parse(data)
33+
applySnapshot(store,snapshot)
34+
})
35+
}
36+
37+
functionarrToDict(arr){
38+
if(!arr){return{}}
39+
returnarr.reduce((dict,elem)=>{
40+
dict[elem]=true
41+
returndict
42+
},{})
43+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp