]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/root-helpers/peertube-web-storage.ts
Refactor a little bit watch html
[github/Chocobozzz/PeerTube.git] / client / src / root-helpers / peertube-web-storage.ts
index 0db1301bd4fa4846b1e6d3096e0417d49849c527..d4cad8a20aa74f6c25ab24ede76117292c590691 100644 (file)
@@ -4,17 +4,19 @@ const valuesMap = new Map()
 
 function proxify (instance: MemoryStorage) {
   return new Proxy(instance, {
-    set: function (obj, prop: string | number, value) {
+    set: function (obj, prop: string | symbol, value) {
       if (MemoryStorage.prototype.hasOwnProperty(prop)) {
-        instance[prop] = value
+        // FIXME: symbol typing issue https://github.com/microsoft/TypeScript/issues/1863
+        instance[prop as any] = value
       } else {
         instance.setItem(prop, value)
       }
       return true
     },
-    get: function (target, name: string | number) {
+    get: function (target, name: string | symbol | number) {
       if (MemoryStorage.prototype.hasOwnProperty(name)) {
-        return instance[name]
+        // FIXME: symbol typing issue https://github.com/microsoft/TypeScript/issues/1863
+        return instance[name as any]
       }
       if (valuesMap.has(name)) {
         return instance.getItem(name)
@@ -23,9 +25,8 @@ function proxify (instance: MemoryStorage) {
   })
 }
 
-class MemoryStorage {
+class MemoryStorage implements Storage {
   [key: string]: any
-  [index: number]: string
 
   getItem (key: any) {
     const stringKey = String(key)
@@ -64,10 +65,8 @@ class MemoryStorage {
 
 let peertubeLocalStorage: Storage
 let peertubeSessionStorage: Storage
-try {
-  peertubeLocalStorage = localStorage
-  peertubeSessionStorage = sessionStorage
-} catch (err) {
+
+function reinitStorage () {
   const instanceLocalStorage = new MemoryStorage()
   const instanceSessionStorage = new MemoryStorage()
 
@@ -75,6 +74,19 @@ try {
   peertubeSessionStorage = proxify(instanceSessionStorage)
 }
 
+try {
+  peertubeLocalStorage = localStorage
+  peertubeSessionStorage = sessionStorage
+} catch (err) {
+  // support Firefox and other browsers using an exception rather than null
+  reinitStorage()
+}
+
+// support Brave and other browsers using null rather than an exception
+if (peertubeLocalStorage === null || peertubeSessionStorage === null) {
+  reinitStorage()
+}
+
 export {
   peertubeLocalStorage,
   peertubeSessionStorage