@browser-storage/core
Advanced tools
Comparing version 0.0.1-alpha.2 to 0.0.1-alpha.3
@@ -1,1 +0,1 @@ | ||
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var r=e();for(var n in r)("object"==typeof exports?exports:t)[n]=r[n]}}(window,function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)r.d(n,o,function(e){return t[e]}.bind(null,o));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e,r){"use strict";r.r(e);var n=function(){return(n=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)},o=function(t,e,r,n){var o,i=arguments.length,u=i<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)u=Reflect.decorate(t,e,r,n);else for(var c=t.length-1;c>=0;c--)(o=t[c])&&(u=(i<3?o(u):i>3?o(e,r,u):o(e,r))||u);return i>3&&u&&Object.defineProperty(e,r,u),u},i=function(t,e,r,n){return new(r||(r=Promise))(function(o,i){function u(t){try{f(n.next(t))}catch(t){i(t)}}function c(t){try{f(n.throw(t))}catch(t){i(t)}}function f(t){t.done?o(t.value):new r(function(e){e(t.value)}).then(u,c)}f((n=n.apply(t,e||[])).next())})},u=function(t,e){var r,n,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(i){return function(c){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;u;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,n=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=(o=u.trys).length>0&&o[o.length-1])&&(6===i[0]||2===i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=e.call(t,u)}catch(t){i=[6,t],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}};function c(t,e,r){var n=r.value;r.value=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return i(this,void 0,void 0,function(){return u(this,function(e){switch(e.label){case 0:return[4,this.ready()];case 1:return e.sent(),[2,n.apply(this,t)]}})})}}var f=function(){function t(t){this._driver=(Array.isArray(t.drivers)?t.drivers:[t.drivers]).find(function(t){return t.isSupported}),this._driver&&this._driver.init(t),this.options=n({},t,{drivers:void 0})}return Object.defineProperty(t.prototype,"isSupported",{get:function(){return!!this._driver&&this._driver.isSupported},enumerable:!0,configurable:!0}),t.prototype.ready=function(){return i(this,void 0,void 0,function(){return u(this,function(t){return[2,!!this._driver&&this._driver.isSupported&&this._driver.ready()]})})},t.prototype.clear=function(){return i(this,void 0,void 0,function(){return u(this,function(t){return[2,this._driver.clear()]})})},t.prototype.getItem=function(t){return i(this,void 0,void 0,function(){return u(this,function(e){return[2,this._driver.getItem(t)]})})},t.prototype.iterate=function(t){return i(this,void 0,void 0,function(){return u(this,function(e){return[2,this._driver.iterate(t)]})})},t.prototype.key=function(t){return i(this,void 0,void 0,function(){return u(this,function(e){return[2,this._driver.key(t)]})})},t.prototype.keys=function(){return i(this,void 0,void 0,function(){return u(this,function(t){return[2,this._driver.keys()]})})},t.prototype.length=function(){return i(this,void 0,void 0,function(){return u(this,function(t){return[2,this._driver.length()]})})},t.prototype.removeItem=function(t){return i(this,void 0,void 0,function(){return u(this,function(e){return[2,this._driver.removeItem(t)]})})},t.prototype.setItem=function(t,e){return i(this,void 0,void 0,function(){return u(this,function(r){return[2,this._driver.setItem(t,e)]})})},t.prototype.init=function(t){},t.prototype.hasItem=function(t){return i(this,void 0,void 0,function(){return u(this,function(e){return[2,this._driver.hasItem(t)]})})},t.prototype.getDriver=function(){return i(this,void 0,void 0,function(){return u(this,function(t){return[2,this._driver]})})},o([c],t.prototype,"clear",null),o([c],t.prototype,"getItem",null),o([c],t.prototype,"iterate",null),o([c],t.prototype,"key",null),o([c],t.prototype,"keys",null),o([c],t.prototype,"length",null),o([c],t.prototype,"removeItem",null),o([c],t.prototype,"setItem",null),o([c],t.prototype,"hasItem",null),o([c],t.prototype,"getDriver",null),t}();r.d(e,"BrowserStorage",function(){return f})}])}); | ||
!function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var r=e();for(var n in r)("object"==typeof exports?exports:t)[n]=r[n]}}(window,function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)r.d(n,o,function(e){return t[e]}.bind(null,o));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e,r){"use strict";r.r(e);var n=function(){function t(t){var e=t.name,r=t.storeName,n=t.version,o=t.key,i=t.oldValue,u=t.newValue;this.name=e,this.storeName=r,this.version=n,this.key=o,this.oldValue=i,this.newValue=u}return t.deserialize=function(e){return new t(JSON.parse(e))},t.serialize=function(t){return JSON.stringify(t)},t}(),o=function(){return(o=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)},i=function(t,e,r,n){var o,i=arguments.length,u=i<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)u=Reflect.decorate(t,e,r,n);else for(var s=t.length-1;s>=0;s--)(o=t[s])&&(u=(i<3?o(u):i>3?o(e,r,u):o(e,r))||u);return i>3&&u&&Object.defineProperty(e,r,u),u},u=function(t,e,r,n){return new(r||(r=Promise))(function(o,i){function u(t){try{a(n.next(t))}catch(t){i(t)}}function s(t){try{a(n.throw(t))}catch(t){i(t)}}function a(t){t.done?o(t.value):new r(function(e){e(t.value)}).then(u,s)}a((n=n.apply(t,e||[])).next())})},s=function(t,e){var r,n,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function s(i){return function(s){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;u;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,n=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!(o=(o=u.trys).length>0&&o[o.length-1])&&(6===i[0]||2===i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=e.call(t,u)}catch(t){i=[6,t],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,s])}}};function a(t,e,r){var n=r.value;r.value=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return u(this,void 0,void 0,function(){return s(this,function(e){switch(e.label){case 0:return[4,this.ready()];case 1:return e.sent(),[2,n.apply(this,t)]}})})}}var c="__browser_storage_event_",f=function(){function t(t){var e=this;this._handlerStore=new Set,this._storageChange=function(t){if(t.key===c){var r=localStorage.getItem(c),o=n.deserialize(r);o.name===e.options.name&&o.storeName===e.options.storeName&&e._applyHandlers(o)}},this._driver=(Array.isArray(t.drivers)?t.drivers:[t.drivers]).find(function(t){return t.isSupported}),this.options=o({},t,{drivers:void 0}),this.init(t)}return Object.defineProperty(t.prototype,"isSupported",{get:function(){return!!this._driver&&this._driver.isSupported},enumerable:!0,configurable:!0}),t.prototype.ready=function(){return u(this,void 0,void 0,function(){return s(this,function(t){return[2,!!this._driver&&this._driver.isSupported&&this._driver.ready()]})})},t.prototype.clear=function(){return u(this,void 0,void 0,function(){return s(this,function(t){return[2,this._driver.clear()]})})},t.prototype.getItem=function(t){return u(this,void 0,void 0,function(){return s(this,function(e){return[2,this._driver.getItem(t)]})})},t.prototype.iterate=function(t){return u(this,void 0,void 0,function(){return s(this,function(e){return[2,this._driver.iterate(t)]})})},t.prototype.key=function(t){return u(this,void 0,void 0,function(){return s(this,function(e){return[2,this._driver.key(t)]})})},t.prototype.keys=function(){return u(this,void 0,void 0,function(){return s(this,function(t){return[2,this._driver.keys()]})})},t.prototype.length=function(){return u(this,void 0,void 0,function(){return s(this,function(t){return[2,this._driver.length()]})})},t.prototype.removeItem=function(t){return u(this,void 0,void 0,function(){return s(this,function(e){return[2,this._driver.removeItem(t)]})})},t.prototype.setItem=function(t,e){return u(this,void 0,void 0,function(){var r,o,i;return s(this,function(u){switch(u.label){case 0:return[4,this.hasItem(t)];case 1:return u.sent()?[4,this.getItem(t)]:[3,3];case 2:return o=u.sent(),[3,4];case 3:o=void 0,u.label=4;case 4:return r=o,[4,this._driver.setItem(t,e)];case 5:return u.sent(),i=new n({name:this.options.name,storeName:this.options.storeName,version:this.options.version,key:t,oldValue:r,newValue:e}),this._applyHandlers(i),this.options.crossTabNotification&&this._triggerCrossTabEvent(i),[2,e]}})})},t.prototype.init=function(t){return this._initCrossTabNotification(),!!this._driver&&this._driver.init(t)},t.prototype.hasItem=function(t){return u(this,void 0,void 0,function(){return s(this,function(e){return[2,this._driver.hasItem(t)]})})},t.prototype.getDriver=function(){return u(this,void 0,void 0,function(){return s(this,function(t){return[2,this._driver]})})},t.prototype.destroy=function(){return u(this,void 0,void 0,function(){return s(this,function(t){return window.removeEventListener("storage",this._storageChange),this._handlerStore.clear(),[2,this._driver.destroy()]})})},t.prototype.addEventListener=function(t){this._handlerStore.add(t)},t.prototype.removeEventListener=function(t){this._handlerStore.delete(t)},t.prototype._triggerCrossTabEvent=function(t){localStorage.setItem(c,n.serialize(t))},t.prototype._applyHandlers=function(t){this._handlerStore.forEach(function(e){return e(t)})},t.prototype._initCrossTabNotification=function(){window.addEventListener("storage",this._storageChange)},i([a],t.prototype,"clear",null),i([a],t.prototype,"getItem",null),i([a],t.prototype,"iterate",null),i([a],t.prototype,"key",null),i([a],t.prototype,"keys",null),i([a],t.prototype,"length",null),i([a],t.prototype,"removeItem",null),i([a],t.prototype,"setItem",null),i([a],t.prototype,"hasItem",null),i([a],t.prototype,"getDriver",null),t}();r.d(e,"EVENT_KEY",function(){return c}),r.d(e,"BrowserStorage",function(){return f}),r.d(e,"BrowserStorageEvent",function(){return n})}])}); |
{ | ||
"name": "@browser-storage/core", | ||
"version": "0.0.1-alpha.2", | ||
"version": "0.0.1-alpha.3", | ||
"description": "Browser storage core", | ||
@@ -37,3 +37,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "ac652f4d06a539e54778e6245e39780c20a80ca0" | ||
"gitHead": "eaf97e6306f224c1236f5fbeedabb6b4260fc7f7" | ||
} |
@@ -8,2 +8,3 @@ import { Driver } from './driver'; | ||
drivers: Driver | Driver[]; | ||
crossTabNotification?: boolean; | ||
} |
@@ -1,6 +0,7 @@ | ||
import { BrowserStorage } from './browser-storage'; | ||
import { BrowserStorage, EVENT_KEY } from './browser-storage'; | ||
import { BrowserStorageEvent } from './browser-storage-event'; | ||
import { BrowserStorageOptions } from './browser-storage-options'; | ||
import { Driver } from './driver'; | ||
function makeOptions(drivers: BrowserStorageOptions['drivers']): BrowserStorageOptions { | ||
function makeOptions(drivers?: BrowserStorageOptions['drivers'], crossTabNotification?: BrowserStorageOptions['crossTabNotification']): BrowserStorageOptions { | ||
return { | ||
@@ -10,3 +11,4 @@ name: 'testName', | ||
version: 1, | ||
drivers | ||
drivers, | ||
crossTabNotification | ||
}; | ||
@@ -22,11 +24,11 @@ } | ||
public getItem<T>(key: string): Promise<T> { | ||
return undefined; | ||
public async getItem<T>(key: string): Promise<T> { | ||
return JSON.parse(localStorage.getItem(key)); | ||
} | ||
public hasItem(key: string): Promise<boolean> { | ||
return undefined; | ||
public async hasItem(key: string): Promise<boolean> { | ||
return localStorage.hasOwnProperty(key); | ||
} | ||
public init(dbOptions: BrowserStorageOptions): Promise<this> { | ||
public init(dbOptions: BrowserStorageOptions): Promise<void> { | ||
return undefined; | ||
@@ -59,3 +61,8 @@ } | ||
public setItem<T>(key: string, item: T): Promise<T> { | ||
public async setItem<T>(key: string, item: T): Promise<T> { | ||
localStorage.setItem(key, JSON.stringify(item)); | ||
return item; | ||
} | ||
public destroy(): Promise<void> { | ||
return undefined; | ||
@@ -89,2 +96,58 @@ } | ||
}); | ||
test('#events', async () => { | ||
const supportedDriver = new SupportedDriver(); | ||
const bs = new BrowserStorage(makeOptions(supportedDriver, true)); | ||
const KEY = 'boolean'; | ||
function handle1(event: BrowserStorageEvent) { | ||
const options = makeOptions(); | ||
expect(event.name).toEqual(options.name); | ||
expect(event.storeName).toEqual(options.storeName); | ||
expect(event.version).toEqual(options.version); | ||
expect(event.key).toEqual(KEY); | ||
expect(event.oldValue).toBeUndefined(); | ||
expect(event.newValue).toBeTruthy(); | ||
} | ||
bs.addEventListener(handle1); | ||
await bs.setItem(KEY, true); | ||
bs.removeEventListener(handle1); | ||
function handle2(event: BrowserStorageEvent) { | ||
const options = makeOptions(); | ||
expect(event.name).toEqual(options.name); | ||
expect(event.storeName).toEqual(options.storeName); | ||
expect(event.version).toEqual(options.version); | ||
expect(event.key).toEqual(KEY); | ||
expect(event.oldValue).toBeTruthy(); | ||
expect(event.newValue).toBeFalsy(); | ||
} | ||
bs.addEventListener(handle2); | ||
await bs.setItem(KEY, false); | ||
const fakeEvent = { | ||
key: EVENT_KEY, | ||
newValue: '', | ||
oldValue: '', | ||
storageArea: null, | ||
url: '' | ||
}; | ||
await (bs as any)._storageChange(fakeEvent as StorageEvent); | ||
const otherEvent = { | ||
key: 'otherEvent', | ||
newValue: '', | ||
oldValue: '', | ||
storageArea: null, | ||
url: '' | ||
}; | ||
await (bs as any)._storageChange(otherEvent as StorageEvent); | ||
await bs.destroy(); | ||
}); | ||
}); |
@@ -0,1 +1,2 @@ | ||
import { BrowserStorageEvent } from './browser-storage-event'; | ||
import { BrowserStorageOptions } from './browser-storage-options'; | ||
@@ -14,5 +15,10 @@ import { Driver } from './driver'; | ||
export const EVENT_KEY = '__browser_storage_event_'; | ||
export type HandlerFn = (event: BrowserStorageEvent) => any; | ||
export class BrowserStorage implements Driver { | ||
private readonly _driver: Driver; | ||
private readonly options: BrowserStorageOptions; | ||
private readonly _handlerStore = new Set<HandlerFn>(); | ||
@@ -23,5 +29,2 @@ constructor(options: BrowserStorageOptions) { | ||
.find(driver => driver.isSupported); | ||
if (this._driver) { | ||
this._driver.init(options); | ||
} | ||
@@ -32,2 +35,4 @@ this.options = { | ||
}; | ||
this.init(options); | ||
} | ||
@@ -80,7 +85,28 @@ | ||
public async setItem<T>(key: string, item: T): Promise<T> { | ||
return this._driver.setItem<T>(key, item); | ||
const oldValue = await this.hasItem(key) | ||
? await this.getItem(key) | ||
: undefined; | ||
await this._driver.setItem<T>(key, item); | ||
const event = new BrowserStorageEvent({ | ||
name: this.options.name, | ||
storeName: this.options.storeName, | ||
version: this.options.version, | ||
key, | ||
oldValue, | ||
newValue: item | ||
}); | ||
this._applyHandlers(event); | ||
if (this.options.crossTabNotification) { | ||
this._triggerCrossTabEvent(event); | ||
} | ||
return item; | ||
} | ||
public init(dbOptions?: BrowserStorageOptions): Promise<this> { | ||
return undefined; | ||
public init(dbOptions?: BrowserStorageOptions): Promise<void> { | ||
this._initCrossTabNotification(); | ||
return !!this._driver && this._driver.init(dbOptions); | ||
} | ||
@@ -97,2 +123,43 @@ | ||
} | ||
public async destroy(): Promise<void> { | ||
window.removeEventListener('storage', this._storageChange); | ||
this._handlerStore.clear(); | ||
return this._driver.destroy(); | ||
} | ||
public addEventListener(fn: HandlerFn) { | ||
this._handlerStore.add(fn); | ||
} | ||
public removeEventListener(fn: HandlerFn) { | ||
this._handlerStore.delete(fn); | ||
} | ||
private _triggerCrossTabEvent(event: BrowserStorageEvent) { | ||
localStorage.setItem(EVENT_KEY, BrowserStorageEvent.serialize(event)); | ||
} | ||
private readonly _storageChange = (evt: StorageEvent) => { | ||
if (evt.key !== EVENT_KEY) { | ||
return; | ||
} | ||
const serializeEvent = localStorage.getItem(EVENT_KEY); | ||
const event = BrowserStorageEvent.deserialize(serializeEvent); | ||
if (event.name !== this.options.name || event.storeName !== this.options.storeName) { | ||
return; | ||
} | ||
this._applyHandlers(event); | ||
}; | ||
private _applyHandlers(event: BrowserStorageEvent) { | ||
this._handlerStore.forEach(fn => fn(event)); | ||
} | ||
private _initCrossTabNotification() { | ||
window.addEventListener('storage', this._storageChange); | ||
} | ||
} |
@@ -8,3 +8,3 @@ import { BrowserStorageOptions } from './browser-storage-options'; | ||
init(dbOptions: BrowserStorageOptions): Promise<this>; | ||
init(dbOptions: BrowserStorageOptions): Promise<void>; | ||
@@ -28,2 +28,4 @@ getItem<T>(key: string): Promise<T>; | ||
clear(): Promise<void>; | ||
destroy(): Promise<void>; | ||
} |
@@ -5,1 +5,2 @@ export * from './browser-storage'; | ||
export * from './serializer'; | ||
export * from './browser-storage-event'; |
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
18592
11
306
0