aurasu 0.1.0 → 0.1.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.
@@ -0,0 +1,100 @@
1
+ export const APP_STATE_SCHEMA = "aurajs.app-state.v1";
2
+
3
+ function cloneAppStateValue(value) {
4
+ if (Array.isArray(value)) {
5
+ return value.map((entry) => cloneAppStateValue(entry));
6
+ }
7
+ if (value && typeof value === 'object') {
8
+ const output = {};
9
+ for (const [key, entry] of Object.entries(value)) {
10
+ output[key] = cloneAppStateValue(entry);
11
+ }
12
+ return output;
13
+ }
14
+ return value;
15
+ }
16
+
17
+ function createStoreSection() {
18
+ return {};
19
+ }
20
+
21
+ export function createAppState({ projectTitle = 'AuraJS Game', template = 'blank' } = {}) {
22
+ return {
23
+ schema: APP_STATE_SCHEMA,
24
+ projectTitle,
25
+ template,
26
+ session: createStoreSection(),
27
+ ui: createStoreSection(),
28
+ runtime: {
29
+ startSceneId: null,
30
+ currentSceneId: null,
31
+ previousSceneId: null,
32
+ hudScreenId: null,
33
+ overlayScreenId: null,
34
+ modalScreenIds: [],
35
+ },
36
+ };
37
+ }
38
+
39
+ function ensureStoreBucket(appState, bucketName) {
40
+ if (!appState || typeof appState !== 'object' || Array.isArray(appState)) {
41
+ throw new Error('Expected appState to be a mutable object.');
42
+ }
43
+ if (!appState[bucketName] || typeof appState[bucketName] !== 'object' || Array.isArray(appState[bucketName])) {
44
+ appState[bucketName] = createStoreSection();
45
+ }
46
+ return appState[bucketName];
47
+ }
48
+
49
+ export function ensureSessionState(appState, featureKey, seed = {}) {
50
+ const key = typeof featureKey === 'string' ? featureKey.trim() : '';
51
+ if (!key) {
52
+ throw new Error('Expected a non-empty session feature key.');
53
+ }
54
+ const session = ensureStoreBucket(appState, 'session');
55
+ if (!session[key] || typeof session[key] !== 'object' || Array.isArray(session[key])) {
56
+ session[key] = cloneAppStateValue(seed);
57
+ }
58
+ return session[key];
59
+ }
60
+
61
+ export function ensureUiState(appState, featureKey, seed = {}) {
62
+ const key = typeof featureKey === 'string' ? featureKey.trim() : '';
63
+ if (!key) {
64
+ throw new Error('Expected a non-empty UI feature key.');
65
+ }
66
+ const ui = ensureStoreBucket(appState, 'ui');
67
+ if (!ui[key] || typeof ui[key] !== 'object' || Array.isArray(ui[key])) {
68
+ ui[key] = cloneAppStateValue(seed);
69
+ }
70
+ return ui[key];
71
+ }
72
+
73
+ export function normalizeAppState(appState, { projectTitle = 'AuraJS Game', template = 'blank' } = {}) {
74
+ const normalized = appState && typeof appState === 'object' && !Array.isArray(appState)
75
+ ? appState
76
+ : createAppState({ projectTitle, template });
77
+ normalized.schema = APP_STATE_SCHEMA;
78
+ normalized.projectTitle = projectTitle;
79
+ normalized.template = template;
80
+ ensureStoreBucket(normalized, 'session');
81
+ ensureStoreBucket(normalized, 'ui');
82
+ const runtimeState = normalized.runtime && typeof normalized.runtime === 'object' && !Array.isArray(normalized.runtime)
83
+ ? normalized.runtime
84
+ : {};
85
+ normalized.runtime = {
86
+ startSceneId: null,
87
+ currentSceneId: null,
88
+ previousSceneId: null,
89
+ hudScreenId: null,
90
+ overlayScreenId: null,
91
+ modalScreenIds: [],
92
+ ...runtimeState,
93
+ modalScreenIds: Array.isArray(runtimeState.modalScreenIds)
94
+ ? runtimeState.modalScreenIds
95
+ .map((entry) => (typeof entry === 'string' ? entry.trim() : ''))
96
+ .filter((entry) => entry.length > 0)
97
+ : [],
98
+ };
99
+ return normalized;
100
+ }