@fuels/local-storage
Advanced tools
Comparing version 0.25.1 to 0.26.0-main-5a9cf49d6b39cb2fb9e9cb5465b117fdab7bf954
# @fuels/local-storage | ||
## 0.26.0-main-5a9cf49d6b39cb2fb9e9cb5465b117fdab7bf954 | ||
### Minor Changes | ||
- Check for localStorage availability, by [@nelitow](https://github.com/nelitow) (See [#132](https://github.com/FuelLabs/fuels-npm-packs/pull/132)) | ||
## 0.25.1 | ||
@@ -4,0 +10,0 @@ |
@@ -6,3 +6,5 @@ import { EventEmitter } from 'events'; | ||
private emitter; | ||
private isAvailable; | ||
constructor(prefix: string, emitter?: EventEmitter); | ||
private checkLocalStorageAvailability; | ||
subscribe: (listener: <T extends unknown[]>(...args: T) => void) => () => void; | ||
@@ -9,0 +11,0 @@ setItem: <T>(key: string, value: T) => void; |
@@ -18,6 +18,13 @@ 'use strict'; | ||
this.setItem = (key, value) => { | ||
localStorage.setItem(this.createKey(key), JSON.stringify(value)); | ||
this.dispatchChange(key, value); | ||
if (!this.isAvailable) | ||
return; | ||
try { | ||
localStorage.setItem(this.createKey(key), JSON.stringify(value)); | ||
this.dispatchChange(key, value); | ||
} catch (error) { | ||
} | ||
}; | ||
this.getItem = (key) => { | ||
if (!this.isAvailable) | ||
return null; | ||
try { | ||
@@ -31,12 +38,35 @@ const data = localStorage.getItem(this.createKey(key)); | ||
this.clear = () => { | ||
Object.keys(localStorage).filter((key) => key.startsWith(this.prefix)).forEach((key) => localStorage.removeItem(key)); | ||
this.dispatchChange(); | ||
if (!this.isAvailable) | ||
return; | ||
try { | ||
Object.keys(localStorage).filter((key) => key.startsWith(this.prefix)).forEach((key) => localStorage.removeItem(key)); | ||
this.dispatchChange(); | ||
} catch (error) { | ||
} | ||
}; | ||
this.removeItem = (key) => { | ||
localStorage.removeItem(this.createKey(key)); | ||
this.dispatchChange(); | ||
if (!this.isAvailable) | ||
return; | ||
try { | ||
localStorage.removeItem(this.createKey(key)); | ||
this.dispatchChange(); | ||
} catch (error) { | ||
} | ||
}; | ||
this.prefix = prefix; | ||
this.emitter = emitter ?? new events.EventEmitter(); | ||
this.isAvailable = this.checkLocalStorageAvailability(); | ||
} | ||
checkLocalStorageAvailability() { | ||
try { | ||
if (typeof window === "undefined" || !window.localStorage) | ||
return false; | ||
const testKey = "__storage_test__"; | ||
localStorage.setItem(testKey, testKey); | ||
localStorage.removeItem(testKey); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
// --------------------------------------------------------------------------- | ||
@@ -43,0 +73,0 @@ // Private methods |
@@ -6,3 +6,5 @@ /// <reference types="node" /> | ||
private emitter; | ||
private isAvailable; | ||
constructor(prefix: string, emitter?: EventEmitter); | ||
private checkLocalStorageAvailability; | ||
subscribe: (listener: <T extends unknown[]>(...args: T) => void) => () => void; | ||
@@ -9,0 +11,0 @@ setItem: <T>(key: string, value: T) => void; |
{ | ||
"name": "@fuels/local-storage", | ||
"version": "0.25.1", | ||
"version": "0.26.0-main-5a9cf49d6b39cb2fb9e9cb5465b117fdab7bf954", | ||
"license": "Apache-2.0", | ||
@@ -14,5 +14,5 @@ "main": "dist/index.js", | ||
"jest-localstorage-mock": "^2.4.26", | ||
"@fuels/ts-config": "0.25.1", | ||
"@fuels/jest": "0.25.1", | ||
"@fuels/tsup-config": "0.25.1" | ||
"@fuels/jest": "0.26.0-main-5a9cf49d6b39cb2fb9e9cb5465b117fdab7bf954", | ||
"@fuels/ts-config": "0.26.0-main-5a9cf49d6b39cb2fb9e9cb5465b117fdab7bf954", | ||
"@fuels/tsup-config": "0.26.0-main-5a9cf49d6b39cb2fb9e9cb5465b117fdab7bf954" | ||
}, | ||
@@ -19,0 +19,0 @@ "scripts": { |
@@ -6,2 +6,3 @@ import { EventEmitter } from 'events'; | ||
private emitter!: EventEmitter; | ||
private isAvailable: boolean; | ||
@@ -11,4 +12,17 @@ constructor(prefix: string, emitter?: EventEmitter) { | ||
this.emitter = emitter ?? new EventEmitter(); | ||
this.isAvailable = this.checkLocalStorageAvailability(); | ||
} | ||
private checkLocalStorageAvailability(): boolean { | ||
try { | ||
if (typeof window === 'undefined' || !window.localStorage) return false; | ||
const testKey = '__storage_test__'; | ||
localStorage.setItem(testKey, testKey); | ||
localStorage.removeItem(testKey); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} | ||
subscribe = (listener: <T extends unknown[]>(...args: T) => void) => { | ||
@@ -23,7 +37,13 @@ if (!this.emitter) return () => {}; | ||
setItem = <T>(key: string, value: T) => { | ||
localStorage.setItem(this.createKey(key), JSON.stringify(value)); | ||
this.dispatchChange(key, value); | ||
if (!this.isAvailable) return; | ||
try { | ||
localStorage.setItem(this.createKey(key), JSON.stringify(value)); | ||
this.dispatchChange(key, value); | ||
} catch (error) { | ||
// Silently fail if localStorage is not available or quota is exceeded | ||
} | ||
}; | ||
getItem = <T>(key: string): T | null => { | ||
if (!this.isAvailable) return null; | ||
try { | ||
@@ -38,11 +58,21 @@ const data = localStorage.getItem(this.createKey(key)); | ||
clear = () => { | ||
Object.keys(localStorage) | ||
.filter((key) => key.startsWith(this.prefix)) | ||
.forEach((key) => localStorage.removeItem(key)); | ||
this.dispatchChange(); | ||
if (!this.isAvailable) return; | ||
try { | ||
Object.keys(localStorage) | ||
.filter((key) => key.startsWith(this.prefix)) | ||
.forEach((key) => localStorage.removeItem(key)); | ||
this.dispatchChange(); | ||
} catch (error) { | ||
// Silently fail if localStorage operations fail | ||
} | ||
}; | ||
removeItem = (key: string) => { | ||
localStorage.removeItem(this.createKey(key)); | ||
this.dispatchChange(); | ||
if (!this.isAvailable) return; | ||
try { | ||
localStorage.removeItem(this.createKey(key)); | ||
this.dispatchChange(); | ||
} catch (error) { | ||
// Silently fail if localStorage operations fail | ||
} | ||
}; | ||
@@ -49,0 +79,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
33673
363