@aerian/local-storage
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -18,2 +18,3 @@ interface KVP<K, V> { | ||
} | ||
export declare const createLocalStorageChangedEvent: <TValue>(payload: KVP<string, TValue>) => CustomEvent<KVP<string, TValue>>; | ||
/** | ||
@@ -20,0 +21,0 @@ * Checks if the event that is passed in is the same type as LocalStorageChanged. |
@@ -30,6 +30,7 @@ "use strict"; | ||
} | ||
LocalStorageChanged.eventName = 'onLocalStorageChange'; | ||
LocalStorageChanged.eventName = "onLocalStorageChange"; | ||
return LocalStorageChanged; | ||
}(CustomEvent)); | ||
exports.LocalStorageChanged = LocalStorageChanged; | ||
exports.createLocalStorageChangedEvent = function (payload) { return new CustomEvent(LocalStorageChanged.eventName, { detail: payload }); }; | ||
/** | ||
@@ -44,3 +45,5 @@ * Checks if the event that is passed in is the same type as LocalStorageChanged. | ||
function isTypeOfLocalStorageChanged(evt) { | ||
return (!!evt) && (evt instanceof LocalStorageChanged || (evt.detail && evt.type === LocalStorageChanged.eventName)); | ||
return (!!evt && | ||
(evt instanceof LocalStorageChanged || | ||
(evt.detail && evt.type === LocalStorageChanged.eventName))); | ||
} | ||
@@ -64,10 +67,11 @@ exports.isTypeOfLocalStorageChanged = isTypeOfLocalStorageChanged; | ||
try { | ||
localStorage.setItem(key, typeof value === 'object' ? JSON.stringify(value) : "" + value); | ||
window.dispatchEvent(new LocalStorageChanged({ key: key, value: value })); | ||
localStorage.setItem(key, typeof value === "object" ? JSON.stringify(value) : "" + value); | ||
window.dispatchEvent(exports.createLocalStorageChangedEvent({ key: key, value: value })); | ||
} | ||
catch (err) { | ||
if (err instanceof TypeError && err.message.includes('circular structure')) { | ||
throw new TypeError('The object that was given to the writeStorage function has circular references.\n' + | ||
'For more information, check here: ' + | ||
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value'); | ||
if (err instanceof TypeError && | ||
err.message.includes("circular structure")) { | ||
throw new TypeError("The object that was given to the writeStorage function has circular references.\n" + | ||
"For more information, check here: " + | ||
"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value"); | ||
} | ||
@@ -97,5 +101,5 @@ throw err; | ||
localStorage.removeItem(key); | ||
window.dispatchEvent(new LocalStorageChanged({ key: key, value: '' })); | ||
window.dispatchEvent(new LocalStorageChanged({ key: key, value: "" })); | ||
} | ||
exports.deleteFromStorage = deleteFromStorage; | ||
//# sourceMappingURL=local-storage-events.js.map |
{ | ||
"name": "@aerian/local-storage", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "React hook for local-storage", | ||
@@ -41,2 +41,3 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@babel/core": "^7.7.7", | ||
"@babel/preset-env": "^7.4.5", | ||
@@ -43,0 +44,0 @@ "@babel/preset-typescript": "^7.3.3", |
interface KVP<K, V> { | ||
key: K, | ||
value: V | ||
key: K; | ||
value: V; | ||
} | ||
/** | ||
@@ -16,10 +15,16 @@ * Used for creating new events for LocalStorage. This enables us to | ||
*/ | ||
export class LocalStorageChanged<TValue> extends CustomEvent<KVP<string, TValue>> { | ||
static eventName = 'onLocalStorageChange'; | ||
export class LocalStorageChanged<TValue> extends CustomEvent< | ||
KVP<string, TValue> | ||
> { | ||
static eventName = "onLocalStorageChange"; | ||
constructor(payload: KVP<string, TValue>) { | ||
super(LocalStorageChanged.eventName, { detail: payload }); | ||
} | ||
constructor(payload: KVP<string, TValue>) { | ||
super(LocalStorageChanged.eventName, { detail: payload }); | ||
} | ||
} | ||
export const createLocalStorageChangedEvent = <TValue>( | ||
payload: KVP<string, TValue> | ||
) => new CustomEvent(LocalStorageChanged.eventName, { detail: payload }); | ||
/** | ||
@@ -33,4 +38,10 @@ * Checks if the event that is passed in is the same type as LocalStorageChanged. | ||
*/ | ||
export function isTypeOfLocalStorageChanged<TValue>(evt: any): evt is LocalStorageChanged<TValue> { | ||
return (!!evt) && (evt instanceof LocalStorageChanged || (evt.detail && evt.type === LocalStorageChanged.eventName)); | ||
export function isTypeOfLocalStorageChanged<TValue>( | ||
evt: any | ||
): evt is LocalStorageChanged<TValue> { | ||
return ( | ||
!!evt && | ||
(evt instanceof LocalStorageChanged || | ||
(evt.detail && evt.type === LocalStorageChanged.eventName)) | ||
); | ||
} | ||
@@ -41,3 +52,3 @@ | ||
* in order to correctly send events within the same window. | ||
* | ||
* | ||
* @example | ||
@@ -48,3 +59,3 @@ * ```js | ||
* ``` | ||
* | ||
* | ||
* @export | ||
@@ -55,18 +66,23 @@ * @param {string} key The key to write to in the localStorage. | ||
export function writeStorage<TValue>(key: string, value: TValue) { | ||
try { | ||
localStorage.setItem(key, typeof value === 'object' ? JSON.stringify(value) : `${value}`); | ||
window.dispatchEvent(new LocalStorageChanged({ key, value })); | ||
} catch (err) { | ||
if (err instanceof TypeError && err.message.includes('circular structure')) { | ||
throw new TypeError( | ||
'The object that was given to the writeStorage function has circular references.\n' + | ||
'For more information, check here: ' + | ||
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value' | ||
); | ||
} | ||
throw err; | ||
try { | ||
localStorage.setItem( | ||
key, | ||
typeof value === "object" ? JSON.stringify(value) : `${value}` | ||
); | ||
window.dispatchEvent(createLocalStorageChangedEvent({ key, value })); | ||
} catch (err) { | ||
if ( | ||
err instanceof TypeError && | ||
err.message.includes("circular structure") | ||
) { | ||
throw new TypeError( | ||
"The object that was given to the writeStorage function has circular references.\n" + | ||
"For more information, check here: " + | ||
"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value" | ||
); | ||
} | ||
throw err; | ||
} | ||
} | ||
/** | ||
@@ -78,10 +94,10 @@ * Use this function to delete a value from localStorage. | ||
* const user = { name: 'John', email: 'John@fakemail.com' }; | ||
* | ||
* | ||
* // Add a user to your localStorage | ||
* writeStorage('user', JSON.stringify(user)); | ||
* | ||
* | ||
* // This will also trigger an update to the state of your component | ||
* deleteFromStorage('user'); | ||
* ``` | ||
* | ||
* | ||
* @export | ||
@@ -91,4 +107,4 @@ * @param {string} key The key of the item you wish to delete from localStorage. | ||
export function deleteFromStorage(key: string) { | ||
localStorage.removeItem(key); | ||
window.dispatchEvent(new LocalStorageChanged({ key, value: '' })) | ||
localStorage.removeItem(key); | ||
window.dispatchEvent(new LocalStorageChanged({ key, value: "" })); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
37179
635
17