phaser-hooks 0.7.0 โ 0.7.1
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.
package/README.md
CHANGED
|
@@ -336,6 +336,34 @@ export const withPlayer = (scene: Phaser.Scene) => {
|
|
|
336
336
|
playerState.patch((current) => ({ hp: current.hp - 10 })); // Log here
|
|
337
337
|
```
|
|
338
338
|
|
|
339
|
+
## Phaser Data Inspector Extension ๐
|
|
340
|
+
|
|
341
|
+
For an even better debugging experience, use the **Phaser Data Inspector** Chrome extension! It provides a visual interface similar to Redux DevTools, allowing you to inspect and debug your Phaser Hooks state in real-time.
|
|
342
|
+
|
|
343
|
+
<p align="center">
|
|
344
|
+
<img src="data/extension.png" alt="Phaser Data Inspector Extension" style="max-width: 800px">
|
|
345
|
+
</p>
|
|
346
|
+
|
|
347
|
+
### Features
|
|
348
|
+
|
|
349
|
+
- ๐ฏ **Real-Time State Monitoring** - Track all state changes from your Phaser Hooks
|
|
350
|
+
- ๐ **Visual Diff Comparison** - See exactly what changed between state updates
|
|
351
|
+
- ๐ **Search & Filter** - Quickly find specific state keys
|
|
352
|
+
- ๐ **Event History** - Browse through all state changes with pagination
|
|
353
|
+
- ๐จ **Modern UI** - Clean, intuitive interface in Chrome DevTools
|
|
354
|
+
|
|
355
|
+
### Installation
|
|
356
|
+
|
|
357
|
+
๐ [Download Phaser Data Inspector from Chrome Web Store](https://chromewebstore.google.com/detail/phaser-data-inspector/jjcogkkooficbbdhfcamcojmepbjnpdk)
|
|
358
|
+
|
|
359
|
+
### Usage
|
|
360
|
+
|
|
361
|
+
1. Install the extension from the Chrome Web Store
|
|
362
|
+
2. Open Chrome DevTools (F12) on your Phaser game
|
|
363
|
+
3. Navigate to the **"Phaser"** tab in DevTools
|
|
364
|
+
4. All state changes from `phaser-hooks` will automatically appear in the inspector
|
|
365
|
+
|
|
366
|
+
The extension works seamlessly with all Phaser Hooks (`withLocalState`, `withGlobalState`, etc.) and provides enhanced debugging capabilities with visual diffs and state history.
|
|
339
367
|
|
|
340
368
|
## Hook API Reference
|
|
341
369
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"with-persistent-state.d.ts","sourceRoot":"","sources":["../../src/hooks/with-persistent-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,QAAQ,CAAC;AAGxC;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,mBAAmB,GAAI,CAAC,EACnC,OAAO,MAAM,CAAC,KAAK,EACnB,KAAK,MAAM,EACX,cAAc,CAAC,EACf,aAAa,MAAM,EACnB,cAAa,SAAS,GAAG,OAAiB,KACzC,SAAS,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"with-persistent-state.d.ts","sourceRoot":"","sources":["../../src/hooks/with-persistent-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,QAAQ,CAAC;AAGxC;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,mBAAmB,GAAI,CAAC,EACnC,OAAO,MAAM,CAAC,KAAK,EACnB,KAAK,MAAM,EACX,cAAc,CAAC,EACf,aAAa,MAAM,EACnB,cAAa,SAAS,GAAG,OAAiB,KACzC,SAAS,CAAC,CAAC,CAuCb,CAAC"}
|
|
@@ -17,13 +17,17 @@ import { withGlobalState } from './with-global-state';
|
|
|
17
17
|
*/
|
|
18
18
|
export const withPersistentState = (scene, key, initialValue, storageKey, storageType = 'local') => {
|
|
19
19
|
const actualStorageKey = storageKey ?? `phaser-hooks-state:${key}`;
|
|
20
|
+
const storage = storageType === 'local' ? localStorage : sessionStorage;
|
|
20
21
|
// Load from localStorage if available
|
|
21
22
|
let storedValue = initialValue;
|
|
22
23
|
try {
|
|
23
|
-
const stored =
|
|
24
|
-
if (stored) {
|
|
24
|
+
const stored = storage.getItem(actualStorageKey);
|
|
25
|
+
if (stored !== null) {
|
|
25
26
|
storedValue = JSON.parse(stored);
|
|
26
27
|
}
|
|
28
|
+
else if (initialValue !== undefined) {
|
|
29
|
+
storage.setItem(actualStorageKey, JSON.stringify(initialValue));
|
|
30
|
+
}
|
|
27
31
|
}
|
|
28
32
|
catch (error) {
|
|
29
33
|
// eslint-disable-next-line no-console
|
|
@@ -32,7 +36,7 @@ export const withPersistentState = (scene, key, initialValue, storageKey, storag
|
|
|
32
36
|
// @ts-ignore
|
|
33
37
|
const state = withGlobalState(scene, key, storedValue);
|
|
34
38
|
// Save to localStorage on changes
|
|
35
|
-
state.
|
|
39
|
+
state.on('change', (newValue) => {
|
|
36
40
|
try {
|
|
37
41
|
const storage = storageType === 'local' ? localStorage : sessionStorage;
|
|
38
42
|
storage.setItem(actualStorageKey, JSON.stringify(newValue));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"with-persistent-state.js","sourceRoot":"","sources":["../../src/hooks/with-persistent-state.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAEtD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,KAAmB,EACnB,GAAW,EACX,YAAe,EACf,UAAmB,EACnB,cAAmC,OAAO,EAC5B,EAAE;IAChB,MAAM,gBAAgB,GAAG,UAAU,IAAI,sBAAsB,GAAG,EAAE,CAAC;
|
|
1
|
+
{"version":3,"file":"with-persistent-state.js","sourceRoot":"","sources":["../../src/hooks/with-persistent-state.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAEtD;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CACjC,KAAmB,EACnB,GAAW,EACX,YAAe,EACf,UAAmB,EACnB,cAAmC,OAAO,EAC5B,EAAE;IAChB,MAAM,gBAAgB,GAAG,UAAU,IAAI,sBAAsB,GAAG,EAAE,CAAC;IACnE,MAAM,OAAO,GAAG,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,cAAc,CAAC;IAExE,sCAAsC;IACtC,IAAI,WAAW,GAAG,YAAY,CAAC;IAC/B,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC;QACjD,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;YACpB,WAAW,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QACnC,CAAC;aAAM,IAAI,YAAY,KAAK,SAAS,EAAE,CAAC;YACtC,OAAO,CAAC,OAAO,CAAC,gBAAgB,EAAE,IAAI,CAAC,SAAS,CAAC,YAAY,CAAC,CAAC,CAAC;QAClE,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,sCAAsC;QACtC,OAAO,CAAC,IAAI,CACV,0DAA0D,GAAG,IAAI,EACjE,KAAK,CACN,CAAC;IACJ,CAAC;IAED,aAAa;IACb,MAAM,KAAK,GAAG,eAAe,CAAI,KAAK,EAAE,GAAG,EAAE,WAAW,CAAC,CAAC;IAE1D,kCAAkC;IAClC,KAAK,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,QAAiB,EAAE,EAAE;QACvC,IAAI,CAAC;YACH,MAAM,OAAO,GAAG,WAAW,KAAK,OAAO,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,cAAc,CAAC;YACxE,OAAO,CAAC,OAAO,CAAC,gBAAgB,EAAE,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC;QAC9D,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,sCAAsC;YACtC,OAAO,CAAC,IAAI,CACV,mDAAmD,GAAG,IAAI,EAC1D,KAAK,CACN,CAAC;QACJ,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC;AACf,CAAC,CAAC"}
|
package/dist/phaser-hooks.js
CHANGED
|
@@ -1033,13 +1033,17 @@
|
|
|
1033
1033
|
var withPersistentState = function (scene, key, initialValue, storageKey, storageType) {
|
|
1034
1034
|
if (storageType === void 0) { storageType = 'local'; }
|
|
1035
1035
|
var actualStorageKey = storageKey !== null && storageKey !== void 0 ? storageKey : "phaser-hooks-state:".concat(key);
|
|
1036
|
+
var storage = storageType === 'local' ? localStorage : sessionStorage;
|
|
1036
1037
|
// Load from localStorage if available
|
|
1037
1038
|
var storedValue = initialValue;
|
|
1038
1039
|
try {
|
|
1039
|
-
var stored =
|
|
1040
|
-
if (stored) {
|
|
1040
|
+
var stored = storage.getItem(actualStorageKey);
|
|
1041
|
+
if (stored !== null) {
|
|
1041
1042
|
storedValue = JSON.parse(stored);
|
|
1042
1043
|
}
|
|
1044
|
+
else if (initialValue !== undefined) {
|
|
1045
|
+
storage.setItem(actualStorageKey, JSON.stringify(initialValue));
|
|
1046
|
+
}
|
|
1043
1047
|
}
|
|
1044
1048
|
catch (error) {
|
|
1045
1049
|
// eslint-disable-next-line no-console
|
|
@@ -1048,10 +1052,10 @@
|
|
|
1048
1052
|
// @ts-ignore
|
|
1049
1053
|
var state = withGlobalState(scene, key, storedValue);
|
|
1050
1054
|
// Save to localStorage on changes
|
|
1051
|
-
state.
|
|
1055
|
+
state.on('change', function (newValue) {
|
|
1052
1056
|
try {
|
|
1053
|
-
var
|
|
1054
|
-
|
|
1057
|
+
var storage_1 = storageType === 'local' ? localStorage : sessionStorage;
|
|
1058
|
+
storage_1.setItem(actualStorageKey, JSON.stringify(newValue));
|
|
1055
1059
|
}
|
|
1056
1060
|
catch (error) {
|
|
1057
1061
|
// eslint-disable-next-line no-console
|
package/dist/phaser-hooks.min.js
CHANGED
|
@@ -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=n;try{var i="local"===r?localStorage.getItem(a):sessionStorage.getItem(a);i&&(c=JSON.parse(i))}catch(t){console.warn('[withPersistentState] Failed to load stored state for "'.concat(e,'":'),t)}var l=b(t,e,c);return l.onChange(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)}}),l},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=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)}})}});
|