Skip to content

Commit

Permalink
adds a test case, changes dispose->destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
capaj committed Jan 29, 2018
1 parent 65e230a commit 3cd5150
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ a simple utility for persisting POJO objects into localStorage via mobx observab

```javascript
import { localStored } from 'mobx-stored'
import { sessionStored } from 'mobx-stored'
import { sessionStored } from 'mobx-stored' // for using sessionStorage rather than localStorage

const defaultUser = { email: null, firstname: null, lastname: null }
const observableUserProfile = localStored('userProfile', defaultUser, 500) // last parameter is optional-miliseconds how often do you want to save into localStorage. It is advised to use bigger value with bigger stores
Expand All @@ -23,7 +23,13 @@ observableUserProfile.name === 'Michael' // true

observableUserProfile.reset()

// need to add new properties?

observableUserProfile.extend({
myNewProp: 1
})

//Don't need it anymore?

observableUserProfile.dispose()
observableUserProfile.destroy()
```
22 changes: 6 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mobx-stored",
"version": "0.9.8",
"version": "0.9.9",
"description": "localstorage persisted observables",
"main": "dist/stored-observable.cjs.js",
"module": "dist/stored-observable.es.js",
Expand All @@ -9,17 +9,13 @@
"test": "BABEL_ENV=ava ava",
"dev": "BABEL_ENV=ava ava -w",
"pretest": "standard",
"prepublish": "npm run build"
"prepublish": "npm test && npm run build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/capaj/mobx-stored.git"
},
"keywords": [
"mobx",
"observable",
"localstorage"
],
"keywords": ["mobx", "observable", "localstorage"],
"author": "[email protected]",
"license": "MIT",
"bugs": {
Expand Down Expand Up @@ -52,12 +48,8 @@
"mobx": "^3.4.1"
},
"ava": {
"files": [
"**/*.spec.js"
],
"require": [
"babel-register"
],
"files": ["**/*.spec.js"],
"require": ["babel-register"],
"babel": "inherit"
},
"dependencies": {
Expand All @@ -66,8 +58,6 @@
"traverse": "^0.6.6"
},
"standard": {
"ignore": [
"stored-observable.js"
]
"ignore": ["stored-observable.js"]
}
}
9 changes: 8 additions & 1 deletion src/stored-observable.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@ function factory(storage) {
window.addEventListener('storage', propagateChangesToMemory)

obsVal.reset = () => {
disposeAutorun && disposeAutorun()
extendObservable(obsVal, defaultValue)
establishAutorun()
}
obsVal.dispose = () => {
obsVal.extend = obj => {
disposeAutorun && disposeAutorun()
extendObservable(obsVal, obj)
establishAutorun()
}
obsVal.destroy = () => {
disposeAutorun()
storage.removeItem(key)
window.removeEventListener('storage', propagateChangesToMemory)
Expand Down
16 changes: 13 additions & 3 deletions src/stored-observable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import test from 'ava'
import { localStored } from './stored-observable'

global.window = {
addEventListener() {},
removeEventListener() {}
addEventListener () {},
removeEventListener () {}
}

test.cb('saves in localStorage, resets and disposes', t => {
Expand All @@ -22,7 +22,7 @@ test.cb('saves in localStorage, resets and disposes', t => {
t.is(obs.a, 1)
setTimeout(() => {
t.is(localStorage.test, '{"a":1,"b":2}')
obs.dispose()
obs.destroy()
t.is(localStorage.test, undefined)
t.end()
}, 2)
Expand All @@ -47,3 +47,13 @@ test.cb('extends existing value', t => {
t.is(obs.c, 0)
t.end()
})

test.cb('can extend stored observable itself', t => {
const def = { a: 1, b: 2, c: 3 }
const obs = localStored('testFour', def, 1)
obs.extend({ d: 4 })
setTimeout(() => {
t.is(localStorage.testFour, '{"a":1,"b":2,"c":3,"d":4}')
t.end()
}, 2)
})

0 comments on commit 3cd5150

Please sign in to comment.