Skip to content

Commit

Permalink
(types/refactor): refactor code into TypeScript
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
agilgur5 committed Jul 11, 2019
1 parent 7b523ff commit f6ba720
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"name": "mst-persist",
"version": "0.0.2",
"description": "Persist and hydrate MobX-state-tree stores",
"main": "src/index.js",
"main": "dist/mst-persist.esm.js",
"files": [
"src/"
"dist/"
],
"scripts": {
"start": "tsdx watch",
Expand Down Expand Up @@ -33,6 +33,8 @@
"mobx-state-tree": "^3.2.1"
},
"devDependencies": {
"mobx": "^5.11.0",
"mobx-state-tree": "^3.14.0",
"tsdx": "^0.7.2",
"typescript": "^3.5.2"
}
Expand Down
11 changes: 9 additions & 2 deletions src/asyncLocalStorage.js → src/asyncLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export const AsyncLocalStorage = {
interface IAsyncLocalStorage {
clear(): Promise<void>
getItem(key: string): Promise<string>
removeItem(key: string): Promise<void>
setItem(key: string, value: string): Promise<void>
}

export const AsyncLocalStorage: IAsyncLocalStorage = {
clear () {
return callWithPromise(window.localStorage.clear)
},
Expand All @@ -13,7 +20,7 @@ export const AsyncLocalStorage = {
}
}

function callWithPromise (func, ...args) {
function callWithPromise (func: Function, ...args: any[]): Promise<any> {
try {
return Promise.resolve(func(...args))
} catch (err) {
Expand Down
18 changes: 14 additions & 4 deletions src/index.js → src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { onSnapshot, applySnapshot } from 'mobx-state-tree'

import AsyncLocalStorage from './asyncLocalStorage.js'
import AsyncLocalStorage from './asyncLocalStorage'

export const persist = (name, store, options = {}) => {
export interface IArgs {
(name: string, store: object, options?: IOptions): Promise<void>
}
export interface IOptions {
storage?: any,
jsonify?: boolean,
readonly whitelist?: Array<string>,
readonly blacklist?: Array<string>
}

export const persist: IArgs = (name, store, options = {}) => {
let {storage, jsonify, whitelist, blacklist} = options

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

return storage.getItem(name)
.then((data) => {
.then((data: any) => {
const snapshot = !jsonify ? data : JSON.parse(data)
// don't apply falsey (which will error), leave store in initial state
if (!snapshot) { return }
applySnapshot(store, snapshot)
})
}

function arrToDict (arr) {
function arrToDict (arr?: Array<string>): object {
if (!arr) { return {} }
return arr.reduce((dict, elem) => {
dict[elem] = true
Expand Down

0 comments on commit f6ba720

Please sign in to comment.