reactive-localstorage
Advanced tools
Comparing version 0.0.2 to 0.1.0-alpha.1
export declare class LocalStorage implements Storage { | ||
readonly supported: boolean; | ||
protected storage: Storage; | ||
readonly native: Storage; | ||
protected _events: { | ||
[P in keyof ReactiveLocalStorageEventMap]?: Set<ReactiveLocalStorageEventMap[P]>; | ||
}; | ||
protected _cache: Map<string, string | null>; | ||
protected _length: number; | ||
protected _injected: boolean; | ||
constructor(window?: Window, storage?: Storage); | ||
@@ -16,8 +19,10 @@ readonly length: number; | ||
off<K extends keyof ReactiveLocalStorageEventMap>(name: K, fn: ReactiveLocalStorageEventMap[K]): void; | ||
feed(key: string, newValue: string | null): void; | ||
feed(key: string, newValue: string | null, oldValue: string | null): void; | ||
protected set(key: string, value: string | null): void; | ||
protected emit<K extends keyof ReactiveLocalStorageEventMap>(name: K, ...args: ReactiveLocalStorageEventMap[K] extends (...args: infer T) => any ? T : any[]): void; | ||
protected inject(target: Window, storage: Storage): void; | ||
} | ||
export interface ReactiveLocalStorageEventMap { | ||
change(key: string, newValue: string | null): any; | ||
change(key: string, newValue: string | null, oldValue: string | null): any; | ||
} | ||
export declare function listen(target: Window, cb: (this: Window, ev: WindowEventMap['storage']) => any): boolean; |
export class LocalStorage { | ||
constructor(window, storage) { | ||
this._events = {}; | ||
this._length = 0; | ||
this._injected = false; | ||
if (window) { | ||
@@ -11,28 +13,37 @@ this.supported = true; | ||
} | ||
this.storage = storage || window.localStorage; | ||
listen(window, ({ key, newValue }) => { | ||
this.native = storage || window.localStorage; | ||
this._cache = new Map(); | ||
this.inject(window, this.native); | ||
listen(window, ({ key, newValue, oldValue }) => { | ||
if (!key) | ||
return; | ||
this.feed(key, newValue); | ||
this.feed(key, newValue, oldValue); | ||
}); | ||
} | ||
get length() { | ||
return this.storage.length; | ||
return this.native.length; | ||
} | ||
key(index) { | ||
return this.storage.key(index); | ||
return this.native.key(index); | ||
} | ||
getItem(key) { | ||
return this.storage.getItem(key); | ||
const cache = this._cache; | ||
if (cache.has(key)) | ||
return cache.get(key); | ||
const value = this.native.getItem(key); | ||
cache.set(key, value); | ||
return value; | ||
} | ||
setItem(key, value) { | ||
this.storage.setItem(key, value); | ||
this.emit('change', key, value); | ||
return this.set(key, value); | ||
} | ||
removeItem(key) { | ||
this.storage.removeItem(key); | ||
this.emit('change', key, null); | ||
return this.set(key, null); | ||
} | ||
clear() { | ||
this.storage.clear(); | ||
this.native.clear(); | ||
const cache = this._cache; | ||
for (const key of cache.keys()) { | ||
cache.set(key, null); | ||
} | ||
} | ||
@@ -52,5 +63,18 @@ on(name, fn) { | ||
} | ||
feed(key, newValue) { | ||
this.emit('change', key, newValue); | ||
feed(key, newValue, oldValue) { | ||
this._cache.set(key, newValue); | ||
this.emit('change', key, newValue, oldValue); | ||
} | ||
set(key, value) { | ||
const cache = this._cache; | ||
const oldValue = cache.has(key) ? cache.get(key) : this.native.getItem(key); | ||
if (typeof value === 'string') { | ||
this.native.setItem(key, value); | ||
} | ||
else { | ||
this.native.removeItem(key); | ||
} | ||
cache.set(key, value); | ||
this.emit('change', key, value, oldValue); | ||
} | ||
emit(name, ...args) { | ||
@@ -64,2 +88,70 @@ const set = this._events[name]; | ||
} | ||
inject(target, storage) { | ||
const me = this; | ||
const cache = this._cache; | ||
const injected = Object.create(storage); | ||
Object.defineProperties(injected, { | ||
getItem: { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value(key) { | ||
const value = storage.getItem(key); | ||
cache.set(key, value); | ||
}, | ||
}, | ||
setItem: { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value(key, value) { | ||
set(key, String(value)); | ||
}, | ||
}, | ||
removeItem: { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value(key) { | ||
set(key, null); | ||
}, | ||
} | ||
}); | ||
Object.defineProperty(target, 'localStorage', { | ||
...Object.getOwnPropertyDescriptor(window, 'localStorage'), | ||
get: () => injected, | ||
}); | ||
this._injected = true; | ||
function set(key, newValue) { | ||
let oldValue; | ||
let error; | ||
let hasError = false; | ||
try { | ||
oldValue = me.getItem(key); | ||
} | ||
catch (e) { | ||
hasError = true; | ||
error = e; | ||
} | ||
if (newValue === null) { | ||
storage.removeItem(key); | ||
} | ||
else { | ||
storage.setItem(key, newValue); | ||
} | ||
if (hasError) { | ||
// tslint:disable-next-line no-console | ||
console.error(error); | ||
return; | ||
} | ||
try { | ||
cache.set(key, newValue); | ||
me.feed(key, newValue, oldValue); | ||
} | ||
catch (e) { | ||
// tslint:disable-next-line no-console | ||
console.error(e); | ||
} | ||
} | ||
} | ||
} | ||
@@ -78,2 +170,2 @@ export function listen(target, cb) { | ||
} | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibG9jYWxzdG9yYWdlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL2xvY2Fsc3RvcmFnZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxNQUFNLE9BQU8sWUFBWTtJQVF2QixZQUFZLE1BQWUsRUFBRSxPQUFpQjtRQUpwQyxZQUFPLEdBRWIsRUFBRSxDQUFBO1FBR0osSUFBSSxNQUFNLEVBQUU7WUFDVixJQUFJLENBQUMsU0FBUyxHQUFHLElBQUksQ0FBQTtTQUN0QjthQUFNO1lBQ0wsSUFBSSxDQUFDLFNBQVMsR0FBRyxLQUFLLENBQUE7WUFDdEIsTUFBTSxHQUFHLElBQUksTUFBTSxFQUFFLENBQUE7U0FDdEI7UUFDRCxJQUFJLENBQUMsT0FBTyxHQUFHLE9BQU8sSUFBSSxNQUFNLENBQUMsWUFBWSxDQUFBO1FBQzdDLE1BQU0sQ0FBQyxNQUFNLEVBQUUsQ0FBQyxFQUFFLEdBQUcsRUFBRSxRQUFRLEVBQUUsRUFBRSxFQUFFO1lBQ25DLElBQUksQ0FBQyxHQUFHO2dCQUFFLE9BQU07WUFDaEIsSUFBSSxDQUFDLElBQUksQ0FBQyxHQUFHLEVBQUUsUUFB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7YUFDbEM7WUFBQyxPQUFPLENBQUMsRUFBRTtnQkFDVixzQ0FBc0M7Z0JBQ3RDLE9BQU8sQ0FBQyxLQUFLLENBQUMsQ0FBQyxDQUFDLENBQUE7YUFDakI7UUFDSCxDQUFDO0lBQ0gsQ0FBQztDQUNGO0FBTUQsTUFBTSxVQUFVLE1BQU0sQ0FBQyxNQUFjLEVBQUUsRUFBd0Q7SUFDN0YsSUFBSSxNQUFNLENBQUMsZ0JBQWdCLEVBQUU7UUFDM0IsTUFBTSxDQUFDLGdCQUFnQixDQUFDLFNBQVMsRUFBRSxFQUFFLEVBQUUsSUFBSSxDQUFDLENBQUE7S0FDN0M7U0FBTSxJQUFJLE1BQU0sQ0FBQyxXQUFXLEVBQUU7UUFDN0IsTUFBTSxDQUFDLFdBQVcsQ0FBQyxTQUFTLEVBQUUsRUFBRSxDQUFDLENBQUE7S0FDbEM7U0FBTTtRQUNMLE9BQU8sS0FBSyxDQUFBO0tBQ2I7SUFDRCxPQUFPLElBQUksQ0FBQTtBQUNiLENBQUMifQ== |
export declare class LocalStorage implements Storage { | ||
readonly supported: boolean; | ||
protected storage: Storage; | ||
readonly native: Storage; | ||
protected _events: { | ||
[P in keyof ReactiveLocalStorageEventMap]?: Set<ReactiveLocalStorageEventMap[P]>; | ||
}; | ||
protected _cache: Map<string, string | null>; | ||
protected _length: number; | ||
protected _injected: boolean; | ||
constructor(window?: Window, storage?: Storage); | ||
@@ -16,8 +19,10 @@ readonly length: number; | ||
off<K extends keyof ReactiveLocalStorageEventMap>(name: K, fn: ReactiveLocalStorageEventMap[K]): void; | ||
feed(key: string, newValue: string | null): void; | ||
feed(key: string, newValue: string | null, oldValue: string | null): void; | ||
protected set(key: string, value: string | null): void; | ||
protected emit<K extends keyof ReactiveLocalStorageEventMap>(name: K, ...args: ReactiveLocalStorageEventMap[K] extends (...args: infer T) => any ? T : any[]): void; | ||
protected inject(target: Window, storage: Storage): void; | ||
} | ||
export interface ReactiveLocalStorageEventMap { | ||
change(key: string, newValue: string | null): any; | ||
change(key: string, newValue: string | null, oldValue: string | null): any; | ||
} | ||
export declare function listen(target: Window, cb: (this: Window, ev: WindowEventMap['storage']) => any): boolean; |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __values = (this && this.__values) || function (o) { | ||
@@ -17,2 +28,4 @@ var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; | ||
this._events = {}; | ||
this._length = 0; | ||
this._injected = false; | ||
if (window) { | ||
@@ -25,8 +38,10 @@ this.supported = true; | ||
} | ||
this.storage = storage || window.localStorage; | ||
this.native = storage || window.localStorage; | ||
this._cache = new Map(); | ||
this.inject(window, this.native); | ||
listen(window, function (_a) { | ||
var key = _a.key, newValue = _a.newValue; | ||
var key = _a.key, newValue = _a.newValue, oldValue = _a.oldValue; | ||
if (!key) | ||
return; | ||
_this.feed(key, newValue); | ||
_this.feed(key, newValue, oldValue); | ||
}); | ||
@@ -36,3 +51,3 @@ } | ||
get: function () { | ||
return this.storage.length; | ||
return this.native.length; | ||
}, | ||
@@ -43,17 +58,35 @@ enumerable: true, | ||
LocalStorage.prototype.key = function (index) { | ||
return this.storage.key(index); | ||
return this.native.key(index); | ||
}; | ||
LocalStorage.prototype.getItem = function (key) { | ||
return this.storage.getItem(key); | ||
var cache = this._cache; | ||
if (cache.has(key)) | ||
return cache.get(key); | ||
var value = this.native.getItem(key); | ||
cache.set(key, value); | ||
return value; | ||
}; | ||
LocalStorage.prototype.setItem = function (key, value) { | ||
this.storage.setItem(key, value); | ||
this.emit('change', key, value); | ||
return this.set(key, value); | ||
}; | ||
LocalStorage.prototype.removeItem = function (key) { | ||
this.storage.removeItem(key); | ||
this.emit('change', key, null); | ||
return this.set(key, null); | ||
}; | ||
LocalStorage.prototype.clear = function () { | ||
this.storage.clear(); | ||
var e_1, _a; | ||
this.native.clear(); | ||
var cache = this._cache; | ||
try { | ||
for (var _b = __values(cache.keys()), _c = _b.next(); !_c.done; _c = _b.next()) { | ||
var key = _c.value; | ||
cache.set(key, null); | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
try { | ||
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
}; | ||
@@ -73,5 +106,18 @@ LocalStorage.prototype.on = function (name, fn) { | ||
}; | ||
LocalStorage.prototype.feed = function (key, newValue) { | ||
this.emit('change', key, newValue); | ||
LocalStorage.prototype.feed = function (key, newValue, oldValue) { | ||
this._cache.set(key, newValue); | ||
this.emit('change', key, newValue, oldValue); | ||
}; | ||
LocalStorage.prototype.set = function (key, value) { | ||
var cache = this._cache; | ||
var oldValue = cache.has(key) ? cache.get(key) : this.native.getItem(key); | ||
if (typeof value === 'string') { | ||
this.native.setItem(key, value); | ||
} | ||
else { | ||
this.native.removeItem(key); | ||
} | ||
cache.set(key, value); | ||
this.emit('change', key, value, oldValue); | ||
}; | ||
LocalStorage.prototype.emit = function (name) { | ||
@@ -82,3 +128,3 @@ var args = []; | ||
} | ||
var e_1, _a; | ||
var e_2, _a; | ||
var set = this._events[name]; | ||
@@ -92,3 +138,3 @@ if (set) { | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
@@ -98,6 +144,71 @@ try { | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
} | ||
}; | ||
LocalStorage.prototype.inject = function (target, storage) { | ||
var me = this; | ||
var cache = this._cache; | ||
var injected = Object.create(storage); | ||
Object.defineProperties(injected, { | ||
getItem: { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value: function (key) { | ||
var value = storage.getItem(key); | ||
cache.set(key, value); | ||
}, | ||
}, | ||
setItem: { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value: function (key, value) { | ||
set(key, String(value)); | ||
}, | ||
}, | ||
removeItem: { | ||
configurable: true, | ||
enumerable: false, | ||
writable: true, | ||
value: function (key) { | ||
set(key, null); | ||
}, | ||
} | ||
}); | ||
Object.defineProperty(target, 'localStorage', __assign({}, Object.getOwnPropertyDescriptor(window, 'localStorage'), { get: function () { return injected; } })); | ||
this._injected = true; | ||
function set(key, newValue) { | ||
var oldValue; | ||
var error; | ||
var hasError = false; | ||
try { | ||
oldValue = me.getItem(key); | ||
} | ||
catch (e) { | ||
hasError = true; | ||
error = e; | ||
} | ||
if (newValue === null) { | ||
storage.removeItem(key); | ||
} | ||
else { | ||
storage.setItem(key, newValue); | ||
} | ||
if (hasError) { | ||
// tslint:disable-next-line no-console | ||
console.error(error); | ||
return; | ||
} | ||
try { | ||
cache.set(key, newValue); | ||
me.feed(key, newValue, oldValue); | ||
} | ||
catch (e) { | ||
// tslint:disable-next-line no-console | ||
console.error(e); | ||
} | ||
} | ||
}; | ||
return LocalStorage; | ||
@@ -119,2 +230,2 @@ }()); | ||
exports.listen = listen; | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibG9jYWxzdG9yYWdlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vc3JjL2xvY2Fsc3RvcmFnZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7QUFBQTtJQVFFLHNCQUFZLE1BQWUsRUFBRSxPQUFpQjtRQUE5QyxpQkFZQztRQWhCUyxZQUFPLEdBRWIsRUFBRSxDQUFBO1FBR0osSUFBSSxNQUFNLEVBQUU7WUFDVixJQUFJLENBQUMsU0FBUyxHQUFHLElBQUksQ0FBQTtTQUN0QjthQUFNO1lBQ0w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Q0FBQTthQUNqQjtRQUNILENBQUM7SUFDSCxDQUFDO0lBQ0gsbUJBQUM7QUFBRCxDQUFDLEFBM0tELElBMktDO0FBM0tZLG9DQUFZO0FBaUx6QixTQUFnQixNQUFNLENBQUMsTUFBYyxFQUFFLEVBQXdEO0lBQzdGLElBQUksTUFBTSxDQUFDLGdCQUFnQixFQUFFO1FBQzNCLE1BQU0sQ0FBQyxnQkFBZ0IsQ0FBQyxTQUFTLEVBQUUsRUFBRSxFQUFFLElBQUksQ0FBQyxDQUFBO0tBQzdDO1NBQU0sSUFBSSxNQUFNLENBQUMsV0FBVyxFQUFFO1FBQzdCLE1BQU0sQ0FBQyxXQUFXLENBQUMsU0FBUyxFQUFFLEVBQUUsQ0FBQyxDQUFBO0tBQ2xDO1NBQU07UUFDTCxPQUFPLEtBQUssQ0FBQTtLQUNiO0lBQ0QsT0FBTyxJQUFJLENBQUE7QUFDYixDQUFDO0FBVEQsd0JBU0MifQ== |
{ | ||
"name": "reactive-localstorage", | ||
"version": "0.0.2", | ||
"version": "0.1.0-alpha.1", | ||
"description": "A reactive localStorage with no dependency", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -13,2 +13,3 @@ # reactive-localstorage | ||
#### Features | ||
* inject window.localStorage | ||
* Emit changes from same window | ||
@@ -32,6 +33,6 @@ * Emit external changes | ||
You can also trigger changes manually, especially when you have other sources that manage localStorage. | ||
It also works with window.localStorage | ||
```js | ||
localStorage.feed('foo', 'alice'); | ||
window.localStorage.setItem('foo', 'alice'); | ||
@@ -41,11 +42,8 @@ // print key foo changed to alice | ||
You can also use injection to override `window.localStorage` | ||
You can also trigger changes manually, especially when you have other sources that manage localStorage. | ||
```js | ||
import 'reactive-localstorage/lib/inject'; | ||
import localStorage from 'reactive-localstorage'; | ||
localStorage.feed('foo', 'bob'); | ||
console.log(window.localStorage === localStorage); | ||
// print true | ||
// print key foo changed to bob | ||
``` | ||
@@ -52,0 +50,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33683
467
11
51
1