From 0bd78bf30b2ae159791bdc90d17ed18e0327f621 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 23 Mar 2018 14:26:20 +0100 Subject: Proxify local storage and handle if it is unavailable --- .../src/app/shared/misc/peertube-local-storage.ts | 70 ++++++++++++++++++++++ client/src/app/shared/rest/rest-table.ts | 5 +- 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 client/src/app/shared/misc/peertube-local-storage.ts (limited to 'client/src/app/shared') diff --git a/client/src/app/shared/misc/peertube-local-storage.ts b/client/src/app/shared/misc/peertube-local-storage.ts new file mode 100644 index 000000000..ad761c82f --- /dev/null +++ b/client/src/app/shared/misc/peertube-local-storage.ts @@ -0,0 +1,70 @@ +// Thanks: https://github.com/capaj/localstorage-polyfill + +const valuesMap = new Map() + +class MemoryStorage { + [key: string]: any + [index: number]: string + + getItem (key) { + const stringKey = String(key) + if (valuesMap.has(key)) { + return String(valuesMap.get(stringKey)) + } + + return null + } + + setItem (key, val) { + valuesMap.set(String(key), String(val)) + } + + removeItem (key) { + valuesMap.delete(key) + } + + clear () { + valuesMap.clear() + } + + key (i: any) { + if (arguments.length === 0) { + throw new TypeError('Failed to execute "key" on "Storage": 1 argument required, but only 0 present.') + } + + const arr = Array.from(valuesMap.keys()) + return arr[i] + } + + get length () { + return valuesMap.size + } +} + +let peertubeLocalStorage: Storage +try { + peertubeLocalStorage = localStorage +} catch (err) { + const instance = new MemoryStorage() + + peertubeLocalStorage = new Proxy(instance, { + set: function (obj, prop, value) { + if (MemoryStorage.prototype.hasOwnProperty(prop)) { + instance[prop] = value + } else { + instance.setItem(prop, value) + } + return true + }, + get: function (target, name) { + if (MemoryStorage.prototype.hasOwnProperty(name)) { + return instance[name] + } + if (valuesMap.has(name)) { + return instance.getItem(name) + } + } + }) +} + +export { peertubeLocalStorage } diff --git a/client/src/app/shared/rest/rest-table.ts b/client/src/app/shared/rest/rest-table.ts index 165fc4e45..fe1a91d2d 100644 --- a/client/src/app/shared/rest/rest-table.ts +++ b/client/src/app/shared/rest/rest-table.ts @@ -1,3 +1,4 @@ +import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage' import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent' import { SortMeta } from 'primeng/components/common/sortmeta' @@ -15,7 +16,7 @@ export abstract class RestTable { protected abstract loadData (): void loadSort () { - const result = localStorage.getItem(this.sortLocalStorageKey) + const result = peertubeLocalStorage.getItem(this.sortLocalStorageKey) if (result) { try { @@ -42,7 +43,7 @@ export abstract class RestTable { } saveSort () { - localStorage.setItem(this.sortLocalStorageKey, JSON.stringify(this.sort)) + peertubeLocalStorage.setItem(this.sortLocalStorageKey, JSON.stringify(this.sort)) } } -- cgit v1.2.3