@browser-storage/core
Advanced tools
Comparing version 0.0.1-alpha.3 to 0.0.1-alpha.4
@@ -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(){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})}])}); | ||
!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,o,i=(n=function(t,e){return(n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)e.hasOwnProperty(r)&&(t[r]=e[r])})(t,e)},function(t,e){function r(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(r.prototype=e.prototype,new r)});function s(t){switch(t.type){case o.Clear:return new l(t);case o.SetItem:return new a(t);case o.RemoveItem:return new c(t);default:return new u(t)}}!function(t){t[t.Default=0]="Default",t[t.SetItem=1]="SetItem",t[t.RemoveItem=2]="RemoveItem",t[t.Clear=3]="Clear"}(o||(o={}));var u=function(){function t(t){var e=t.name,r=t.storeName,n=t.version,i=t.key,s=t.oldValue,u=t.newValue,a=t.isCrossTab,c=void 0!==a&&a;this.type=o.Default,this.name=e,this.storeName=r,this.version=n,this.key=i,this.oldValue=s,this.newValue=u,this.isCrossTab=c}return t.deserialize=function(t){return s(JSON.parse(t))},t.serialize=function(t){return JSON.stringify(t)},t.prototype.copyWith=function(t){var e=t.name,r=void 0===e?this.name:e,n=t.storeName,o=void 0===n?this.storeName:n,i=t.version,u=void 0===i?this.version:i,a=t.key,c=void 0===a?this.key:a,l=t.oldValue,f=void 0===l?this.oldValue:l,p=t.newValue,h=void 0===p?this.newValue:p,d=t.isCrossTab,v=void 0===d?this.isCrossTab:d;return s({type:this.type,name:r,storeName:o,version:u,key:c,oldValue:f,newValue:h,isCrossTab:v})},t}(),a=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e.type=o.SetItem,e}return i(e,t),e}(u),c=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e.type=o.RemoveItem,e}return i(e,t),e}(u),l=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e.type=o.Clear,e}return i(e,t),e}(u),f=function(){return(f=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)},p=function(t,e,r,n){var o,i=arguments.length,s=i<3?e:null===n?n=Object.getOwnPropertyDescriptor(e,r):n;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(t,e,r,n);else for(var u=t.length-1;u>=0;u--)(o=t[u])&&(s=(i<3?o(s):i>3?o(e,r,s):o(e,r))||s);return i>3&&s&&Object.defineProperty(e,r,s),s},h=function(t,e,r,n){return new(r||(r=Promise))(function(o,i){function s(t){try{a(n.next(t))}catch(t){i(t)}}function u(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(s,u)}a((n=n.apply(t,e||[])).next())})},d=function(t,e){var r,n,o,i,s={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;s;)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 s.label++,{value:i[1],done:!1};case 5:s.label++,n=i[1],i=[0];continue;case 7:i=s.ops.pop(),s.trys.pop();continue;default:if(!(o=(o=s.trys).length>0&&o[o.length-1])&&(6===i[0]||2===i[0])){s=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){s.label=i[1];break}if(6===i[0]&&s.label<o[1]){s.label=o[1],o=i;break}if(o&&s.label<o[2]){s.label=o[2],s.ops.push(i);break}o[2]&&s.ops.pop(),s.trys.pop();continue}i=e.call(t,s)}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,u])}}};function v(t,e,r){var n=r.value;r.value=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return h(this,void 0,void 0,function(){return d(this,function(e){switch(e.label){case 0:return[4,this.ready()];case 1:return e.sent(),[2,n.apply(this,t)]}})})}}var y="__browser_storage_event_",m=function(){function t(t){var e=this;this._handlerStore=new Set,this._storageChange=function(t){if(t.key===y){var r=localStorage.getItem(y),n=u.deserialize(r);n.name===e.options.name&&n.storeName===e.options.storeName&&e._applyHandlers(n)}},this._driver=(Array.isArray(t.drivers)?t.drivers:[t.drivers]).find(function(t){return t.isSupported}),this.options=f({},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 h(this,void 0,void 0,function(){return d(this,function(t){return[2,!!this._driver&&this._driver.isSupported&&this._driver.ready()]})})},t.prototype.clear=function(){return h(this,void 0,void 0,function(){var t;return d(this,function(e){switch(e.label){case 0:return[4,this._driver.clear()];case 1:return e.sent(),t=new l({name:this.options.name,storeName:this.options.storeName,version:this.options.version,key:null,oldValue:null,newValue:null}),this._triggerEvent(t),[2]}})})},t.prototype.getItem=function(t){return h(this,void 0,void 0,function(){return d(this,function(e){return[2,this._driver.getItem(t)]})})},t.prototype.iterate=function(t){return h(this,void 0,void 0,function(){return d(this,function(e){return[2,this._driver.iterate(t)]})})},t.prototype.key=function(t){return h(this,void 0,void 0,function(){return d(this,function(e){return[2,this._driver.key(t)]})})},t.prototype.keys=function(){return h(this,void 0,void 0,function(){return d(this,function(t){return[2,this._driver.keys()]})})},t.prototype.length=function(){return h(this,void 0,void 0,function(){return d(this,function(t){return[2,this._driver.length()]})})},t.prototype.removeItem=function(t){return h(this,void 0,void 0,function(){var e,r,n;return d(this,function(o){switch(o.label){case 0:return[4,this.hasItem(t)];case 1:return o.sent()?[4,this.getItem(t)]:[3,3];case 2:return r=o.sent(),[3,4];case 3:r=void 0,o.label=4;case 4:return e=r,[4,this._driver.removeItem(t)];case 5:return o.sent(),n=new c({name:this.options.name,storeName:this.options.storeName,version:this.options.version,key:t,oldValue:e,newValue:null}),this._triggerEvent(n),[2]}})})},t.prototype.setItem=function(t,e){return h(this,void 0,void 0,function(){var r,n,o;return d(this,function(i){switch(i.label){case 0:return[4,this.hasItem(t)];case 1:return i.sent()?[4,this.getItem(t)]:[3,3];case 2:return n=i.sent(),[3,4];case 3:n=void 0,i.label=4;case 4:return r=n,[4,this._driver.setItem(t,e)];case 5:return i.sent(),o=new a({name:this.options.name,storeName:this.options.storeName,version:this.options.version,key:t,oldValue:r,newValue:e}),this._triggerEvent(o),[2,e]}})})},t.prototype.init=function(t){return this._initCrossTabNotification(),!!this._driver&&this._driver.init(t)},t.prototype.hasItem=function(t){return h(this,void 0,void 0,function(){return d(this,function(e){return[2,this._driver.hasItem(t)]})})},t.prototype.getDriver=function(){return h(this,void 0,void 0,function(){return d(this,function(t){return[2,this._driver]})})},t.prototype.destroy=function(){return h(this,void 0,void 0,function(){return d(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._triggerEvent=function(t){this._applyHandlers(t),this._triggerCrossTabEvent(t)},t.prototype._triggerCrossTabEvent=function(t){this.options.crossTabNotification&&localStorage.setItem(y,u.serialize(t.copyWith({isCrossTab:!0})))},t.prototype._applyHandlers=function(t){this._handlerStore.forEach(function(e){return e(t)})},t.prototype._initCrossTabNotification=function(){window.addEventListener("storage",this._storageChange)},p([v],t.prototype,"clear",null),p([v],t.prototype,"getItem",null),p([v],t.prototype,"iterate",null),p([v],t.prototype,"key",null),p([v],t.prototype,"keys",null),p([v],t.prototype,"length",null),p([v],t.prototype,"removeItem",null),p([v],t.prototype,"setItem",null),p([v],t.prototype,"hasItem",null),p([v],t.prototype,"getDriver",null),t}();r.d(e,"EVENT_KEY",function(){return y}),r.d(e,"BrowserStorage",function(){return m}),r.d(e,"BrowserStorageEventTypes",function(){return o}),r.d(e,"BrowserStorageEvent",function(){return u}),r.d(e,"SetItemBrowserStorageEvent",function(){return a}),r.d(e,"RemoveItemBrowserStorageEvent",function(){return c}),r.d(e,"ClearBrowserStorageEvent",function(){return l})}])}); |
{ | ||
"name": "@browser-storage/core", | ||
"version": "0.0.1-alpha.3", | ||
"version": "0.0.1-alpha.4", | ||
"description": "Browser storage core", | ||
@@ -37,3 +37,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "eaf97e6306f224c1236f5fbeedabb6b4260fc7f7" | ||
"gitHead": "b9205a43e366e466704f3f832b607e425ffcfef7" | ||
} |
@@ -8,4 +8,25 @@ export interface BrowserStorageEventOptions { | ||
newValue: any; | ||
isCrossTab?: boolean; | ||
} | ||
export enum BrowserStorageEventTypes { | ||
Default, | ||
SetItem, | ||
RemoveItem, | ||
Clear | ||
} | ||
function getEventFromJSON(json): BrowserStorageEvents { | ||
switch (json.type) { | ||
case BrowserStorageEventTypes.Clear: | ||
return new ClearBrowserStorageEvent(json); | ||
case BrowserStorageEventTypes.SetItem: | ||
return new SetItemBrowserStorageEvent(json); | ||
case BrowserStorageEventTypes.RemoveItem: | ||
return new RemoveItemBrowserStorageEvent(json); | ||
default: | ||
return new BrowserStorageEvent(json); | ||
} | ||
} | ||
export class BrowserStorageEvent { | ||
@@ -18,4 +39,6 @@ public readonly name: string; | ||
public readonly newValue; | ||
public isCrossTab: boolean; | ||
public type: BrowserStorageEventTypes = BrowserStorageEventTypes.Default; | ||
constructor({ name, storeName, version, key, oldValue, newValue }: BrowserStorageEventOptions) { | ||
constructor({ name, storeName, version, key, oldValue, newValue, isCrossTab = false }: BrowserStorageEventOptions) { | ||
this.name = name; | ||
@@ -27,11 +50,53 @@ this.storeName = storeName; | ||
this.newValue = newValue; | ||
this.isCrossTab = isCrossTab; | ||
} | ||
public static deserialize(event: string) { | ||
return new BrowserStorageEvent(JSON.parse(event)); | ||
public static deserialize(event: string): BrowserStorageEvents { | ||
const json = JSON.parse(event); | ||
return getEventFromJSON(json); | ||
} | ||
public static serialize(event: BrowserStorageEvent) { | ||
public static serialize(event: BrowserStorageEvent): string { | ||
return JSON.stringify(event); | ||
} | ||
public copyWith({ | ||
name = this.name, | ||
storeName = this.storeName, | ||
version = this.version, | ||
key = this.key, | ||
oldValue = this.oldValue, | ||
newValue = this.newValue, | ||
isCrossTab = this.isCrossTab | ||
}: Partial<BrowserStorageEventOptions>): BrowserStorageEvents { | ||
return getEventFromJSON({ | ||
type: this.type, | ||
name, | ||
storeName, | ||
version, | ||
key, | ||
oldValue, | ||
newValue, | ||
isCrossTab | ||
}); | ||
} | ||
} | ||
export class SetItemBrowserStorageEvent extends BrowserStorageEvent { | ||
public type: BrowserStorageEventTypes = BrowserStorageEventTypes.SetItem; | ||
} | ||
export class RemoveItemBrowserStorageEvent extends BrowserStorageEvent { | ||
public type: BrowserStorageEventTypes = BrowserStorageEventTypes.RemoveItem; | ||
} | ||
export class ClearBrowserStorageEvent extends BrowserStorageEvent { | ||
public type: BrowserStorageEventTypes = BrowserStorageEventTypes.Clear; | ||
} | ||
export type BrowserStorageEvents = | ||
BrowserStorageEvent | | ||
SetItemBrowserStorageEvent | | ||
RemoveItemBrowserStorageEvent | | ||
ClearBrowserStorageEvent; |
@@ -1,2 +0,8 @@ | ||
import { BrowserStorageEvent } from './browser-storage-event'; | ||
import { | ||
BrowserStorageEvent, | ||
BrowserStorageEvents, | ||
ClearBrowserStorageEvent, | ||
RemoveItemBrowserStorageEvent, | ||
SetItemBrowserStorageEvent | ||
} from './browser-storage-event'; | ||
import { BrowserStorageOptions } from './browser-storage-options'; | ||
@@ -17,3 +23,3 @@ import { Driver } from './driver'; | ||
export type HandlerFn = (event: BrowserStorageEvent) => any; | ||
export type HandlerFn = (event: BrowserStorageEvents) => any; | ||
@@ -48,3 +54,14 @@ export class BrowserStorage implements Driver { | ||
public async clear(): Promise<void> { | ||
return this._driver.clear(); | ||
await this._driver.clear(); | ||
const event = new ClearBrowserStorageEvent({ | ||
name: this.options.name, | ||
storeName: this.options.storeName, | ||
version: this.options.version, | ||
key: null, | ||
oldValue: null, | ||
newValue: null | ||
}); | ||
this._triggerEvent(event); | ||
} | ||
@@ -79,3 +96,18 @@ | ||
public async removeItem(key: string): Promise<void> { | ||
return this._driver.removeItem(key); | ||
const oldValue = await this.hasItem(key) | ||
? await this.getItem(key) | ||
: undefined; | ||
await this._driver.removeItem(key); | ||
const event = new RemoveItemBrowserStorageEvent({ | ||
name: this.options.name, | ||
storeName: this.options.storeName, | ||
version: this.options.version, | ||
key, | ||
oldValue, | ||
newValue: null | ||
}); | ||
this._triggerEvent(event); | ||
} | ||
@@ -91,3 +123,3 @@ | ||
const event = new BrowserStorageEvent({ | ||
const event = new SetItemBrowserStorageEvent({ | ||
name: this.options.name, | ||
@@ -101,6 +133,3 @@ storeName: this.options.storeName, | ||
this._applyHandlers(event); | ||
if (this.options.crossTabNotification) { | ||
this._triggerCrossTabEvent(event); | ||
} | ||
this._triggerEvent(event); | ||
@@ -139,4 +168,11 @@ return item; | ||
private _triggerEvent(event: BrowserStorageEvents) { | ||
this._applyHandlers(event); | ||
this._triggerCrossTabEvent(event); | ||
} | ||
private _triggerCrossTabEvent(event: BrowserStorageEvent) { | ||
localStorage.setItem(EVENT_KEY, BrowserStorageEvent.serialize(event)); | ||
if (this.options.crossTabNotification) { | ||
localStorage.setItem(EVENT_KEY, BrowserStorageEvent.serialize(event.copyWith({ isCrossTab: true }))); | ||
} | ||
} | ||
@@ -159,3 +195,3 @@ | ||
private _applyHandlers(event: BrowserStorageEvent) { | ||
private _applyHandlers(event: BrowserStorageEvents) { | ||
this._handlerStore.forEach(fn => fn(event)); | ||
@@ -162,0 +198,0 @@ } |
23680
393