File tree Expand file tree Collapse file tree 4 files changed +22
-6
lines changed
Expand file tree Collapse file tree 4 files changed +22
-6
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import { debounce } from 'decko';
33import codeExample from './code-example.txt' ;
44import todoExample from './todo-example.txt' ;
55import style from './style' ;
6+ import { localStorageGet , localStorageSet } from '../../../lib/localstorage' ;
67
78const EXAMPLES = [
89 {
@@ -18,7 +19,7 @@ const EXAMPLES = [
1819export 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 } ) {
Original file line number Diff line number Diff line change 11import { h , Component } from 'preact' ;
22import { memoize } from 'decko' ;
3+ import { localStorageGet , localStorageSet } from '../lib/localstorage' ;
34
45const 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
1213export 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 } ;
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 11import createStore from './lib/store' ;
22import getDefaultLanguage from './lib/default-language' ;
33import config from './config' ;
4+ import { localStorageSet , localStorageGet } from './lib/localstorage' ;
45
56const SAVE = [ 'lang' ] ;
67
@@ -22,13 +23,13 @@ export default () => {
2223function 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
2829function 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}
You can’t perform that action at this time.
0 commit comments