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

Commitf6ba720

Browse files
committed
(types/refactor): refactor code into TypeScript
- add a few interfaces for the types- add a few function signatures- no .js extension import, and no .ts extension either, TS only supports extensionless imports apparently(??)- no errors or warnings in current (non-strict) config - need to specify some `any`s a bit more however(deps): add mobx + MST to devDeps so that the types work
1 parent7b523ff commitf6ba720

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

‎package-lock.json‎

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"name":"mst-persist",
33
"version":"0.0.2",
44
"description":"Persist and hydrate MobX-state-tree stores",
5-
"main":"src/index.js",
5+
"main":"dist/mst-persist.esm.js",
66
"files": [
7-
"src/"
7+
"dist/"
88
],
99
"scripts": {
1010
"start":"tsdx watch",
@@ -33,6 +33,8 @@
3333
"mobx-state-tree":"^3.2.1"
3434
},
3535
"devDependencies": {
36+
"mobx":"^5.11.0",
37+
"mobx-state-tree":"^3.14.0",
3638
"tsdx":"^0.7.2",
3739
"typescript":"^3.5.2"
3840
}

‎src/asyncLocalStorage.js‎renamed to ‎src/asyncLocalStorage.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
exportconstAsyncLocalStorage={
1+
interfaceIAsyncLocalStorage{
2+
clear():Promise<void>
3+
getItem(key:string):Promise<string>
4+
removeItem(key:string):Promise<void>
5+
setItem(key:string,value:string):Promise<void>
6+
}
7+
8+
exportconstAsyncLocalStorage:IAsyncLocalStorage={
29
clear(){
310
returncallWithPromise(window.localStorage.clear)
411
},
@@ -13,7 +20,7 @@ export const AsyncLocalStorage = {
1320
}
1421
}
1522

16-
functioncallWithPromise(func, ...args){
23+
functioncallWithPromise(func:Function, ...args:any[]):Promise<any>{
1724
try{
1825
returnPromise.resolve(func(...args))
1926
}catch(err){

‎src/index.js‎renamed to ‎src/index.ts‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import{onSnapshot,applySnapshot}from'mobx-state-tree'
22

3-
importAsyncLocalStoragefrom'./asyncLocalStorage.js'
3+
importAsyncLocalStoragefrom'./asyncLocalStorage'
44

5-
exportconstpersist=(name,store,options={})=>{
5+
exportinterfaceIArgs{
6+
(name:string,store:object,options?:IOptions):Promise<void>
7+
}
8+
exportinterfaceIOptions{
9+
storage?:any,
10+
jsonify?:boolean,
11+
readonlywhitelist?:Array<string>,
12+
readonlyblacklist?:Array<string>
13+
}
14+
15+
exportconstpersist:IArgs=(name,store,options={})=>{
616
let{storage, jsonify, whitelist, blacklist}=options
717

818
if(typeofwindow.localStorage!=='undefined'&&(!storage||storage===window.localStorage)){
@@ -28,15 +38,15 @@ export const persist = (name, store, options = {}) => {
2838
})
2939

3040
returnstorage.getItem(name)
31-
.then((data)=>{
41+
.then((data:any)=>{
3242
constsnapshot=!jsonify ?data :JSON.parse(data)
3343
// don't apply falsey (which will error), leave store in initial state
3444
if(!snapshot){return}
3545
applySnapshot(store,snapshot)
3646
})
3747
}
3848

39-
functionarrToDict(arr){
49+
functionarrToDict(arr?:Array<string>):object{
4050
if(!arr){return{}}
4151
returnarr.reduce((dict,elem)=>{
4252
dict[elem]=true

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp