UNPKG

@petkoneo/phaser3-rex-plugins

Version:
2 lines (1 loc) 2.77 kB
var t,e;t=void 0,e=function(){var t=function(t,e){return e&&""!==e?`${e}.${t}`:t},e=function(e,s,n){n=JSON.stringify([n]),localStorage.setItem(t(e,s),n)},s=function(e,s){var n=localStorage.getItem(t(e,s));return null==n?void 0:n=JSON.parse(n)[0]},n=function(e,s){return localStorage.removeItem(t(e,s)),this},a={getStoreKey(e){return t(e,this.name)},getDataKey(t){return e=t,(s=this.name)&&""!==s?e.substring(s.length+1):e;var e,s},setItem(t,s){return e(t,this.name,s),this},getItem(t){return s(t,this.name)},removeItem(t){return n(t,this.name),this}},i=function(){this.dataKeys.clear();var t=this.getItem("__keys__");if(t)for(var e=0,s=t.length;e<s;e++)this.dataKeys.set(t[e]);return this};const r=Phaser.Utils.Objects.GetValue,o=Phaser.Structs.Set;var h={load:function(t,e){if(void 0===t&&(e=!1),i.call(this),this.defaultData=t,this._syncEnable=!1,this.reset(),e||this.dataKeys.iterate((function(t,e){this.set(t,this.getItem(t))}),this),this._syncEnable=!0,t){var s,n;for(var a in t)s=void 0===(n=e?void 0:this.getItem(a))?t[a]:n,this.set(a,s);this.setItem("__keys__",this.dataKeys.entries)}return this},getDefaultValue:function(t){return this.defaultData?this.defaultData[t]:void 0}},d=function(t,e){if(t.hasOwnProperty("_syncEnable"))return t;if(t._syncEnable=!0,t.dataKeys=new o,t.defaultData=void 0,Object.assign(t,a,h),function(t){t.events.on("changedata",(function(t,e,s,n){this._syncEnable&&("object"!=typeof s&&s===n||(this.setItem(e,s),this.dataKeys.contains(e)||(this.dataKeys.set(e),this.setItem("__keys__",this.dataKeys.entries))))}),t).on("setdata",(function(t,e,s){this._syncEnable&&(this.setItem(e,s),this.dataKeys.set(e),this.setItem("__keys__",this.dataKeys.entries))}),t).on("removedata",(function(t,e,s){this._syncEnable&&(this.removeItem(e),this.dataKeys.delete(e),this.setItem("__keys__",this.dataKeys.entries))}),t)}(t),t.name=r(e,"name",""),r(e,"load",!0)){var s=r(e,"default",void 0),n=r(e,"reset",!1);t.load(s,n)}return t};const u=Phaser.Data.DataManager,l=Phaser.Events.EventEmitter,c=Phaser.Utils.Objects.IsPlainObject;class f extends u{constructor(t,e,s){c(t)?(s=t,t=void 0,e=void 0):c(e)&&(s=e,e=void 0);var n=void 0===e;if(n&&(e=new l),void 0===t&&(t=e),super(t,e),n){var a=t.events?t.events:t;a&&a.once("destroy",this.destroy,this)}d(this,s)}}class y extends Phaser.Plugins.BasePlugin{constructor(t){super(t)}start(){this.game.events.on("destroy",this.destroy,this)}add(t,e,s){return new f(t,e,s)}extend(t,e){return d(t,e)}setItem(t,s,n){return e(t,s,n),this}getItem(t,e){return s(t,e)}removeItem(t,e){return n(t,e),this}}return y},"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).rexlocalstoragedataplugin=e();