phaser-hooks 0.7.0 โ 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.
- package/README.md +28 -0
- package/dist/hooks/with-computed-state.d.ts.map +1 -1
- package/dist/hooks/with-computed-state.js +22 -5
- package/dist/hooks/with-computed-state.js.map +1 -1
- package/dist/hooks/with-persistent-state.d.ts.map +1 -1
- package/dist/hooks/with-persistent-state.js +7 -3
- package/dist/hooks/with-persistent-state.js.map +1 -1
- package/dist/phaser-hooks.js +31 -10
- package/dist/phaser-hooks.min.js +1 -1
- package/package.json +1 -1
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-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,
|
|
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
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
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,
|
|
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"}
|
|
@@ -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
|
@@ -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
|
|
897
|
-
|
|
898
|
-
|
|
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
|
-
|
|
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
|
/**
|
|
@@ -1033,13 +1050,17 @@
|
|
|
1033
1050
|
var withPersistentState = function (scene, key, initialValue, storageKey, storageType) {
|
|
1034
1051
|
if (storageType === void 0) { storageType = 'local'; }
|
|
1035
1052
|
var actualStorageKey = storageKey !== null && storageKey !== void 0 ? storageKey : "phaser-hooks-state:".concat(key);
|
|
1053
|
+
var storage = storageType === 'local' ? localStorage : sessionStorage;
|
|
1036
1054
|
// Load from localStorage if available
|
|
1037
1055
|
var storedValue = initialValue;
|
|
1038
1056
|
try {
|
|
1039
|
-
var stored =
|
|
1040
|
-
if (stored) {
|
|
1057
|
+
var stored = storage.getItem(actualStorageKey);
|
|
1058
|
+
if (stored !== null) {
|
|
1041
1059
|
storedValue = JSON.parse(stored);
|
|
1042
1060
|
}
|
|
1061
|
+
else if (initialValue !== undefined) {
|
|
1062
|
+
storage.setItem(actualStorageKey, JSON.stringify(initialValue));
|
|
1063
|
+
}
|
|
1043
1064
|
}
|
|
1044
1065
|
catch (error) {
|
|
1045
1066
|
// eslint-disable-next-line no-console
|
|
@@ -1048,10 +1069,10 @@
|
|
|
1048
1069
|
// @ts-ignore
|
|
1049
1070
|
var state = withGlobalState(scene, key, storedValue);
|
|
1050
1071
|
// Save to localStorage on changes
|
|
1051
|
-
state.
|
|
1072
|
+
state.on('change', function (newValue) {
|
|
1052
1073
|
try {
|
|
1053
|
-
var
|
|
1054
|
-
|
|
1074
|
+
var storage_1 = storageType === 'local' ? localStorage : sessionStorage;
|
|
1075
|
+
storage_1.setItem(actualStorageKey, JSON.stringify(newValue));
|
|
1055
1076
|
}
|
|
1056
1077
|
catch (error) {
|
|
1057
1078
|
// 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="__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)}})}});
|