Skip to content

Commit 093021e

Browse files
molilydevelopit
authored andcommitted
Guard the usage of localStorage to catch exceptions. (#270)
1 parent 371b0af commit 093021e

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

src/components/controllers/repl/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { debounce } from 'decko';
33
import codeExample from './code-example.txt';
44
import todoExample from './todo-example.txt';
55
import style from './style';
6+
import { localStorageGet, localStorageSet } from '../../../lib/localstorage';
67

78
const EXAMPLES = [
89
{
@@ -18,7 +19,7 @@ const EXAMPLES = [
1819
export default class Repl extends Component {
1920
state = {
2021
loading: 'Initializing...',
21-
code: localStorage.getItem('preact-www-repl-code') || codeExample
22+
code: localStorageGet('preact-www-repl-code') || codeExample
2223
};
2324

2425
constructor(props, context) {
@@ -87,7 +88,7 @@ export default class Repl extends Component {
8788
componentDidUpdate = debounce(500, () => {
8889
let { code } = this.state;
8990
if (code===codeExample) code = '';
90-
localStorage.setItem('preact-www-repl-code', code || '');
91+
localStorageSet('preact-www-repl-code', code || '');
9192
})
9293

9394
componentWillReceiveProps({ code }) {

src/components/github-stars.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { h, Component } from 'preact';
22
import { memoize } from 'decko';
3+
import { localStorageGet, localStorageSet } from '../lib/localstorage';
34

45
const githubStars = memoize( repo => fetch('//api.github.com/repos/'+repo)
56
.then( r => r.json() )
@@ -11,12 +12,12 @@ if (typeof window!=='undefined') window.githubStars = githubStars;
1112

1213
export default class GithubStars extends Component {
1314
state = {
14-
stars: localStorage._stars || ''
15+
stars: localStorageGet('_stars') || ''
1516
};
1617

1718
setStars = stars => {
1819
if (stars && stars!=this.state.stars) {
19-
localStorage._stars = stars;
20+
localStorageSet('_stars', stars);
2021
this.setState({ stars });
2122
}
2223
};

src/lib/localstorage.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export const localStorageGet = (key) => {
2+
try {
3+
return localStorage.getItem(key);
4+
} catch (e) {
5+
return null;
6+
}
7+
};
8+
9+
export const localStorageSet = (key, value) => {
10+
try {
11+
localStorage.setItem(key, value);
12+
} catch (e) {}
13+
};

src/store.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import createStore from './lib/store';
22
import getDefaultLanguage from './lib/default-language';
33
import config from './config';
4+
import { localStorageSet, localStorageGet } from './lib/localstorage';
45

56
const SAVE = ['lang'];
67

@@ -22,13 +23,13 @@ export default () => {
2223
function saveState(state) {
2324
let saved = {};
2425
for (let i=SAVE.length; i--; ) saved[SAVE[i]] = state[SAVE[i]];
25-
localStorage.state = JSON.stringify(saved);
26+
localStorageSet('state', JSON.stringify(saved));
2627
}
2728

2829
function getSavedState() {
2930
let state;
3031
try {
31-
state = JSON.parse(localStorage.state);
32+
state = JSON.parse(localStorageGet('state'));
3233
} catch (e) {}
3334
return state || {};
3435
}

0 commit comments

Comments
 (0)