aihornmac / mobx-localstorage Goto Github PK
View Code? Open in Web Editor NEWA declarative reactive localStorage using MobX
License: MIT License
A declarative reactive localStorage using MobX
License: MIT License
import localStorageMobx from "mobx-localstorage"
console.log(localStorageMobx.getItem("foo") // null -- incorrect
console.log(localStorage.getItem("foo") // bar -- correct
For some reason, all values returned by mobx-localstorage are null but using the normal localstorage returns the correct value. Seems to have broken in v1.1.1, because it doesn't work in 1.1.1 or 1.1.2, but does work in 1.1.0.
Function: getItem
Expected: null result for a key that doesn't exist
Actual result: got underfined for a key that doesn't exist
https://html.spec.whatwg.org/multipage/webstorage.html
The getItem(key) method must return the current value associated with the given key. If the given key does not exist in the list associated with the object then this method must return null.
Is there any reason why strings saved into localstorage are in quotes "string"
?
The issue is that when MobX is configured to always enforce actions, this library does not respect the pattern that all observable properties can only be modified with a function decorated as an action.
The main issue is in the constructor in the call super.set()
and super.delete()
:
constructor() {
super(undefined, referenceEnhancer, 'LocalStorage')
const len = localStorage.length
for (let i = 0; i < len; i++) {
const key = localStorage.key(i)!
const value = parseValue(localStorage.getItem(key))
super.set(key, value)
}
localStorage.on('change', (key, value) => {
if (typeof value === 'string') {
super.set(key, parseValue(value))
} else {
super.delete(key)
}
})
}
Also, the class functions for setting / removing keys should probably also be decorated as actions to be safe.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.