@uistate/core 5.0.3 → 5.1.0

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,149 @@
1
+ /**
2
+ * EventState v2 - Optimized Path-Based State Management
3
+ *
4
+ * A lightweight, performant state management library using path-based subscriptions.
5
+ * Optimized for selective notifications and granular updates.
6
+ *
7
+ * Features:
8
+ * - Path-based get/set operations (e.g., 'user.profile.name')
9
+ * - Selective subscriptions (only relevant subscribers fire)
10
+ * - Wildcard subscriptions (e.g., 'user.*' catches all user changes)
11
+ * - Global subscriptions (e.g., '*' catches all changes)
12
+ * - Zero dependencies
13
+ * - ~2KB minified
14
+ *
15
+ * Performance characteristics:
16
+ * - 2-9x faster than Zustand for selective subscriptions
17
+ * - Competitive overall performance
18
+ * - Minimal rendering overhead (1.27x faster paint times)
19
+ *
20
+ * @example
21
+ * const store = createEventState({ count: 0, user: { name: 'Alice' } });
22
+ *
23
+ * // Subscribe to specific path (receives value directly)
24
+ * const unsub = store.subscribe('count', (value) => {
25
+ * console.log('Count changed:', value);
26
+ * });
27
+ *
28
+ * // Update state
29
+ * store.set('count', 1);
30
+ *
31
+ * // Get state
32
+ * const count = store.get('count');
33
+ *
34
+ * // Wildcard subscription
35
+ * store.subscribe('user.*', ({ path, value }) => {
36
+ * console.log(`User field ${path} changed to:`, value);
37
+ * });
38
+ *
39
+ * // Global subscription
40
+ * store.subscribe('*', ({ path, value }) => {
41
+ * console.log(`State changed at ${path}:`, value);
42
+ * });
43
+ *
44
+ * // Cleanup
45
+ * unsub();
46
+ * store.destroy();
47
+ */
48
+
49
+ export function createEventState(initial = {}) {
50
+ const state = JSON.parse(JSON.stringify(initial));
51
+ const listeners = new Map();
52
+ let destroyed = false;
53
+
54
+ return {
55
+ /**
56
+ * Get value at path
57
+ * @param {string} path - Dot-separated path (e.g., 'user.profile.name')
58
+ * @returns {*} Value at path, or entire state if no path provided
59
+ */
60
+ get(path) {
61
+ if (destroyed) throw new Error('Cannot get from destroyed store');
62
+ if (!path) return state;
63
+ return path.split(".").reduce((obj, key) => obj?.[key], state);
64
+ },
65
+
66
+ /**
67
+ * Set value at path and notify subscribers
68
+ * @param {string} path - Dot-separated path (e.g., 'user.profile.name')
69
+ * @param {*} value - New value
70
+ * @returns {*} The value that was set
71
+ */
72
+ set(path, value) {
73
+ if (destroyed) throw new Error('Cannot set on destroyed store');
74
+ if (!path) return value;
75
+
76
+ const parts = path.split(".");
77
+ const key = parts.pop();
78
+ let cur = state;
79
+
80
+ // Navigate to parent object, creating nested objects as needed
81
+ for (const p of parts) {
82
+ if (!cur[p]) cur[p] = {};
83
+ cur = cur[p];
84
+ }
85
+
86
+ const oldValue = cur[key];
87
+ cur[key] = value;
88
+
89
+ if (!destroyed) {
90
+ const detail = { path, value, oldValue };
91
+
92
+ // Notify exact path subscribers (pass value directly for backwards compatibility)
93
+ const exactListeners = listeners.get(path);
94
+ if (exactListeners) {
95
+ exactListeners.forEach(cb => cb(value));
96
+ }
97
+
98
+ // Notify wildcard subscribers for all parent paths (pass detail object)
99
+ for (let i = 0; i < parts.length; i++) {
100
+ const parentPath = parts.slice(0, i + 1).join('.');
101
+ const wildcardListeners = listeners.get(`${parentPath}.*`);
102
+ if (wildcardListeners) {
103
+ wildcardListeners.forEach(cb => cb(detail));
104
+ }
105
+ }
106
+
107
+ // Notify global subscribers (pass detail object)
108
+ const globalListeners = listeners.get('*');
109
+ if (globalListeners) {
110
+ globalListeners.forEach(cb => cb(detail));
111
+ }
112
+ }
113
+
114
+ return value;
115
+ },
116
+
117
+ /**
118
+ * Subscribe to changes at path
119
+ * @param {string} path - Path to subscribe to (supports wildcards: 'user.*', '*')
120
+ * @param {Function} handler - Callback function receiving { path, value, oldValue }
121
+ * @returns {Function} Unsubscribe function
122
+ */
123
+ subscribe(path, handler) {
124
+ if (destroyed) throw new Error('Cannot subscribe to destroyed store');
125
+ if (!path || typeof handler !== 'function') {
126
+ throw new TypeError('subscribe requires path and handler');
127
+ }
128
+
129
+ if (!listeners.has(path)) {
130
+ listeners.set(path, new Set());
131
+ }
132
+ listeners.get(path).add(handler);
133
+
134
+ return () => listeners.get(path)?.delete(handler);
135
+ },
136
+
137
+ /**
138
+ * Destroy store and clear all subscriptions
139
+ */
140
+ destroy() {
141
+ if (!destroyed) {
142
+ destroyed = true;
143
+ listeners.clear();
144
+ }
145
+ }
146
+ };
147
+ }
148
+
149
+ export default createEventState;
package/eventTest.js CHANGED
@@ -24,7 +24,7 @@
24
24
  * Provides TDD-style testing with type extraction capabilities
25
25
  */
26
26
 
27
- import { createEventState } from './eventState.js';
27
+ import { createEventState } from './eventStateNew.js';
28
28
 
29
29
  export function createEventTest(initialState = {}) {
30
30
  const store = createEventState(initialState);
@@ -1,5 +1,5 @@
1
1
  // store.js — singleton eventState store for counter app
2
- import { createEventState } from '../runtime/core/eventState.js';
2
+ import { createEventState } from '../runtime/core/eventStateNew.js';
3
3
 
4
4
  const initial = {
5
5
  count: 0
@@ -0,0 +1,149 @@
1
+ /**
2
+ * EventState v2 - Optimized Path-Based State Management
3
+ *
4
+ * A lightweight, performant state management library using path-based subscriptions.
5
+ * Optimized for selective notifications and granular updates.
6
+ *
7
+ * Features:
8
+ * - Path-based get/set operations (e.g., 'user.profile.name')
9
+ * - Selective subscriptions (only relevant subscribers fire)
10
+ * - Wildcard subscriptions (e.g., 'user.*' catches all user changes)
11
+ * - Global subscriptions (e.g., '*' catches all changes)
12
+ * - Zero dependencies
13
+ * - ~2KB minified
14
+ *
15
+ * Performance characteristics:
16
+ * - 2-9x faster than Zustand for selective subscriptions
17
+ * - Competitive overall performance
18
+ * - Minimal rendering overhead (1.27x faster paint times)
19
+ *
20
+ * @example
21
+ * const store = createEventState({ count: 0, user: { name: 'Alice' } });
22
+ *
23
+ * // Subscribe to specific path (receives value directly)
24
+ * const unsub = store.subscribe('count', (value) => {
25
+ * console.log('Count changed:', value);
26
+ * });
27
+ *
28
+ * // Update state
29
+ * store.set('count', 1);
30
+ *
31
+ * // Get state
32
+ * const count = store.get('count');
33
+ *
34
+ * // Wildcard subscription
35
+ * store.subscribe('user.*', ({ path, value }) => {
36
+ * console.log(`User field ${path} changed to:`, value);
37
+ * });
38
+ *
39
+ * // Global subscription
40
+ * store.subscribe('*', ({ path, value }) => {
41
+ * console.log(`State changed at ${path}:`, value);
42
+ * });
43
+ *
44
+ * // Cleanup
45
+ * unsub();
46
+ * store.destroy();
47
+ */
48
+
49
+ export function createEventState(initial = {}) {
50
+ const state = JSON.parse(JSON.stringify(initial));
51
+ const listeners = new Map();
52
+ let destroyed = false;
53
+
54
+ return {
55
+ /**
56
+ * Get value at path
57
+ * @param {string} path - Dot-separated path (e.g., 'user.profile.name')
58
+ * @returns {*} Value at path, or entire state if no path provided
59
+ */
60
+ get(path) {
61
+ if (destroyed) throw new Error('Cannot get from destroyed store');
62
+ if (!path) return state;
63
+ return path.split(".").reduce((obj, key) => obj?.[key], state);
64
+ },
65
+
66
+ /**
67
+ * Set value at path and notify subscribers
68
+ * @param {string} path - Dot-separated path (e.g., 'user.profile.name')
69
+ * @param {*} value - New value
70
+ * @returns {*} The value that was set
71
+ */
72
+ set(path, value) {
73
+ if (destroyed) throw new Error('Cannot set on destroyed store');
74
+ if (!path) return value;
75
+
76
+ const parts = path.split(".");
77
+ const key = parts.pop();
78
+ let cur = state;
79
+
80
+ // Navigate to parent object, creating nested objects as needed
81
+ for (const p of parts) {
82
+ if (!cur[p]) cur[p] = {};
83
+ cur = cur[p];
84
+ }
85
+
86
+ const oldValue = cur[key];
87
+ cur[key] = value;
88
+
89
+ if (!destroyed) {
90
+ const detail = { path, value, oldValue };
91
+
92
+ // Notify exact path subscribers (pass value directly for backwards compatibility)
93
+ const exactListeners = listeners.get(path);
94
+ if (exactListeners) {
95
+ exactListeners.forEach(cb => cb(value));
96
+ }
97
+
98
+ // Notify wildcard subscribers for all parent paths (pass detail object)
99
+ for (let i = 0; i < parts.length; i++) {
100
+ const parentPath = parts.slice(0, i + 1).join('.');
101
+ const wildcardListeners = listeners.get(`${parentPath}.*`);
102
+ if (wildcardListeners) {
103
+ wildcardListeners.forEach(cb => cb(detail));
104
+ }
105
+ }
106
+
107
+ // Notify global subscribers (pass detail object)
108
+ const globalListeners = listeners.get('*');
109
+ if (globalListeners) {
110
+ globalListeners.forEach(cb => cb(detail));
111
+ }
112
+ }
113
+
114
+ return value;
115
+ },
116
+
117
+ /**
118
+ * Subscribe to changes at path
119
+ * @param {string} path - Path to subscribe to (supports wildcards: 'user.*', '*')
120
+ * @param {Function} handler - Callback function receiving { path, value, oldValue }
121
+ * @returns {Function} Unsubscribe function
122
+ */
123
+ subscribe(path, handler) {
124
+ if (destroyed) throw new Error('Cannot subscribe to destroyed store');
125
+ if (!path || typeof handler !== 'function') {
126
+ throw new TypeError('subscribe requires path and handler');
127
+ }
128
+
129
+ if (!listeners.has(path)) {
130
+ listeners.set(path, new Set());
131
+ }
132
+ listeners.get(path).add(handler);
133
+
134
+ return () => listeners.get(path)?.delete(handler);
135
+ },
136
+
137
+ /**
138
+ * Destroy store and clear all subscriptions
139
+ */
140
+ destroy() {
141
+ if (!destroyed) {
142
+ destroyed = true;
143
+ listeners.clear();
144
+ }
145
+ }
146
+ };
147
+ }
148
+
149
+ export default createEventState;
@@ -4,7 +4,7 @@
4
4
  * Provides TDD-style testing with type extraction capabilities
5
5
  */
6
6
 
7
- import { createEventState } from '../runtime/core/eventState.js';
7
+ import { createEventState } from '../runtime/core/eventStateNew.js';
8
8
 
9
9
  export function createEventTest(initialState = {}) {
10
10
  const store = createEventState(initialState);
@@ -1,5 +1,5 @@
1
1
  // store.js — singleton eventState store for the SPA
2
- import { createEventState } from '../runtime/core/eventState.js';
2
+ import { createEventState } from '../runtime/core/eventStateNew.js';
3
3
 
4
4
  const initial = {
5
5
  ui: {
@@ -0,0 +1,149 @@
1
+ /**
2
+ * EventState v2 - Optimized Path-Based State Management
3
+ *
4
+ * A lightweight, performant state management library using path-based subscriptions.
5
+ * Optimized for selective notifications and granular updates.
6
+ *
7
+ * Features:
8
+ * - Path-based get/set operations (e.g., 'user.profile.name')
9
+ * - Selective subscriptions (only relevant subscribers fire)
10
+ * - Wildcard subscriptions (e.g., 'user.*' catches all user changes)
11
+ * - Global subscriptions (e.g., '*' catches all changes)
12
+ * - Zero dependencies
13
+ * - ~2KB minified
14
+ *
15
+ * Performance characteristics:
16
+ * - 2-9x faster than Zustand for selective subscriptions
17
+ * - Competitive overall performance
18
+ * - Minimal rendering overhead (1.27x faster paint times)
19
+ *
20
+ * @example
21
+ * const store = createEventState({ count: 0, user: { name: 'Alice' } });
22
+ *
23
+ * // Subscribe to specific path (receives value directly)
24
+ * const unsub = store.subscribe('count', (value) => {
25
+ * console.log('Count changed:', value);
26
+ * });
27
+ *
28
+ * // Update state
29
+ * store.set('count', 1);
30
+ *
31
+ * // Get state
32
+ * const count = store.get('count');
33
+ *
34
+ * // Wildcard subscription
35
+ * store.subscribe('user.*', ({ path, value }) => {
36
+ * console.log(`User field ${path} changed to:`, value);
37
+ * });
38
+ *
39
+ * // Global subscription
40
+ * store.subscribe('*', ({ path, value }) => {
41
+ * console.log(`State changed at ${path}:`, value);
42
+ * });
43
+ *
44
+ * // Cleanup
45
+ * unsub();
46
+ * store.destroy();
47
+ */
48
+
49
+ export function createEventState(initial = {}) {
50
+ const state = JSON.parse(JSON.stringify(initial));
51
+ const listeners = new Map();
52
+ let destroyed = false;
53
+
54
+ return {
55
+ /**
56
+ * Get value at path
57
+ * @param {string} path - Dot-separated path (e.g., 'user.profile.name')
58
+ * @returns {*} Value at path, or entire state if no path provided
59
+ */
60
+ get(path) {
61
+ if (destroyed) throw new Error('Cannot get from destroyed store');
62
+ if (!path) return state;
63
+ return path.split(".").reduce((obj, key) => obj?.[key], state);
64
+ },
65
+
66
+ /**
67
+ * Set value at path and notify subscribers
68
+ * @param {string} path - Dot-separated path (e.g., 'user.profile.name')
69
+ * @param {*} value - New value
70
+ * @returns {*} The value that was set
71
+ */
72
+ set(path, value) {
73
+ if (destroyed) throw new Error('Cannot set on destroyed store');
74
+ if (!path) return value;
75
+
76
+ const parts = path.split(".");
77
+ const key = parts.pop();
78
+ let cur = state;
79
+
80
+ // Navigate to parent object, creating nested objects as needed
81
+ for (const p of parts) {
82
+ if (!cur[p]) cur[p] = {};
83
+ cur = cur[p];
84
+ }
85
+
86
+ const oldValue = cur[key];
87
+ cur[key] = value;
88
+
89
+ if (!destroyed) {
90
+ const detail = { path, value, oldValue };
91
+
92
+ // Notify exact path subscribers (pass value directly for backwards compatibility)
93
+ const exactListeners = listeners.get(path);
94
+ if (exactListeners) {
95
+ exactListeners.forEach(cb => cb(value));
96
+ }
97
+
98
+ // Notify wildcard subscribers for all parent paths (pass detail object)
99
+ for (let i = 0; i < parts.length; i++) {
100
+ const parentPath = parts.slice(0, i + 1).join('.');
101
+ const wildcardListeners = listeners.get(`${parentPath}.*`);
102
+ if (wildcardListeners) {
103
+ wildcardListeners.forEach(cb => cb(detail));
104
+ }
105
+ }
106
+
107
+ // Notify global subscribers (pass detail object)
108
+ const globalListeners = listeners.get('*');
109
+ if (globalListeners) {
110
+ globalListeners.forEach(cb => cb(detail));
111
+ }
112
+ }
113
+
114
+ return value;
115
+ },
116
+
117
+ /**
118
+ * Subscribe to changes at path
119
+ * @param {string} path - Path to subscribe to (supports wildcards: 'user.*', '*')
120
+ * @param {Function} handler - Callback function receiving { path, value, oldValue }
121
+ * @returns {Function} Unsubscribe function
122
+ */
123
+ subscribe(path, handler) {
124
+ if (destroyed) throw new Error('Cannot subscribe to destroyed store');
125
+ if (!path || typeof handler !== 'function') {
126
+ throw new TypeError('subscribe requires path and handler');
127
+ }
128
+
129
+ if (!listeners.has(path)) {
130
+ listeners.set(path, new Set());
131
+ }
132
+ listeners.get(path).add(handler);
133
+
134
+ return () => listeners.get(path)?.delete(handler);
135
+ },
136
+
137
+ /**
138
+ * Destroy store and clear all subscriptions
139
+ */
140
+ destroy() {
141
+ if (!destroyed) {
142
+ destroyed = true;
143
+ listeners.clear();
144
+ }
145
+ }
146
+ };
147
+ }
148
+
149
+ export default createEventState;
@@ -4,7 +4,7 @@
4
4
  * Provides TDD-style testing with type extraction capabilities
5
5
  */
6
6
 
7
- import { createEventState } from '../runtime/core/eventState.js';
7
+ import { createEventState } from '../runtime/core/eventStateNew.js';
8
8
 
9
9
  export function createEventTest(initialState = {}) {
10
10
  const store = createEventState(initialState);
@@ -1,5 +1,5 @@
1
1
  // store.js — singleton eventState store for the SPA
2
- import { createEventState } from '../runtime/core/eventState.js';
2
+ import { createEventState } from '../runtime/core/eventStateNew.js';
3
3
 
4
4
  const initial = {
5
5
  ui: {
@@ -0,0 +1,149 @@
1
+ /**
2
+ * EventState v2 - Optimized Path-Based State Management
3
+ *
4
+ * A lightweight, performant state management library using path-based subscriptions.
5
+ * Optimized for selective notifications and granular updates.
6
+ *
7
+ * Features:
8
+ * - Path-based get/set operations (e.g., 'user.profile.name')
9
+ * - Selective subscriptions (only relevant subscribers fire)
10
+ * - Wildcard subscriptions (e.g., 'user.*' catches all user changes)
11
+ * - Global subscriptions (e.g., '*' catches all changes)
12
+ * - Zero dependencies
13
+ * - ~2KB minified
14
+ *
15
+ * Performance characteristics:
16
+ * - 2-9x faster than Zustand for selective subscriptions
17
+ * - Competitive overall performance
18
+ * - Minimal rendering overhead (1.27x faster paint times)
19
+ *
20
+ * @example
21
+ * const store = createEventState({ count: 0, user: { name: 'Alice' } });
22
+ *
23
+ * // Subscribe to specific path (receives value directly)
24
+ * const unsub = store.subscribe('count', (value) => {
25
+ * console.log('Count changed:', value);
26
+ * });
27
+ *
28
+ * // Update state
29
+ * store.set('count', 1);
30
+ *
31
+ * // Get state
32
+ * const count = store.get('count');
33
+ *
34
+ * // Wildcard subscription
35
+ * store.subscribe('user.*', ({ path, value }) => {
36
+ * console.log(`User field ${path} changed to:`, value);
37
+ * });
38
+ *
39
+ * // Global subscription
40
+ * store.subscribe('*', ({ path, value }) => {
41
+ * console.log(`State changed at ${path}:`, value);
42
+ * });
43
+ *
44
+ * // Cleanup
45
+ * unsub();
46
+ * store.destroy();
47
+ */
48
+
49
+ export function createEventState(initial = {}) {
50
+ const state = JSON.parse(JSON.stringify(initial));
51
+ const listeners = new Map();
52
+ let destroyed = false;
53
+
54
+ return {
55
+ /**
56
+ * Get value at path
57
+ * @param {string} path - Dot-separated path (e.g., 'user.profile.name')
58
+ * @returns {*} Value at path, or entire state if no path provided
59
+ */
60
+ get(path) {
61
+ if (destroyed) throw new Error('Cannot get from destroyed store');
62
+ if (!path) return state;
63
+ return path.split(".").reduce((obj, key) => obj?.[key], state);
64
+ },
65
+
66
+ /**
67
+ * Set value at path and notify subscribers
68
+ * @param {string} path - Dot-separated path (e.g., 'user.profile.name')
69
+ * @param {*} value - New value
70
+ * @returns {*} The value that was set
71
+ */
72
+ set(path, value) {
73
+ if (destroyed) throw new Error('Cannot set on destroyed store');
74
+ if (!path) return value;
75
+
76
+ const parts = path.split(".");
77
+ const key = parts.pop();
78
+ let cur = state;
79
+
80
+ // Navigate to parent object, creating nested objects as needed
81
+ for (const p of parts) {
82
+ if (!cur[p]) cur[p] = {};
83
+ cur = cur[p];
84
+ }
85
+
86
+ const oldValue = cur[key];
87
+ cur[key] = value;
88
+
89
+ if (!destroyed) {
90
+ const detail = { path, value, oldValue };
91
+
92
+ // Notify exact path subscribers (pass value directly for backwards compatibility)
93
+ const exactListeners = listeners.get(path);
94
+ if (exactListeners) {
95
+ exactListeners.forEach(cb => cb(value));
96
+ }
97
+
98
+ // Notify wildcard subscribers for all parent paths (pass detail object)
99
+ for (let i = 0; i < parts.length; i++) {
100
+ const parentPath = parts.slice(0, i + 1).join('.');
101
+ const wildcardListeners = listeners.get(`${parentPath}.*`);
102
+ if (wildcardListeners) {
103
+ wildcardListeners.forEach(cb => cb(detail));
104
+ }
105
+ }
106
+
107
+ // Notify global subscribers (pass detail object)
108
+ const globalListeners = listeners.get('*');
109
+ if (globalListeners) {
110
+ globalListeners.forEach(cb => cb(detail));
111
+ }
112
+ }
113
+
114
+ return value;
115
+ },
116
+
117
+ /**
118
+ * Subscribe to changes at path
119
+ * @param {string} path - Path to subscribe to (supports wildcards: 'user.*', '*')
120
+ * @param {Function} handler - Callback function receiving { path, value, oldValue }
121
+ * @returns {Function} Unsubscribe function
122
+ */
123
+ subscribe(path, handler) {
124
+ if (destroyed) throw new Error('Cannot subscribe to destroyed store');
125
+ if (!path || typeof handler !== 'function') {
126
+ throw new TypeError('subscribe requires path and handler');
127
+ }
128
+
129
+ if (!listeners.has(path)) {
130
+ listeners.set(path, new Set());
131
+ }
132
+ listeners.get(path).add(handler);
133
+
134
+ return () => listeners.get(path)?.delete(handler);
135
+ },
136
+
137
+ /**
138
+ * Destroy store and clear all subscriptions
139
+ */
140
+ destroy() {
141
+ if (!destroyed) {
142
+ destroyed = true;
143
+ listeners.clear();
144
+ }
145
+ }
146
+ };
147
+ }
148
+
149
+ export default createEventState;
@@ -3,7 +3,7 @@
3
3
  // NOTE: This module composes the existing './eventState.js' implementation and returns
4
4
  // an enhanced facade. The original fine-grained semantics (per-path events) remain intact.
5
5
 
6
- import createEventStateBase from '../core/eventState.js';
6
+ import createEventStateBase from '../core/eventStateNew.js';
7
7
 
8
8
  /**
9
9
  * Create an enhanced EventState store while preserving the original semantics.
@@ -4,7 +4,7 @@
4
4
  * Provides TDD-style testing with type extraction capabilities
5
5
  */
6
6
 
7
- import { createEventState } from '../runtime/core/eventState.js';
7
+ import { createEventState } from '../runtime/core/eventStateNew.js';
8
8
 
9
9
  export function createEventTest(initialState = {}) {
10
10
  const store = createEventState(initialState);
@@ -1,5 +1,5 @@
1
1
  // store.js — singleton eventState store for the SPA
2
- import { createEventState } from '../runtime/core/eventState.js';
2
+ import { createEventState } from '../runtime/core/eventStateNew.js';
3
3
 
4
4
  const initial = {
5
5
  ui: {
@@ -0,0 +1,149 @@
1
+ /**
2
+ * EventState v2 - Optimized Path-Based State Management
3
+ *
4
+ * A lightweight, performant state management library using path-based subscriptions.
5
+ * Optimized for selective notifications and granular updates.
6
+ *
7
+ * Features:
8
+ * - Path-based get/set operations (e.g., 'user.profile.name')
9
+ * - Selective subscriptions (only relevant subscribers fire)
10
+ * - Wildcard subscriptions (e.g., 'user.*' catches all user changes)
11
+ * - Global subscriptions (e.g., '*' catches all changes)
12
+ * - Zero dependencies
13
+ * - ~2KB minified
14
+ *
15
+ * Performance characteristics:
16
+ * - 2-9x faster than Zustand for selective subscriptions
17
+ * - Competitive overall performance
18
+ * - Minimal rendering overhead (1.27x faster paint times)
19
+ *
20
+ * @example
21
+ * const store = createEventState({ count: 0, user: { name: 'Alice' } });
22
+ *
23
+ * // Subscribe to specific path (receives value directly)
24
+ * const unsub = store.subscribe('count', (value) => {
25
+ * console.log('Count changed:', value);
26
+ * });
27
+ *
28
+ * // Update state
29
+ * store.set('count', 1);
30
+ *
31
+ * // Get state
32
+ * const count = store.get('count');
33
+ *
34
+ * // Wildcard subscription
35
+ * store.subscribe('user.*', ({ path, value }) => {
36
+ * console.log(`User field ${path} changed to:`, value);
37
+ * });
38
+ *
39
+ * // Global subscription
40
+ * store.subscribe('*', ({ path, value }) => {
41
+ * console.log(`State changed at ${path}:`, value);
42
+ * });
43
+ *
44
+ * // Cleanup
45
+ * unsub();
46
+ * store.destroy();
47
+ */
48
+
49
+ export function createEventState(initial = {}) {
50
+ const state = JSON.parse(JSON.stringify(initial));
51
+ const listeners = new Map();
52
+ let destroyed = false;
53
+
54
+ return {
55
+ /**
56
+ * Get value at path
57
+ * @param {string} path - Dot-separated path (e.g., 'user.profile.name')
58
+ * @returns {*} Value at path, or entire state if no path provided
59
+ */
60
+ get(path) {
61
+ if (destroyed) throw new Error('Cannot get from destroyed store');
62
+ if (!path) return state;
63
+ return path.split(".").reduce((obj, key) => obj?.[key], state);
64
+ },
65
+
66
+ /**
67
+ * Set value at path and notify subscribers
68
+ * @param {string} path - Dot-separated path (e.g., 'user.profile.name')
69
+ * @param {*} value - New value
70
+ * @returns {*} The value that was set
71
+ */
72
+ set(path, value) {
73
+ if (destroyed) throw new Error('Cannot set on destroyed store');
74
+ if (!path) return value;
75
+
76
+ const parts = path.split(".");
77
+ const key = parts.pop();
78
+ let cur = state;
79
+
80
+ // Navigate to parent object, creating nested objects as needed
81
+ for (const p of parts) {
82
+ if (!cur[p]) cur[p] = {};
83
+ cur = cur[p];
84
+ }
85
+
86
+ const oldValue = cur[key];
87
+ cur[key] = value;
88
+
89
+ if (!destroyed) {
90
+ const detail = { path, value, oldValue };
91
+
92
+ // Notify exact path subscribers (pass value directly for backwards compatibility)
93
+ const exactListeners = listeners.get(path);
94
+ if (exactListeners) {
95
+ exactListeners.forEach(cb => cb(value));
96
+ }
97
+
98
+ // Notify wildcard subscribers for all parent paths (pass detail object)
99
+ for (let i = 0; i < parts.length; i++) {
100
+ const parentPath = parts.slice(0, i + 1).join('.');
101
+ const wildcardListeners = listeners.get(`${parentPath}.*`);
102
+ if (wildcardListeners) {
103
+ wildcardListeners.forEach(cb => cb(detail));
104
+ }
105
+ }
106
+
107
+ // Notify global subscribers (pass detail object)
108
+ const globalListeners = listeners.get('*');
109
+ if (globalListeners) {
110
+ globalListeners.forEach(cb => cb(detail));
111
+ }
112
+ }
113
+
114
+ return value;
115
+ },
116
+
117
+ /**
118
+ * Subscribe to changes at path
119
+ * @param {string} path - Path to subscribe to (supports wildcards: 'user.*', '*')
120
+ * @param {Function} handler - Callback function receiving { path, value, oldValue }
121
+ * @returns {Function} Unsubscribe function
122
+ */
123
+ subscribe(path, handler) {
124
+ if (destroyed) throw new Error('Cannot subscribe to destroyed store');
125
+ if (!path || typeof handler !== 'function') {
126
+ throw new TypeError('subscribe requires path and handler');
127
+ }
128
+
129
+ if (!listeners.has(path)) {
130
+ listeners.set(path, new Set());
131
+ }
132
+ listeners.get(path).add(handler);
133
+
134
+ return () => listeners.get(path)?.delete(handler);
135
+ },
136
+
137
+ /**
138
+ * Destroy store and clear all subscriptions
139
+ */
140
+ destroy() {
141
+ if (!destroyed) {
142
+ destroyed = true;
143
+ listeners.clear();
144
+ }
145
+ }
146
+ };
147
+ }
148
+
149
+ export default createEventState;
@@ -3,7 +3,7 @@
3
3
  // NOTE: This module composes the existing './eventState.js' implementation and returns
4
4
  // an enhanced facade. The original fine-grained semantics (per-path events) remain intact.
5
5
 
6
- import createEventStateBase from '../core/eventState.js';
6
+ import createEventStateBase from '../core/eventStateNew.js';
7
7
 
8
8
  /**
9
9
  * Create an enhanced EventState store while preserving the original semantics.
@@ -4,7 +4,7 @@
4
4
  * Provides TDD-style testing with type extraction capabilities
5
5
  */
6
6
 
7
- import { createEventState } from '../runtime/core/eventState.js';
7
+ import { createEventState } from '../runtime/core/eventStateNew.js';
8
8
 
9
9
  export function createEventTest(initialState = {}) {
10
10
  const store = createEventState(initialState);
package/index.js CHANGED
@@ -6,8 +6,8 @@
6
6
  */
7
7
 
8
8
  // Primary: EventState (recommended for application state)
9
- export { createEventState } from './eventState.js';
10
- export { createEventState as default } from './eventState.js';
9
+ export { createEventState } from './eventStateNew.js';
10
+ export { createEventState as default } from './eventStateNew.js';
11
11
 
12
12
  // Specialized: CSS State (for CSS variables and theme management)
13
13
  export { createCssState } from './cssState.js';
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@uistate/core",
3
- "version": "5.0.3",
3
+ "version": "5.1.0",
4
4
  "description": "Lightweight event-driven state management with slot orchestration and experimental event-sequence testing (eventTest.js available under dual license)",
5
5
  "type": "module",
6
6
  "main": "index.js",
7
7
  "exports": {
8
8
  ".": "./index.js",
9
9
  "./eventState": "./eventState.js",
10
+ "./eventStateNew": "./eventStateNew.js",
10
11
  "./cssState": "./cssState.js",
11
12
  "./stateSerializer": "./stateSerializer.js",
12
13
  "./templateManager": "./templateManager.js"
@@ -14,6 +15,7 @@
14
15
  "files": [
15
16
  "index.js",
16
17
  "eventState.js",
18
+ "eventStateNew.js",
17
19
  "cssState.js",
18
20
  "stateSerializer.js",
19
21
  "templateManager.js",