aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-03-23 14:26:20 +0100
committerChocobozzz <me@florianbigard.com>2018-03-23 14:26:20 +0100
commit0bd78bf30b2ae159791bdc90d17ed18e0327f621 (patch)
treee8f69cef499a99c51deeea39f7197b7ff93a80a2 /client/src/app/shared
parent9c673970f66fe91c665e1e3905fa768f57e11a18 (diff)
downloadPeerTube-0bd78bf30b2ae159791bdc90d17ed18e0327f621.tar.gz
PeerTube-0bd78bf30b2ae159791bdc90d17ed18e0327f621.tar.zst
PeerTube-0bd78bf30b2ae159791bdc90d17ed18e0327f621.zip
Proxify local storage and handle if it is unavailable
Diffstat (limited to 'client/src/app/shared')
-rw-r--r--client/src/app/shared/misc/peertube-local-storage.ts70
-rw-r--r--client/src/app/shared/rest/rest-table.ts5
2 files changed, 73 insertions, 2 deletions
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 @@
1// Thanks: https://github.com/capaj/localstorage-polyfill
2
3const valuesMap = new Map()
4
5class MemoryStorage {
6 [key: string]: any
7 [index: number]: string
8
9 getItem (key) {
10 const stringKey = String(key)
11 if (valuesMap.has(key)) {
12 return String(valuesMap.get(stringKey))
13 }
14
15 return null
16 }
17
18 setItem (key, val) {
19 valuesMap.set(String(key), String(val))
20 }
21
22 removeItem (key) {
23 valuesMap.delete(key)
24 }
25
26 clear () {
27 valuesMap.clear()
28 }
29
30 key (i: any) {
31 if (arguments.length === 0) {
32 throw new TypeError('Failed to execute "key" on "Storage": 1 argument required, but only 0 present.')
33 }
34
35 const arr = Array.from(valuesMap.keys())
36 return arr[i]
37 }
38
39 get length () {
40 return valuesMap.size
41 }
42}
43
44let peertubeLocalStorage: Storage
45try {
46 peertubeLocalStorage = localStorage
47} catch (err) {
48 const instance = new MemoryStorage()
49
50 peertubeLocalStorage = new Proxy(instance, {
51 set: function (obj, prop, value) {
52 if (MemoryStorage.prototype.hasOwnProperty(prop)) {
53 instance[prop] = value
54 } else {
55 instance.setItem(prop, value)
56 }
57 return true
58 },
59 get: function (target, name) {
60 if (MemoryStorage.prototype.hasOwnProperty(name)) {
61 return instance[name]
62 }
63 if (valuesMap.has(name)) {
64 return instance.getItem(name)
65 }
66 }
67 })
68}
69
70export { 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 @@
1import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
1import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent' 2import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent'
2import { SortMeta } from 'primeng/components/common/sortmeta' 3import { SortMeta } from 'primeng/components/common/sortmeta'
3 4
@@ -15,7 +16,7 @@ export abstract class RestTable {
15 protected abstract loadData (): void 16 protected abstract loadData (): void
16 17
17 loadSort () { 18 loadSort () {
18 const result = localStorage.getItem(this.sortLocalStorageKey) 19 const result = peertubeLocalStorage.getItem(this.sortLocalStorageKey)
19 20
20 if (result) { 21 if (result) {
21 try { 22 try {
@@ -42,7 +43,7 @@ export abstract class RestTable {
42 } 43 }
43 44
44 saveSort () { 45 saveSort () {
45 localStorage.setItem(this.sortLocalStorageKey, JSON.stringify(this.sort)) 46 peertubeLocalStorage.setItem(this.sortLocalStorageKey, JSON.stringify(this.sort))
46 } 47 }
47 48
48} 49}