Skip to content

Instantly share code, notes, and snippets.

@whisher
Created August 3, 2018 16:13
Show Gist options
  • Save whisher/bc00aca961aa2ea9a005c842eececbbe to your computer and use it in GitHub Desktop.
Save whisher/bc00aca961aa2ea9a005c842eececbbe to your computer and use it in GitHub Desktop.
export const customStorage: Storage = {
length: 0,
clear: function(): void {
if (window && window.localStorage) {
window.localStorage.clear();
this.length = window.localStorage.length;
}
},
getItem: function(key: string): string | null {
try {
return window.localStorage.getItem(key);
} catch (err) {
return null;
}
},
key: function(index: number): string | null {
try {
return window.localStorage.key(index);
} catch (err) {
return null;
}
},
removeItem: function(key: string): void {
try {
window.localStorage.removeItem(key);
this.length = window.localStorage.length;
} catch (err) {
return;
}
},
setItem: function(key: string, data: string): void {
try {
window.localStorage.setItem(key, data);
this.length = window.localStorage.length;
} catch (err) {
return;
}
}
};
export function localStorageSyncReducer(reducer: ActionReducer<any>): ActionReducer<any> {
return localStorageSync({
keys: [{auth:['status','account']}],
rehydrate: true,
storage: customStorage
})(reducer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment