phaser-hooks 0.7.1 โ†’ 0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"with-computed-state.d.ts","sourceRoot":"","sources":["../../src/hooks/with-computed-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,aAAa,EAAE,MAAM,QAAQ,CAAC;AAG5D;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,EAAE,CAAC,EACpC,OAAO,MAAM,CAAC,KAAK,EACnB,KAAK,MAAM,EACX,aAAa,SAAS,CAAC,CAAC,CAAC,EACzB,UAAU,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,KAC5B,SAAS,CAAC,CAAC,CAoBb,CAAC"}
1
+ {"version":3,"file":"with-computed-state.d.ts","sourceRoot":"","sources":["../../src/hooks/with-computed-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAE,KAAK,aAAa,EAAE,MAAM,QAAQ,CAAC;AAG5D;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,iBAAiB,GAAI,CAAC,EAAE,CAAC,EACpC,OAAO,MAAM,CAAC,KAAK,EACnB,KAAK,MAAM,EACX,aAAa,SAAS,CAAC,CAAC,CAAC,EACzB,UAAU,aAAa,CAAC,CAAC,EAAE,CAAC,CAAC,KAC5B,SAAS,CAAC,CAAC,CA2Cb,CAAC"}
@@ -21,20 +21,37 @@ import { withLocalState } from './with-local-state';
21
21
  * ```
22
22
  */
23
23
  export const withComputedState = (scene, key, sourceState, selector) => {
24
+ const cacheKey = `__phaser-hooks:computed:${key}`;
25
+ const existing = scene.data.get(cacheKey);
26
+ if (existing) {
27
+ return existing;
28
+ }
24
29
  // Initialize with computed value
25
30
  const initialValue = selector(sourceState.get());
26
31
  const computedState = withLocalState(scene, key, initialValue);
32
+ let prev = initialValue;
27
33
  // Update computed state when source changes
28
- sourceState.on('change', () => {
29
- const newSourceValue = sourceState.get();
30
- const newComputedValue = selector(newSourceValue);
31
- computedState.set(newComputedValue);
34
+ const unsubscribe = sourceState.on('change', (newValue) => {
35
+ const next = selector(newValue);
36
+ if (!Object.is(prev, next)) {
37
+ prev = next;
38
+ computedState.set(next);
39
+ }
32
40
  });
33
- return {
41
+ scene.events.once(Phaser.Scenes.Events.DESTROY, () => {
42
+ try {
43
+ unsubscribe?.();
44
+ }
45
+ catch { /* ignore */ }
46
+ scene.data.remove(cacheKey);
47
+ });
48
+ const wrapped = {
34
49
  ...computedState,
35
50
  set: () => {
36
51
  throw new Error(`[withComputedState] Cannot directly set computed state "${key}". Update the source state instead.`);
37
52
  },
38
53
  };
54
+ scene.data.set(cacheKey, wrapped);
55
+ return wrapped;
39
56
  };
40
57
  //# sourceMappingURL=with-computed-state.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"with-computed-state.js","sourceRoot":"","sources":["../../src/hooks/with-computed-state.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEpD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,KAAmB,EACnB,GAAW,EACX,WAAyB,EACzB,QAA6B,EACf,EAAE;IAChB,iCAAiC;IACjC,MAAM,YAAY,GAAG,QAAQ,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,CAAC;IACjD,MAAM,aAAa,GAAG,cAAc,CAAI,KAAK,EAAE,GAAG,EAAE,YAAY,CAAC,CAAC;IAElE,4CAA4C;IAC5C,WAAW,CAAC,EAAE,CAAC,QAAQ,EAAE,GAAG,EAAE;QAC5B,MAAM,cAAc,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC;QACzC,MAAM,gBAAgB,GAAG,QAAQ,CAAC,cAAc,CAAC,CAAC;QAClD,aAAa,CAAC,GAAG,CAAC,gBAAgB,CAAC,CAAC;IACtC,CAAC,CAAC,CAAC;IAEH,OAAO;QACL,GAAG,aAAa;QAChB,GAAG,EAAE,GAAS,EAAE;YACd,MAAM,IAAI,KAAK,CACb,2DAA2D,GAAG,qCAAqC,CACpG,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
1
+ {"version":3,"file":"with-computed-state.js","sourceRoot":"","sources":["../../src/hooks/with-computed-state.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAEpD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAC/B,KAAmB,EACnB,GAAW,EACX,WAAyB,EACzB,QAA6B,EACf,EAAE;IAChB,MAAM,QAAQ,GAAG,2BAA2B,GAAG,EAAE,CAAC;IAElD,MAAM,QAAQ,GAAG,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAA6B,CAAC;IACtE,IAAI,QAAQ,EAAE,CAAC;QACb,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,iCAAiC;IACjC,MAAM,YAAY,GAAG,QAAQ,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,CAAC;IACjD,MAAM,aAAa,GAAG,cAAc,CAAI,KAAK,EAAE,GAAG,EAAE,YAAY,CAAC,CAAC;IAElE,IAAI,IAAI,GAAG,YAAY,CAAC;IAExB,4CAA4C;IAC5C,MAAM,WAAW,GAAG,WAAW,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAQ,EAAE,EAAE;QACxD,MAAM,IAAI,GAAG,QAAQ,CAAC,QAAQ,CAAC,CAAC;QAEhC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,CAAC;YAC3B,IAAI,GAAG,IAAI,CAAC;YACZ,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QAC1B,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,GAAG,EAAE;QACnD,IAAI,CAAC;YACH,WAAW,EAAE,EAAE,CAAC;QAClB,CAAC;QAAC,MAAM,CAAC,CAAC,YAAY,CAAC,CAAC;QAExB,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC9B,CAAC,CAAC,CAAC;IAEH,MAAM,OAAO,GAAiB;QAC5B,GAAG,aAAa;QAChB,GAAG,EAAE,GAAS,EAAE;YACd,MAAM,IAAI,KAAK,CACb,2DAA2D,GAAG,qCAAqC,CACpG,CAAC;QACJ,CAAC;KACF,CAAC;IAEF,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC;IAClC,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC"}
@@ -888,18 +888,35 @@
888
888
  * ```
889
889
  */
890
890
  var withComputedState = function (scene, key, sourceState, selector) {
891
+ var cacheKey = "__phaser-hooks:computed:".concat(key);
892
+ var existing = scene.data.get(cacheKey);
893
+ if (existing) {
894
+ return existing;
895
+ }
891
896
  // Initialize with computed value
892
897
  var initialValue = selector(sourceState.get());
893
898
  var computedState = withLocalState(scene, key, initialValue);
899
+ var prev = initialValue;
894
900
  // Update computed state when source changes
895
- sourceState.on('change', function () {
896
- var newSourceValue = sourceState.get();
897
- var newComputedValue = selector(newSourceValue);
898
- computedState.set(newComputedValue);
901
+ var unsubscribe = sourceState.on('change', function (newValue) {
902
+ var next = selector(newValue);
903
+ if (!Object.is(prev, next)) {
904
+ prev = next;
905
+ computedState.set(next);
906
+ }
907
+ });
908
+ scene.events.once(Phaser.Scenes.Events.DESTROY, function () {
909
+ try {
910
+ unsubscribe === null || unsubscribe === void 0 ? void 0 : unsubscribe();
911
+ }
912
+ catch ( /* ignore */_a) { /* ignore */ }
913
+ scene.data.remove(cacheKey);
899
914
  });
900
- return __assign(__assign({}, computedState), { set: function () {
915
+ var wrapped = __assign(__assign({}, computedState), { set: function () {
901
916
  throw new Error("[withComputedState] Cannot directly set computed state \"".concat(key, "\". Update the source state instead."));
902
917
  } });
918
+ scene.data.set(cacheKey, wrapped);
919
+ return wrapped;
903
920
  };
904
921
 
905
922
  /**
@@ -1 +1 @@
1
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).PhaserHooks={})}(this,function(t){"use strict";var e={numberRange:function(t,e){return function(n){var o=n;return"number"!=typeof o||Number.isNaN(o)?"Value must be a number":!(o<t||o>e)||"Value must be between ".concat(t," and ").concat(e)}},nonEmptyString:function(t){return"string"==typeof t&&0!==t.trim().length||"Value must be a non-empty string"},arrayLength:function(t,e){return function(n){var o=n;return Array.isArray(o)?o.length<t?"Array must have at least ".concat(t," items"):!(void 0!==e&&o.length>e)||"Array must have at most ".concat(e," items"):"Value must be an array"}},oneOf:function(t){return function(e){return!!t.includes(e)||"Value must be one of: ".concat(t.join(", "))}}},n=function(){return n=Object.assign||function(t){for(var e,n=1,o=arguments.length;n<o;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},n.apply(this,arguments)};function o(t,e,n){if(n||2===arguments.length)for(var o,r=0,a=e.length;r<a;r++)!o&&r in e||(o||(o=Array.prototype.slice.call(e,0,r)),o[r]=e[r]);return t.concat(o||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var r=function(){return(new Date).toISOString().replace("T"," ").replace("Z","")},a=function(t){var e=r(),n=t?" %c".concat(t,"%c"):"";return"%c[".concat(e,"]%c ").concat("%c[phaser-hooks]%c").concat(n)},c=function(t){var e=["color: #bd93f9; font-weight: bold;","color: inherit;","color: #2563eb; font-weight: bold;","color: inherit;"];return t&&e.push("color: #059669; font-weight: bold;","color: inherit;"),e},i=function(t,e,n){var i=a("STATE_SET"),l=c("STATE_SET");console.groupCollapsed.apply(console,o(["".concat(i,' Updating state "').concat(t,'"')],l,!1)),console.log("๐Ÿ”‘ Key:",t),console.log("๐Ÿ“ค Old Value:",e),console.log("๐Ÿ“ฅ New Value:",n),console.log("๐Ÿ”„ Changed:",e!==n),console.log("โฐ Timestamp:",r()),console.groupEnd()},l=function(t,e,n){var i=a("EVENT_ADD"),l=c("EVENT_ADD");console.groupCollapsed.apply(console,o(["".concat(i,' Adding listener for "').concat(t,'"')],l,!1)),console.log("๐Ÿ”‘ Key:",t),console.log("๐Ÿ“ก Event:",e),console.log("๐ŸŽฏ Callback:",n.name||"anonymous"),console.log("โฐ Timestamp:",r()),console.groupEnd()},s=function(t,e,n){var i=a("EVENT_REMOVE"),l=c("EVENT_REMOVE");console.groupCollapsed.apply(console,o(["".concat(i,' Removing listener for "').concat(t,'"')],l,!1)),console.log("๐Ÿ”‘ Key:",t),console.log("๐Ÿ“ก Event:",e),console.log("๐ŸŽฏ Callback:",n.name||"anonymous"),console.log("โฐ Timestamp:",r()),console.groupEnd()},u=function(t,e,n){var i=a(t),l=c(t);console.groupCollapsed.apply(console,o(["".concat(i," WARNING")],l,!1)),console.warn("โš ๏ธ Operation:",t),console.warn("๐Ÿ“ข Message:",e),n&&console.warn("๐Ÿ“‹ Context:",n),console.warn("โฐ Timestamp:",r()),console.groupEnd()},f=function(t){return null!==t&&"object"==typeof t&&!Array.isArray(t)&&"[object Object]"===Object.prototype.toString.call(t)},p=function(t){if(null===t||"object"!=typeof t)return t;if(Array.isArray(t))return t.map(function(t){return p(t)});if(f(t)){var e={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=p(t[n]));return e}return t},g=function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];if(!f(t))return t;for(var o=p(t),r=0,a=e;r<a.length;r++){var c=a[r];if(f(c)){var i=p(c);for(var l in i)if(Object.prototype.hasOwnProperty.call(i,l)){var s=i[l],u=o[l];f(s)&&f(u)?o[l]=g(u,s):void 0!==s&&(o[l]=s)}}}return o},h=function(t,e){if("undefined"!=typeof window){var o=window,r=o.__PHASER_HOOKS_CTX__||(o.__PHASER_HOOKS_CTX__=[]);r.push(n({lib:"phaser-hooks",version:"0.6.1"},t));try{e()}finally{r.pop()}}else e()},v=new WeakMap,d=function(t,e,n){var r=t.get(e);return n&&function(t,e){var n=a("STATE_GET"),r=c("STATE_GET");console.log.apply(console,o(o(["".concat(n,' Getting state "').concat(t,'":')],r,!1),[e],!1))}(e,r),r},y=function(t,e,n,l){if(u("DEPRECATED_ONCHANGE","onChange callback is deprecated in phaser-hooks. Use .on('change', callback) or .once('change', callback) instead.",{key:e}),!l||"function"!=typeof l)throw new Error("[withStateDef] onChange callback must be a function");t.events.on("changedata-".concat(e),function(t,e,s,u){n&&i(e,u,s);try{l(s,u)}catch(t){!function(t,e,n){var i=a(t),l=c(t);console.groupCollapsed.apply(console,o(["".concat(i," ERROR")],l,!1)),console.error("๐Ÿšจ Operation:",t),console.error("๐Ÿ’ฅ Error:",e),n&&console.error("๐Ÿ“‹ Context:",n),console.error("โฐ Timestamp:",r()),console.groupEnd()}("ONCHANGE_CALLBACK_ERROR",t,{key:e})}})},w=function(t,e,n,i,l){if(!t.has(e)&&void 0!==l){if(i){var s=i(l);if(!0!==s){var u="string"==typeof s?s:'Invalid initial value for key "'.concat(e,'"');throw new Error("[withStateDef] ".concat(u))}}t.set(e,l),n&&function(t,e){var n=a("STATE_INIT"),i=c("STATE_INIT");console.groupCollapsed.apply(console,o(["".concat(n,' Initializing state "').concat(t,'"')],i,!1)),console.log("๐Ÿ”ง Key:",t),console.log("๐Ÿ“ฆ Initial Value:",e),console.log("โฐ Timestamp:",r()),console.groupEnd()}(e,l)}},E=function(t,e,n){t.events.removeAllListeners("changedata-".concat(e)),n&&function(t){var e=a("CLEAR_LISTENERS"),n=c("CLEAR_LISTENERS");console.groupCollapsed.apply(console,o(["".concat(e,' Clearing all listeners for "').concat(t,'"')],n,!1)),console.log("๐Ÿ”‘ Key:",t),console.log("๐Ÿงน Action:","Removing all event listeners"),console.log("โฐ Timestamp:",r()),console.groupEnd()}(e)},S=function(t,e,n,o){void 0===o&&(o={}),function(t,e,n){if(!t)throw new Error("[withStateDef] Scene parameter is required");if(!n||"string"!=typeof n||0===n.trim().length)throw new Error("[withStateDef] Key must be a non-empty string");if(e.global&&!t.registry)throw new Error("[withStateDef] Scene registry is not available. Ensure the scene is properly initialized.");if(!e.global&&!t.data)throw new Error("[withStateDef] Scene data is not available. Ensure the scene is properly initialized.")}(t,o,e);var r=o.validator,a=o.debug,c=void 0!==a&&a,f=o.global,p=void 0!==f&&f,S=p?t.registry:t.data,m=p?"global":"local";return w(S,e,c,r,n),{get:function(){return d(S,e,c)},set:function(t){return function(t,e,n,o,r,a){var c=t.get(e),l="function"==typeof n?n(c):n;if(a){var s=a(l);if(!0!==s){var u="string"==typeof s?s:'Invalid value for key "'.concat(e,'"');throw new Error("[withStateDef] ".concat(u))}}h({op:"set",oldValue:c,store:e,registryType:r},function(){t.set(e,l)}),o&&i(e,c,l)}(S,e,t,c,m,r)},patch:function(t){return function(t,e,n,o,r,a){var c=t.get(e);if("object"!=typeof c||null===c)throw new Error("[withStateDef] Current value is not an object");var l="function"==typeof n?n(c):n,s=g({},c,l);if(a){var u=a(s);if(!0!==u){var f="string"==typeof u?u:'Invalid value for key "'.concat(e,'"');throw new Error("[withStateDef] ".concat(f))}}h({op:"patch",oldValue:c,store:e,registryType:r},function(){t.set(e,s)}),o&&i(e,c,s)}(S,e,t,c,m,r)},onChange:function(t){return y(S,e,c,t)},on:function(t,n){return function(t,e,n,o,r){if("change"!==e)throw new Error('[withStateDef] Invalid event. Only "change" is supported.');var a=function(t,e,n){r(e,n)};return v.has(r)||v.set(r,a),o&&l(n,e,a),t.events.on("changedata-".concat(n),a),function(){o&&s(n,e,a),t.events.off("changedata-".concat(n),a)}}(S,t,e,c,n)},once:function(t,n){return function(t,e,n,o,r){if("change"!==e)throw new Error('[withStateDef] Invalid event. Only "change" is supported.');var a=function(t,e,n){r(e,n)};return v.has(r)||v.set(r,a),o&&l(n,e,a),t.events.once("changedata-".concat(n),a),function(){o&&s(n,e,a),t.events.off("changedata-".concat(n),a)}}(S,t,e,c,n)},off:function(t,n){return function(t,e,n,o,r){if("change"!==e)throw new Error('[withStateDef] Invalid event. Only "change" is supported.');var a=v.get(r);a?(t.events.off("changedata-".concat(n),a),o&&s(n,e,a)):u("CALLBACK_NOT_FOUND",'Callback not found for key "'.concat(n,'"'),{key:n})}(S,t,e,c,n)},clearListeners:function(){return E(S,e,c)}}},m=function(t,e,o,r){if(!t)throw new Error("[withLocalState] Scene parameter is required");var a="phaser-hooks:local:".concat(e);return S(t,a,o,n(n({},r),{persistent:!1,global:!1}))},b=function(t,e,o,r){if(!t)throw new Error("[withGlobalState] Scene parameter is required");var a="phaser-hooks:global:".concat(e);return S(t,a,o,n(n({},r),{persistent:!1,global:!0}))};t.batchStateUpdates=function(t){t()},t.isValidScene=function(t){return null!=t&&"object"==typeof t&&"registry"in t&&"scene"in t},t.validators=e,t.withComputedState=function(t,e,o,r){var a=r(o.get()),c=m(t,e,a);return o.on("change",function(){var t=o.get(),e=r(t);c.set(e)}),n(n({},c),{set:function(){throw new Error('[withComputedState] Cannot directly set computed state "'.concat(e,'". Update the source state instead.'))}})},t.withDebouncedState=function(t,e,o,r){var a=m(t,e,o),c=null,i=function(t){var e="function"==typeof t?t(a.get()):t;c&&clearTimeout(c),c=setTimeout(function(){a.set(e),c=null},r)};return n(n({},a),{set:i,patch:function(t){var e="function"==typeof t?t(a.get()):t;i(function(t){return g(t,e)})}})},t.withGlobalState=b,t.withLocalState=m,t.withPersistentState=function(t,e,n,o,r){void 0===r&&(r="local");var a=null!=o?o:"phaser-hooks-state:".concat(e),c="local"===r?localStorage:sessionStorage,i=n;try{var l=c.getItem(a);null!==l?i=JSON.parse(l):void 0!==n&&c.setItem(a,JSON.stringify(n))}catch(t){console.warn('[withPersistentState] Failed to load stored state for "'.concat(e,'":'),t)}var s=b(t,e,i);return s.on("change",function(t){try{("local"===r?localStorage:sessionStorage).setItem(a,JSON.stringify(t))}catch(t){console.warn('[withPersistentState] Failed to save state for "'.concat(e,'":'),t)}}),s},t.withStateDef=S,t.withUndoableState=function(t,e,o,r){var a=m(t,e,o),c=m(t,"".concat(e,":history"),[o]),i=m(t,"".concat(e,":historyIndex"),0),l=function(t){var e="function"==typeof t?t(a.get()):t;!function(t){var e=c.get(),n=i.get(),o=e.slice(0,n+1);o.push(t),o.length>r?o.shift():i.set(n+1),c.set(o)}(e),a.set(e)};return n(n({},a),{set:function(t){var e="function"==typeof t?t(a.get()):t;l(e)},patch:function(t){var e="function"==typeof t?t(a.get()):t;l(function(t){return g(t,e)})},undo:function(){var t=i.get();if(t>0){var e=t-1;i.set(e);var n=c.get()[e];if(void 0!==n)return a.set(n),!0}return!1},redo:function(){var t=i.get(),e=c.get();if(t<e.length-1){var n=t+1;i.set(n);var o=e[n];if(void 0!==o)return a.set(o),!0}return!1},canUndo:function(){return i.get()>0},canRedo:function(){return i.get()<c.get().length-1},clearHistory:function(){var t=a.get();c.set([t]),i.set(0)}})}});
1
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).PhaserHooks={})}(this,function(t){"use strict";var e={numberRange:function(t,e){return function(n){var o=n;return"number"!=typeof o||Number.isNaN(o)?"Value must be a number":!(o<t||o>e)||"Value must be between ".concat(t," and ").concat(e)}},nonEmptyString:function(t){return"string"==typeof t&&0!==t.trim().length||"Value must be a non-empty string"},arrayLength:function(t,e){return function(n){var o=n;return Array.isArray(o)?o.length<t?"Array must have at least ".concat(t," items"):!(void 0!==e&&o.length>e)||"Array must have at most ".concat(e," items"):"Value must be an array"}},oneOf:function(t){return function(e){return!!t.includes(e)||"Value must be one of: ".concat(t.join(", "))}}},n=function(){return n=Object.assign||function(t){for(var e,n=1,o=arguments.length;n<o;n++)for(var r in e=arguments[n])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t},n.apply(this,arguments)};function o(t,e,n){if(n||2===arguments.length)for(var o,r=0,a=e.length;r<a;r++)!o&&r in e||(o||(o=Array.prototype.slice.call(e,0,r)),o[r]=e[r]);return t.concat(o||Array.prototype.slice.call(e))}"function"==typeof SuppressedError&&SuppressedError;var r=function(){return(new Date).toISOString().replace("T"," ").replace("Z","")},a=function(t){var e=r(),n=t?" %c".concat(t,"%c"):"";return"%c[".concat(e,"]%c ").concat("%c[phaser-hooks]%c").concat(n)},c=function(t){var e=["color: #bd93f9; font-weight: bold;","color: inherit;","color: #2563eb; font-weight: bold;","color: inherit;"];return t&&e.push("color: #059669; font-weight: bold;","color: inherit;"),e},i=function(t,e,n){var i=a("STATE_SET"),l=c("STATE_SET");console.groupCollapsed.apply(console,o(["".concat(i,' Updating state "').concat(t,'"')],l,!1)),console.log("๐Ÿ”‘ Key:",t),console.log("๐Ÿ“ค Old Value:",e),console.log("๐Ÿ“ฅ New Value:",n),console.log("๐Ÿ”„ Changed:",e!==n),console.log("โฐ Timestamp:",r()),console.groupEnd()},l=function(t,e,n){var i=a("EVENT_ADD"),l=c("EVENT_ADD");console.groupCollapsed.apply(console,o(["".concat(i,' Adding listener for "').concat(t,'"')],l,!1)),console.log("๐Ÿ”‘ Key:",t),console.log("๐Ÿ“ก Event:",e),console.log("๐ŸŽฏ Callback:",n.name||"anonymous"),console.log("โฐ Timestamp:",r()),console.groupEnd()},s=function(t,e,n){var i=a("EVENT_REMOVE"),l=c("EVENT_REMOVE");console.groupCollapsed.apply(console,o(["".concat(i,' Removing listener for "').concat(t,'"')],l,!1)),console.log("๐Ÿ”‘ Key:",t),console.log("๐Ÿ“ก Event:",e),console.log("๐ŸŽฏ Callback:",n.name||"anonymous"),console.log("โฐ Timestamp:",r()),console.groupEnd()},u=function(t,e,n){var i=a(t),l=c(t);console.groupCollapsed.apply(console,o(["".concat(i," WARNING")],l,!1)),console.warn("โš ๏ธ Operation:",t),console.warn("๐Ÿ“ข Message:",e),n&&console.warn("๐Ÿ“‹ Context:",n),console.warn("โฐ Timestamp:",r()),console.groupEnd()},f=function(t){return null!==t&&"object"==typeof t&&!Array.isArray(t)&&"[object Object]"===Object.prototype.toString.call(t)},p=function(t){if(null===t||"object"!=typeof t)return t;if(Array.isArray(t))return t.map(function(t){return p(t)});if(f(t)){var e={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=p(t[n]));return e}return t},g=function(t){for(var e=[],n=1;n<arguments.length;n++)e[n-1]=arguments[n];if(!f(t))return t;for(var o=p(t),r=0,a=e;r<a.length;r++){var c=a[r];if(f(c)){var i=p(c);for(var l in i)if(Object.prototype.hasOwnProperty.call(i,l)){var s=i[l],u=o[l];f(s)&&f(u)?o[l]=g(u,s):void 0!==s&&(o[l]=s)}}}return o},h=function(t,e){if("undefined"!=typeof window){var o=window,r=o.__PHASER_HOOKS_CTX__||(o.__PHASER_HOOKS_CTX__=[]);r.push(n({lib:"phaser-hooks",version:"0.6.1"},t));try{e()}finally{r.pop()}}else e()},v=new WeakMap,d=function(t,e,n){var r=t.get(e);return n&&function(t,e){var n=a("STATE_GET"),r=c("STATE_GET");console.log.apply(console,o(o(["".concat(n,' Getting state "').concat(t,'":')],r,!1),[e],!1))}(e,r),r},y=function(t,e,n,l){if(u("DEPRECATED_ONCHANGE","onChange callback is deprecated in phaser-hooks. Use .on('change', callback) or .once('change', callback) instead.",{key:e}),!l||"function"!=typeof l)throw new Error("[withStateDef] onChange callback must be a function");t.events.on("changedata-".concat(e),function(t,e,s,u){n&&i(e,u,s);try{l(s,u)}catch(t){!function(t,e,n){var i=a(t),l=c(t);console.groupCollapsed.apply(console,o(["".concat(i," ERROR")],l,!1)),console.error("๐Ÿšจ Operation:",t),console.error("๐Ÿ’ฅ Error:",e),n&&console.error("๐Ÿ“‹ Context:",n),console.error("โฐ Timestamp:",r()),console.groupEnd()}("ONCHANGE_CALLBACK_ERROR",t,{key:e})}})},w=function(t,e,n,i,l){if(!t.has(e)&&void 0!==l){if(i){var s=i(l);if(!0!==s){var u="string"==typeof s?s:'Invalid initial value for key "'.concat(e,'"');throw new Error("[withStateDef] ".concat(u))}}t.set(e,l),n&&function(t,e){var n=a("STATE_INIT"),i=c("STATE_INIT");console.groupCollapsed.apply(console,o(["".concat(n,' Initializing state "').concat(t,'"')],i,!1)),console.log("๐Ÿ”ง Key:",t),console.log("๐Ÿ“ฆ Initial Value:",e),console.log("โฐ Timestamp:",r()),console.groupEnd()}(e,l)}},E=function(t,e,n){t.events.removeAllListeners("changedata-".concat(e)),n&&function(t){var e=a("CLEAR_LISTENERS"),n=c("CLEAR_LISTENERS");console.groupCollapsed.apply(console,o(["".concat(e,' Clearing all listeners for "').concat(t,'"')],n,!1)),console.log("๐Ÿ”‘ Key:",t),console.log("๐Ÿงน Action:","Removing all event listeners"),console.log("โฐ Timestamp:",r()),console.groupEnd()}(e)},S=function(t,e,n,o){void 0===o&&(o={}),function(t,e,n){if(!t)throw new Error("[withStateDef] Scene parameter is required");if(!n||"string"!=typeof n||0===n.trim().length)throw new Error("[withStateDef] Key must be a non-empty string");if(e.global&&!t.registry)throw new Error("[withStateDef] Scene registry is not available. Ensure the scene is properly initialized.");if(!e.global&&!t.data)throw new Error("[withStateDef] Scene data is not available. Ensure the scene is properly initialized.")}(t,o,e);var r=o.validator,a=o.debug,c=void 0!==a&&a,f=o.global,p=void 0!==f&&f,S=p?t.registry:t.data,m=p?"global":"local";return w(S,e,c,r,n),{get:function(){return d(S,e,c)},set:function(t){return function(t,e,n,o,r,a){var c=t.get(e),l="function"==typeof n?n(c):n;if(a){var s=a(l);if(!0!==s){var u="string"==typeof s?s:'Invalid value for key "'.concat(e,'"');throw new Error("[withStateDef] ".concat(u))}}h({op:"set",oldValue:c,store:e,registryType:r},function(){t.set(e,l)}),o&&i(e,c,l)}(S,e,t,c,m,r)},patch:function(t){return function(t,e,n,o,r,a){var c=t.get(e);if("object"!=typeof c||null===c)throw new Error("[withStateDef] Current value is not an object");var l="function"==typeof n?n(c):n,s=g({},c,l);if(a){var u=a(s);if(!0!==u){var f="string"==typeof u?u:'Invalid value for key "'.concat(e,'"');throw new Error("[withStateDef] ".concat(f))}}h({op:"patch",oldValue:c,store:e,registryType:r},function(){t.set(e,s)}),o&&i(e,c,s)}(S,e,t,c,m,r)},onChange:function(t){return y(S,e,c,t)},on:function(t,n){return function(t,e,n,o,r){if("change"!==e)throw new Error('[withStateDef] Invalid event. Only "change" is supported.');var a=function(t,e,n){r(e,n)};return v.has(r)||v.set(r,a),o&&l(n,e,a),t.events.on("changedata-".concat(n),a),function(){o&&s(n,e,a),t.events.off("changedata-".concat(n),a)}}(S,t,e,c,n)},once:function(t,n){return function(t,e,n,o,r){if("change"!==e)throw new Error('[withStateDef] Invalid event. Only "change" is supported.');var a=function(t,e,n){r(e,n)};return v.has(r)||v.set(r,a),o&&l(n,e,a),t.events.once("changedata-".concat(n),a),function(){o&&s(n,e,a),t.events.off("changedata-".concat(n),a)}}(S,t,e,c,n)},off:function(t,n){return function(t,e,n,o,r){if("change"!==e)throw new Error('[withStateDef] Invalid event. Only "change" is supported.');var a=v.get(r);a?(t.events.off("changedata-".concat(n),a),o&&s(n,e,a)):u("CALLBACK_NOT_FOUND",'Callback not found for key "'.concat(n,'"'),{key:n})}(S,t,e,c,n)},clearListeners:function(){return E(S,e,c)}}},m=function(t,e,o,r){if(!t)throw new Error("[withLocalState] Scene parameter is required");var a="phaser-hooks:local:".concat(e);return S(t,a,o,n(n({},r),{persistent:!1,global:!1}))},b=function(t,e,o,r){if(!t)throw new Error("[withGlobalState] Scene parameter is required");var a="phaser-hooks:global:".concat(e);return S(t,a,o,n(n({},r),{persistent:!1,global:!0}))};t.batchStateUpdates=function(t){t()},t.isValidScene=function(t){return null!=t&&"object"==typeof t&&"registry"in t&&"scene"in t},t.validators=e,t.withComputedState=function(t,e,o,r){var a="__phaser-hooks:computed:".concat(e),c=t.data.get(a);if(c)return c;var i=r(o.get()),l=m(t,e,i),s=i,u=o.on("change",function(t){var e=r(t);Object.is(s,e)||(s=e,l.set(e))});t.events.once(Phaser.Scenes.Events.DESTROY,function(){try{null==u||u()}catch(t){}t.data.remove(a)});var f=n(n({},l),{set:function(){throw new Error('[withComputedState] Cannot directly set computed state "'.concat(e,'". Update the source state instead.'))}});return t.data.set(a,f),f},t.withDebouncedState=function(t,e,o,r){var a=m(t,e,o),c=null,i=function(t){var e="function"==typeof t?t(a.get()):t;c&&clearTimeout(c),c=setTimeout(function(){a.set(e),c=null},r)};return n(n({},a),{set:i,patch:function(t){var e="function"==typeof t?t(a.get()):t;i(function(t){return g(t,e)})}})},t.withGlobalState=b,t.withLocalState=m,t.withPersistentState=function(t,e,n,o,r){void 0===r&&(r="local");var a=null!=o?o:"phaser-hooks-state:".concat(e),c="local"===r?localStorage:sessionStorage,i=n;try{var l=c.getItem(a);null!==l?i=JSON.parse(l):void 0!==n&&c.setItem(a,JSON.stringify(n))}catch(t){console.warn('[withPersistentState] Failed to load stored state for "'.concat(e,'":'),t)}var s=b(t,e,i);return s.on("change",function(t){try{("local"===r?localStorage:sessionStorage).setItem(a,JSON.stringify(t))}catch(t){console.warn('[withPersistentState] Failed to save state for "'.concat(e,'":'),t)}}),s},t.withStateDef=S,t.withUndoableState=function(t,e,o,r){var a=m(t,e,o),c=m(t,"".concat(e,":history"),[o]),i=m(t,"".concat(e,":historyIndex"),0),l=function(t){var e="function"==typeof t?t(a.get()):t;!function(t){var e=c.get(),n=i.get(),o=e.slice(0,n+1);o.push(t),o.length>r?o.shift():i.set(n+1),c.set(o)}(e),a.set(e)};return n(n({},a),{set:function(t){var e="function"==typeof t?t(a.get()):t;l(e)},patch:function(t){var e="function"==typeof t?t(a.get()):t;l(function(t){return g(t,e)})},undo:function(){var t=i.get();if(t>0){var e=t-1;i.set(e);var n=c.get()[e];if(void 0!==n)return a.set(n),!0}return!1},redo:function(){var t=i.get(),e=c.get();if(t<e.length-1){var n=t+1;i.set(n);var o=e[n];if(void 0!==o)return a.set(o),!0}return!1},canUndo:function(){return i.get()>0},canRedo:function(){return i.get()<c.get().length-1},clearHistory:function(){var t=a.get();c.set([t]),i.set(0)}})}});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "phaser-hooks",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "description": "Hooks in react-style for Phaser games",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",