UNPKG

phaser4-rex-plugins

Version:
2 lines (1 loc) 2.76 kB
var t,e;t=void 0,e=function(){var t=function(t,e){return e&&""!==e?`${e}.${t}`:t},e=function(e,s,a){a=JSON.stringify([a]),localStorage.setItem(t(e,s),a)},s=function(e,s){var a=localStorage.getItem(t(e,s));return null==a?void 0:a=JSON.parse(a)[0]},a=function(e,s){return localStorage.removeItem(t(e,s)),this},n={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 a(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.add(t[e]);return this};const r=Phaser.Utils.Objects.GetValue;var o={load:function(t,e){if(void 0===t&&(e=!1),i.call(this),this.defaultData=t,this._syncEnable=!1,this.reset(),e||this.dataKeys.forEach((function(t,e){this.set(t,this.getItem(t))}),this),this._syncEnable=!0,t){var s,a;for(var n in t)s=void 0===(a=e?void 0:this.getItem(n))?t[n]:a,this.set(n,s);this.setItem("__keys__",Array.from(this.dataKeys))}return this},getDefaultValue:function(t){return this.defaultData?this.defaultData[t]:void 0}},h=function(t,e){if(t.hasOwnProperty("_syncEnable"))return t;if(t._syncEnable=!0,t.dataKeys=new Set,t.defaultData=void 0,Object.assign(t,n,o),function(t){t.events.on("changedata",(function(t,e,s,a){this._syncEnable&&("object"!=typeof s&&s===a||(this.setItem(e,s),this.dataKeys.has(e)||(this.dataKeys.add(e),this.setItem("__keys__",Array.from(this.dataKeys)))))}),t).on("setdata",(function(t,e,s){this._syncEnable&&(this.setItem(e,s),this.dataKeys.add(e),this.setItem("__keys__",Array.from(this.dataKeys)))}),t).on("removedata",(function(t,e,s){this._syncEnable&&(this.removeItem(e),this.dataKeys.delete(e),this.setItem("__keys__",Array.from(this.dataKeys)))}),t)}(t),t.name=r(e,"name",""),r(e,"load",!0)){var s=r(e,"default",void 0),a=r(e,"reset",!1);t.load(s,a)}return t};const d=Phaser.Data.DataManager,u=Phaser.Events.EventEmitter,l=Phaser.Utils.Objects.IsPlainObject;class c extends d{constructor(t,e,s){l(t)?(s=t,t=void 0,e=void 0):l(e)&&(s=e,e=void 0);var a=void 0===e;if(a&&(e=new u),void 0===t&&(t=e),super(t,e),a){var n=t.events?t.events:t;n&&n.once("destroy",this.destroy,this)}h(this,s)}}class f extends Phaser.Plugins.BasePlugin{constructor(t){super(t)}start(){this.game.events.on("destroy",this.destroy,this)}add(t,e,s){return new c(t,e,s)}extend(t,e){return h(t,e)}setItem(t,s,a){return e(t,s,a),this}getItem(t,e){return s(t,e)}removeItem(t,e){return a(t,e),this}}return f},"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();