Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/components/controllers/repl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { debounce } from 'decko';
import codeExample from './code-example.txt';
import todoExample from './todo-example.txt';
import style from './style';
import { localStorageGet, localStorageSet } from '../../../lib/localstorage';

const EXAMPLES = [
{
Expand All @@ -18,7 +19,7 @@ const EXAMPLES = [
export default class Repl extends Component {
state = {
loading: 'Initializing...',
code: localStorage.getItem('preact-www-repl-code') || codeExample
code: localStorageGet('preact-www-repl-code') || codeExample
};

constructor(props, context) {
Expand Down Expand Up @@ -87,7 +88,7 @@ export default class Repl extends Component {
componentDidUpdate = debounce(500, () => {
let { code } = this.state;
if (code===codeExample) code = '';
localStorage.setItem('preact-www-repl-code', code || '');
localStorageSet('preact-www-repl-code', code || '');
})

componentWillReceiveProps({ code }) {
Expand Down
5 changes: 3 additions & 2 deletions src/components/github-stars.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { h, Component } from 'preact';
import { memoize } from 'decko';
import { localStorageGet, localStorageSet } from '../lib/localstorage';

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

export default class GithubStars extends Component {
state = {
stars: localStorage._stars || ''
stars: localStorageGet('_stars') || ''
};

setStars = stars => {
if (stars && stars!=this.state.stars) {
localStorage._stars = stars;
localStorageSet('_stars', stars);
this.setState({ stars });
}
};
Expand Down
13 changes: 13 additions & 0 deletions src/lib/localstorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const localStorageGet = (key) => {
try {
return localStorage.getItem(key);
} catch (e) {
return null;
}
};

export const localStorageSet = (key, value) => {
try {
localStorage.setItem(key, value);
} catch (e) {}
};
5 changes: 3 additions & 2 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import createStore from './lib/store';
import getDefaultLanguage from './lib/default-language';
import config from './config';
import { localStorageSet, localStorageGet } from './lib/localstorage';

const SAVE = ['lang'];

Expand All @@ -22,13 +23,13 @@ export default () => {
function saveState(state) {
let saved = {};
for (let i=SAVE.length; i--; ) saved[SAVE[i]] = state[SAVE[i]];
localStorage.state = JSON.stringify(saved);
localStorageSet('state', JSON.stringify(saved));
}

function getSavedState() {
let state;
try {
state = JSON.parse(localStorage.state);
state = JSON.parse(localStorageGet('state'));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is already wrapped in a try-catch, so it was safe already, but i’ve used localStorageGet here for consistency.

} catch (e) {}
return state || {};
}