@uistate/core 4.1.2 → 5.0.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.
- package/LICENSE-eventTest.md +26 -0
- package/README.md +409 -42
- package/cssState.js +32 -1
- package/eventState.js +58 -49
- package/eventTest.js +196 -0
- package/examples/001-counter/README.md +44 -0
- package/examples/001-counter/eventState.js +86 -0
- package/examples/001-counter/index.html +30 -0
- package/examples/002-counter-improved/README.md +44 -0
- package/examples/002-counter-improved/eventState.js +86 -0
- package/examples/002-counter-improved/index.html +44 -0
- package/examples/003-input-reactive/README.md +44 -0
- package/examples/003-input-reactive/eventState.js +86 -0
- package/examples/003-input-reactive/index.html +30 -0
- package/examples/004-computed-state/README.md +45 -0
- package/examples/004-computed-state/eventState.js +86 -0
- package/examples/004-computed-state/index.html +62 -0
- package/examples/005-conditional-rendering/README.md +42 -0
- package/examples/005-conditional-rendering/eventState.js +86 -0
- package/examples/005-conditional-rendering/index.html +36 -0
- package/examples/006-list-rendering/README.md +49 -0
- package/examples/006-list-rendering/eventState.js +86 -0
- package/examples/006-list-rendering/index.html +60 -0
- package/examples/007-form-validation/README.md +52 -0
- package/examples/007-form-validation/eventState.js +86 -0
- package/examples/007-form-validation/index.html +99 -0
- package/examples/008-undo-redo/README.md +70 -0
- package/examples/008-undo-redo/eventState.js +86 -0
- package/examples/008-undo-redo/index.html +105 -0
- package/examples/009-localStorage-side-effects/README.md +72 -0
- package/examples/009-localStorage-side-effects/eventState.js +86 -0
- package/examples/009-localStorage-side-effects/index.html +54 -0
- package/examples/010-decoupled-components/README.md +74 -0
- package/examples/010-decoupled-components/eventState.js +86 -0
- package/examples/010-decoupled-components/index.html +90 -0
- package/examples/011-async-patterns/README.md +98 -0
- package/examples/011-async-patterns/eventState.js +86 -0
- package/examples/011-async-patterns/index.html +129 -0
- package/examples/028-counter-improved-eventTest/LICENSE +55 -0
- package/examples/028-counter-improved-eventTest/README.md +131 -0
- package/examples/028-counter-improved-eventTest/app/store.js +9 -0
- package/examples/028-counter-improved-eventTest/index.html +49 -0
- package/examples/028-counter-improved-eventTest/runtime/core/behaviors.runtime.js +282 -0
- package/examples/028-counter-improved-eventTest/runtime/core/eventState.js +100 -0
- package/examples/028-counter-improved-eventTest/runtime/core/helpers.js +212 -0
- package/examples/028-counter-improved-eventTest/runtime/core/router.js +271 -0
- package/examples/028-counter-improved-eventTest/store.d.ts +8 -0
- package/examples/028-counter-improved-eventTest/style.css +170 -0
- package/examples/028-counter-improved-eventTest/tests/README.md +208 -0
- package/examples/028-counter-improved-eventTest/tests/counter.test.js +116 -0
- package/examples/028-counter-improved-eventTest/tests/eventTest.js +176 -0
- package/examples/028-counter-improved-eventTest/tests/generateTypes.js +168 -0
- package/examples/028-counter-improved-eventTest/tests/run.js +20 -0
- package/examples/030-todo-app-with-eventTest/LICENSE +55 -0
- package/examples/030-todo-app-with-eventTest/README.md +121 -0
- package/examples/030-todo-app-with-eventTest/app/router.js +25 -0
- package/examples/030-todo-app-with-eventTest/app/store.js +16 -0
- package/examples/030-todo-app-with-eventTest/app/views/home.js +11 -0
- package/examples/030-todo-app-with-eventTest/app/views/todoDemo.js +88 -0
- package/examples/030-todo-app-with-eventTest/index.html +65 -0
- package/examples/030-todo-app-with-eventTest/runtime/core/behaviors.runtime.js +282 -0
- package/examples/030-todo-app-with-eventTest/runtime/core/eventState.js +100 -0
- package/examples/030-todo-app-with-eventTest/runtime/core/helpers.js +212 -0
- package/examples/030-todo-app-with-eventTest/runtime/core/router.js +271 -0
- package/examples/030-todo-app-with-eventTest/store.d.ts +18 -0
- package/examples/030-todo-app-with-eventTest/style.css +170 -0
- package/examples/030-todo-app-with-eventTest/tests/README.md +208 -0
- package/examples/030-todo-app-with-eventTest/tests/eventTest.js +176 -0
- package/examples/030-todo-app-with-eventTest/tests/generateTypes.js +189 -0
- package/examples/030-todo-app-with-eventTest/tests/run.js +20 -0
- package/examples/030-todo-app-with-eventTest/tests/todos.test.js +167 -0
- package/examples/031-todo-app-with-eventTest/LICENSE +55 -0
- package/examples/031-todo-app-with-eventTest/README.md +54 -0
- package/examples/031-todo-app-with-eventTest/TUTORIAL.md +390 -0
- package/examples/031-todo-app-with-eventTest/WHY_EVENTSTATE.md +777 -0
- package/examples/031-todo-app-with-eventTest/app/bridges.js +113 -0
- package/examples/031-todo-app-with-eventTest/app/router.js +26 -0
- package/examples/031-todo-app-with-eventTest/app/store.js +15 -0
- package/examples/031-todo-app-with-eventTest/app/views/home.js +46 -0
- package/examples/031-todo-app-with-eventTest/app/views/todoDemo.js +69 -0
- package/examples/031-todo-app-with-eventTest/devtools/dock.js +41 -0
- package/examples/031-todo-app-with-eventTest/devtools/stateTracker.dock.js +10 -0
- package/examples/031-todo-app-with-eventTest/devtools/stateTracker.js +246 -0
- package/examples/031-todo-app-with-eventTest/devtools/telemetry.js +104 -0
- package/examples/031-todo-app-with-eventTest/devtools/typeGenerator.js +339 -0
- package/examples/031-todo-app-with-eventTest/index.html +103 -0
- package/examples/031-todo-app-with-eventTest/package-lock.json +2184 -0
- package/examples/031-todo-app-with-eventTest/package.json +24 -0
- package/examples/031-todo-app-with-eventTest/runtime/core/behaviors.runtime.js +282 -0
- package/examples/031-todo-app-with-eventTest/runtime/core/eventState.js +100 -0
- package/examples/031-todo-app-with-eventTest/runtime/core/helpers.js +212 -0
- package/examples/031-todo-app-with-eventTest/runtime/core/router.js +271 -0
- package/examples/031-todo-app-with-eventTest/runtime/extensions/boundary.js +36 -0
- package/examples/031-todo-app-with-eventTest/runtime/extensions/converge.js +63 -0
- package/examples/031-todo-app-with-eventTest/runtime/extensions/eventState.plus.js +210 -0
- package/examples/031-todo-app-with-eventTest/runtime/extensions/hydrate.js +157 -0
- package/examples/031-todo-app-with-eventTest/runtime/extensions/queryBinding.js +69 -0
- package/examples/031-todo-app-with-eventTest/runtime/forms/computed.js +78 -0
- package/examples/031-todo-app-with-eventTest/runtime/forms/meta.js +51 -0
- package/examples/031-todo-app-with-eventTest/runtime/forms/submitWithBoundary.js +28 -0
- package/examples/031-todo-app-with-eventTest/runtime/forms/validators.js +55 -0
- package/examples/031-todo-app-with-eventTest/store.d.ts +23 -0
- package/examples/031-todo-app-with-eventTest/style.css +170 -0
- package/examples/031-todo-app-with-eventTest/tests/README.md +208 -0
- package/examples/031-todo-app-with-eventTest/tests/eventTest.js +176 -0
- package/examples/031-todo-app-with-eventTest/tests/generateTypes.js +191 -0
- package/examples/031-todo-app-with-eventTest/tests/run.js +20 -0
- package/examples/031-todo-app-with-eventTest/tests/todos.test.js +192 -0
- package/examples/032-todo-app-with-eventTest/LICENSE +55 -0
- package/examples/032-todo-app-with-eventTest/README.md +54 -0
- package/examples/032-todo-app-with-eventTest/TUTORIAL.md +390 -0
- package/examples/032-todo-app-with-eventTest/WHY_EVENTSTATE.md +777 -0
- package/examples/032-todo-app-with-eventTest/app/actions/index.js +153 -0
- package/examples/032-todo-app-with-eventTest/app/bridges.js +113 -0
- package/examples/032-todo-app-with-eventTest/app/router.js +26 -0
- package/examples/032-todo-app-with-eventTest/app/store.js +15 -0
- package/examples/032-todo-app-with-eventTest/app/views/home.js +46 -0
- package/examples/032-todo-app-with-eventTest/app/views/todoDemo.js +69 -0
- package/examples/032-todo-app-with-eventTest/devtools/dock.js +41 -0
- package/examples/032-todo-app-with-eventTest/devtools/stateTracker.dock.js +10 -0
- package/examples/032-todo-app-with-eventTest/devtools/stateTracker.js +246 -0
- package/examples/032-todo-app-with-eventTest/devtools/telemetry.js +104 -0
- package/examples/032-todo-app-with-eventTest/devtools/typeGenerator.js +339 -0
- package/examples/032-todo-app-with-eventTest/index.html +87 -0
- package/examples/032-todo-app-with-eventTest/package-lock.json +2184 -0
- package/examples/032-todo-app-with-eventTest/package.json +24 -0
- package/examples/032-todo-app-with-eventTest/runtime/core/behaviors.runtime.js +282 -0
- package/examples/032-todo-app-with-eventTest/runtime/core/eventState.js +100 -0
- package/examples/032-todo-app-with-eventTest/runtime/core/helpers.js +212 -0
- package/examples/032-todo-app-with-eventTest/runtime/core/router.js +271 -0
- package/examples/032-todo-app-with-eventTest/runtime/extensions/boundary.js +36 -0
- package/examples/032-todo-app-with-eventTest/runtime/extensions/converge.js +63 -0
- package/examples/032-todo-app-with-eventTest/runtime/extensions/eventState.plus.js +210 -0
- package/examples/032-todo-app-with-eventTest/runtime/extensions/hydrate.js +157 -0
- package/examples/032-todo-app-with-eventTest/runtime/extensions/queryBinding.js +69 -0
- package/examples/032-todo-app-with-eventTest/runtime/forms/computed.js +78 -0
- package/examples/032-todo-app-with-eventTest/runtime/forms/meta.js +51 -0
- package/examples/032-todo-app-with-eventTest/runtime/forms/submitWithBoundary.js +28 -0
- package/examples/032-todo-app-with-eventTest/runtime/forms/validators.js +55 -0
- package/examples/032-todo-app-with-eventTest/store.d.ts +23 -0
- package/examples/032-todo-app-with-eventTest/style.css +170 -0
- package/examples/032-todo-app-with-eventTest/tests/README.md +208 -0
- package/examples/032-todo-app-with-eventTest/tests/eventTest.js +176 -0
- package/examples/032-todo-app-with-eventTest/tests/generateTypes.js +191 -0
- package/examples/032-todo-app-with-eventTest/tests/run.js +20 -0
- package/examples/032-todo-app-with-eventTest/tests/todos.test.js +192 -0
- package/index.js +14 -3
- package/package.json +16 -7
- package/stateSerializer.js +99 -4
- package/templateManager.js +50 -2
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>006 List Rendering - EventState</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<h1>Shopping List</h1>
|
|
10
|
+
|
|
11
|
+
<input type="text" id="itemInput" placeholder="Add item...">
|
|
12
|
+
<button id="addBtn">Add</button>
|
|
13
|
+
|
|
14
|
+
<ul id="itemList"></ul>
|
|
15
|
+
|
|
16
|
+
<script type="module">
|
|
17
|
+
import { createEventState } from './eventState.js';
|
|
18
|
+
|
|
19
|
+
// Create store
|
|
20
|
+
const store = createEventState({ items: [] });
|
|
21
|
+
|
|
22
|
+
// Render list
|
|
23
|
+
const renderList = () => {
|
|
24
|
+
const items = store.get('items');
|
|
25
|
+
const list = document.getElementById('itemList');
|
|
26
|
+
|
|
27
|
+
if (items.length === 0) {
|
|
28
|
+
list.innerHTML = '<li><em>No items yet</em></li>';
|
|
29
|
+
} else {
|
|
30
|
+
list.innerHTML = items.map(item => `<li>${item}</li>`).join('');
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// Subscribe to items changes
|
|
35
|
+
store.subscribe('items', renderList);
|
|
36
|
+
|
|
37
|
+
// Add item
|
|
38
|
+
document.getElementById('addBtn').onclick = () => {
|
|
39
|
+
const input = document.getElementById('itemInput');
|
|
40
|
+
const value = input.value.trim();
|
|
41
|
+
|
|
42
|
+
if (value) {
|
|
43
|
+
const items = store.get('items');
|
|
44
|
+
store.set('items', [...items, value]);
|
|
45
|
+
input.value = '';
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// Add on Enter key
|
|
50
|
+
document.getElementById('itemInput').onkeypress = (e) => {
|
|
51
|
+
if (e.key === 'Enter') {
|
|
52
|
+
document.getElementById('addBtn').click();
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// Initial render
|
|
57
|
+
renderList();
|
|
58
|
+
</script>
|
|
59
|
+
</body>
|
|
60
|
+
</html>
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# 007 Form Validation - Real-time Feedback
|
|
2
|
+
|
|
3
|
+
Demonstrates form validation with multiple state paths and computed validity.
|
|
4
|
+
|
|
5
|
+
## What's Here
|
|
6
|
+
|
|
7
|
+
- Email and password inputs
|
|
8
|
+
- Real-time validation messages
|
|
9
|
+
- Submit button enabled only when form is valid
|
|
10
|
+
- **No form library needed** - just state and functions
|
|
11
|
+
|
|
12
|
+
## How It Works
|
|
13
|
+
|
|
14
|
+
```javascript
|
|
15
|
+
// 1. Store input values AND validation state
|
|
16
|
+
const store = createEventState({
|
|
17
|
+
email: '',
|
|
18
|
+
password: '',
|
|
19
|
+
emailValid: false,
|
|
20
|
+
passwordValid: false
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// 2. Validate on input change
|
|
24
|
+
store.subscribe('email', (value) => {
|
|
25
|
+
const isValid = validateEmail(value);
|
|
26
|
+
store.set('emailValid', isValid);
|
|
27
|
+
// Show error message
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// 3. Enable submit when all fields valid
|
|
31
|
+
store.subscribe('emailValid', updateSubmitButton);
|
|
32
|
+
store.subscribe('passwordValid', updateSubmitButton);
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Key Insight
|
|
36
|
+
|
|
37
|
+
**Validation is just derived state.**
|
|
38
|
+
|
|
39
|
+
- Input values are **source state**
|
|
40
|
+
- Validation flags are **derived state**
|
|
41
|
+
- Submit button state is **computed from derived state**
|
|
42
|
+
|
|
43
|
+
Other frameworks need:
|
|
44
|
+
- React: `useForm` hooks, validation libraries
|
|
45
|
+
- Vue: `v-model` + validation plugins
|
|
46
|
+
- Svelte: Stores + validation actions
|
|
47
|
+
|
|
48
|
+
**EventState:** Just subscribe to inputs, validate, and update state. That's it.
|
|
49
|
+
|
|
50
|
+
## Run It
|
|
51
|
+
|
|
52
|
+
Open `index.html` in a browser. Type invalid data to see error messages. Submit button enables when both fields are valid.
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const createEventState = (initial = {}) => {
|
|
2
|
+
const store = JSON.parse(JSON.stringify(initial));
|
|
3
|
+
|
|
4
|
+
const bus = new EventTarget();
|
|
5
|
+
let destroyed = false;
|
|
6
|
+
|
|
7
|
+
return {
|
|
8
|
+
get: (path) => {
|
|
9
|
+
if (destroyed) throw new Error('Cannot get from destroyed store');
|
|
10
|
+
if (!path) return store;
|
|
11
|
+
|
|
12
|
+
// Parse dot-paths: 'items.0' → ['items', '0']
|
|
13
|
+
const parts = path.split('.').flatMap(part => {
|
|
14
|
+
const match = part.match(/([^\[]+)\[(\d+)\]/);
|
|
15
|
+
return match ? [match[1], match[2]] : part;
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
return parts.reduce(
|
|
19
|
+
(obj, prop) => (obj && obj[prop] !== undefined ? obj[prop] : undefined),
|
|
20
|
+
store
|
|
21
|
+
);
|
|
22
|
+
},
|
|
23
|
+
set: (path, value) => {
|
|
24
|
+
if (destroyed) throw new Error('Cannot set on destroyed store');
|
|
25
|
+
if (!path) return;
|
|
26
|
+
const parts = path.split('.');
|
|
27
|
+
const last = parts.pop();
|
|
28
|
+
let target = store;
|
|
29
|
+
|
|
30
|
+
for (const key of parts) {
|
|
31
|
+
// create intermediate objects as needed
|
|
32
|
+
if (typeof target[key] !== 'object' || target[key] === null) {
|
|
33
|
+
target[key] = {};
|
|
34
|
+
}
|
|
35
|
+
target = target[key];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
target[last] = value;
|
|
39
|
+
|
|
40
|
+
if (!destroyed) {
|
|
41
|
+
// exact path
|
|
42
|
+
bus.dispatchEvent(new CustomEvent(path, { detail: value }));
|
|
43
|
+
|
|
44
|
+
// parent wildcards: a, a.b -> 'a.*', 'a.b.*'
|
|
45
|
+
for (let i = 1; i <= parts.length; i++) {
|
|
46
|
+
const parent = parts.slice(0, i).join('.');
|
|
47
|
+
bus.dispatchEvent(new CustomEvent(`${parent}.*`, { detail: { path, value } }));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// root wildcard
|
|
51
|
+
bus.dispatchEvent(new CustomEvent('*', { detail: { path, value } }));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return value;
|
|
55
|
+
},
|
|
56
|
+
subscribe(path, handler) {
|
|
57
|
+
if (destroyed) throw new Error('store destroyed');
|
|
58
|
+
if (!path || typeof handler !== 'function') {
|
|
59
|
+
throw new TypeError('subscribe(path, handler) requires a string path and function handler');
|
|
60
|
+
}
|
|
61
|
+
const onEvent = (evt) => handler(evt.detail, evt);
|
|
62
|
+
bus.addEventListener(path, onEvent);
|
|
63
|
+
|
|
64
|
+
return function unsubscribe() {
|
|
65
|
+
bus.removeEventListener(path, onEvent);
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
off(unsubscribe) {
|
|
69
|
+
if (typeof unsubscribe !== 'function') {
|
|
70
|
+
throw new TypeError('off(unsubscribe) requires a function returned by subscribe');
|
|
71
|
+
}
|
|
72
|
+
return unsubscribe();
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
destroy() {
|
|
76
|
+
if (!destroyed) {
|
|
77
|
+
destroyed = true;
|
|
78
|
+
// EventTarget has no parentNode - just mark as destroyed
|
|
79
|
+
// Future sets/subscribes will be blocked by destroyed flag
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export default createEventState;
|
|
86
|
+
export { createEventState };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>007 Form Validation - EventState</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<h1>Sign Up Form</h1>
|
|
10
|
+
|
|
11
|
+
<label>
|
|
12
|
+
Email:
|
|
13
|
+
<input type="email" id="email" placeholder="you@example.com">
|
|
14
|
+
</label>
|
|
15
|
+
<p id="emailError" style="color: red;"></p>
|
|
16
|
+
|
|
17
|
+
<label>
|
|
18
|
+
Password:
|
|
19
|
+
<input type="password" id="password" placeholder="Min 8 characters">
|
|
20
|
+
</label>
|
|
21
|
+
<p id="passwordError" style="color: red;"></p>
|
|
22
|
+
|
|
23
|
+
<button id="submitBtn" disabled>Submit</button>
|
|
24
|
+
<p id="submitMessage" style="color: green;"></p>
|
|
25
|
+
|
|
26
|
+
<script type="module">
|
|
27
|
+
import { createEventState } from './eventState.js';
|
|
28
|
+
|
|
29
|
+
// Create store
|
|
30
|
+
const store = createEventState({
|
|
31
|
+
email: '',
|
|
32
|
+
password: '',
|
|
33
|
+
emailValid: false,
|
|
34
|
+
passwordValid: false
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// Validate email
|
|
38
|
+
const validateEmail = (email) => {
|
|
39
|
+
return email.includes('@') && email.includes('.');
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Validate password
|
|
43
|
+
const validatePassword = (password) => {
|
|
44
|
+
return password.length >= 8;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// Update submit button state
|
|
48
|
+
const updateSubmitButton = () => {
|
|
49
|
+
const emailValid = store.get('emailValid');
|
|
50
|
+
const passwordValid = store.get('passwordValid');
|
|
51
|
+
document.getElementById('submitBtn').disabled = !(emailValid && passwordValid);
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// Subscribe to email changes
|
|
55
|
+
store.subscribe('email', (value) => {
|
|
56
|
+
const isValid = validateEmail(value);
|
|
57
|
+
store.set('emailValid', isValid);
|
|
58
|
+
|
|
59
|
+
const error = document.getElementById('emailError');
|
|
60
|
+
error.textContent = value && !isValid ? 'Invalid email address' : '';
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Subscribe to password changes
|
|
64
|
+
store.subscribe('password', (value) => {
|
|
65
|
+
const isValid = validatePassword(value);
|
|
66
|
+
store.set('passwordValid', isValid);
|
|
67
|
+
|
|
68
|
+
const error = document.getElementById('passwordError');
|
|
69
|
+
error.textContent = value && !isValid ? 'Password must be at least 8 characters' : '';
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// Subscribe to validation state changes
|
|
73
|
+
store.subscribe('emailValid', updateSubmitButton);
|
|
74
|
+
store.subscribe('passwordValid', updateSubmitButton);
|
|
75
|
+
|
|
76
|
+
// Wire up inputs
|
|
77
|
+
document.getElementById('email').oninput = (e) => {
|
|
78
|
+
store.set('email', e.target.value);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
document.getElementById('password').oninput = (e) => {
|
|
82
|
+
store.set('password', e.target.value);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// Handle submit
|
|
86
|
+
document.getElementById('submitBtn').onclick = () => {
|
|
87
|
+
const email = store.get('email');
|
|
88
|
+
const message = document.getElementById('submitMessage');
|
|
89
|
+
message.textContent = `✓ Form submitted for ${email}`;
|
|
90
|
+
|
|
91
|
+
// Clear form
|
|
92
|
+
document.getElementById('email').value = '';
|
|
93
|
+
document.getElementById('password').value = '';
|
|
94
|
+
store.set('email', '');
|
|
95
|
+
store.set('password', '');
|
|
96
|
+
};
|
|
97
|
+
</script>
|
|
98
|
+
</body>
|
|
99
|
+
</html>
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# 008 Undo/Redo - Time Travel Debugging
|
|
2
|
+
|
|
3
|
+
Demonstrates how EventState's event-driven architecture makes undo/redo trivial.
|
|
4
|
+
|
|
5
|
+
## What's Here
|
|
6
|
+
|
|
7
|
+
- Counter with multiple operations
|
|
8
|
+
- Undo/Redo buttons
|
|
9
|
+
- History tracking showing current position
|
|
10
|
+
- **No special library needed** - just an array
|
|
11
|
+
|
|
12
|
+
## How It Works
|
|
13
|
+
|
|
14
|
+
```javascript
|
|
15
|
+
// 1. Track history in an array
|
|
16
|
+
let history = [0];
|
|
17
|
+
let historyIndex = 0;
|
|
18
|
+
|
|
19
|
+
// 2. Record state changes via subscription
|
|
20
|
+
store.subscribe('count', (value) => {
|
|
21
|
+
if (!isTimeTravel) {
|
|
22
|
+
history.push(value);
|
|
23
|
+
historyIndex = history.length - 1;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// 3. Undo = go back in history
|
|
28
|
+
document.getElementById('undo').onclick = () => {
|
|
29
|
+
historyIndex--;
|
|
30
|
+
isTimeTravel = true;
|
|
31
|
+
store.set('count', history[historyIndex]);
|
|
32
|
+
isTimeTravel = false;
|
|
33
|
+
};
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Key Insight
|
|
37
|
+
|
|
38
|
+
**Time travel is just replaying state.**
|
|
39
|
+
|
|
40
|
+
Because EventState uses **events** for all state changes, you can:
|
|
41
|
+
- ✅ Record every change automatically (wildcard subscription)
|
|
42
|
+
- ✅ Replay any previous state
|
|
43
|
+
- ✅ Build undo/redo in ~30 lines
|
|
44
|
+
|
|
45
|
+
Other frameworks need:
|
|
46
|
+
- React: Redux DevTools, Immer, or custom middleware
|
|
47
|
+
- Vue: Vuex plugins or manual history tracking
|
|
48
|
+
- Svelte: Custom stores with history logic
|
|
49
|
+
|
|
50
|
+
**EventState:** Just subscribe to `'*'` and push to an array. That's it.
|
|
51
|
+
|
|
52
|
+
## This is Telemetry
|
|
53
|
+
|
|
54
|
+
This example shows the **foundation of telemetry**:
|
|
55
|
+
- Every state change is an event
|
|
56
|
+
- Events can be logged, replayed, or analyzed
|
|
57
|
+
- Time-travel debugging comes for free
|
|
58
|
+
|
|
59
|
+
In the full UIstate framework, this becomes:
|
|
60
|
+
- Automatic telemetry logging
|
|
61
|
+
- Event sequence testing (`eventTest.js`)
|
|
62
|
+
- Production debugging and replay
|
|
63
|
+
|
|
64
|
+
## Run It
|
|
65
|
+
|
|
66
|
+
Open `index.html` in a browser. Click operations, then use Undo/Redo to travel through time.
|
|
67
|
+
|
|
68
|
+
## Try This
|
|
69
|
+
|
|
70
|
+
Make multiple changes, undo halfway, then make a new change. Notice how the "future" states are discarded (like Git branches).
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
const createEventState = (initial = {}) => {
|
|
2
|
+
const store = JSON.parse(JSON.stringify(initial));
|
|
3
|
+
|
|
4
|
+
const bus = new EventTarget();
|
|
5
|
+
let destroyed = false;
|
|
6
|
+
|
|
7
|
+
return {
|
|
8
|
+
get: (path) => {
|
|
9
|
+
if (destroyed) throw new Error('Cannot get from destroyed store');
|
|
10
|
+
if (!path) return store;
|
|
11
|
+
|
|
12
|
+
// Parse dot-paths: 'items.0' → ['items', '0']
|
|
13
|
+
const parts = path.split('.').flatMap(part => {
|
|
14
|
+
const match = part.match(/([^\[]+)\[(\d+)\]/);
|
|
15
|
+
return match ? [match[1], match[2]] : part;
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
return parts.reduce(
|
|
19
|
+
(obj, prop) => (obj && obj[prop] !== undefined ? obj[prop] : undefined),
|
|
20
|
+
store
|
|
21
|
+
);
|
|
22
|
+
},
|
|
23
|
+
set: (path, value) => {
|
|
24
|
+
if (destroyed) throw new Error('Cannot set on destroyed store');
|
|
25
|
+
if (!path) return;
|
|
26
|
+
const parts = path.split('.');
|
|
27
|
+
const last = parts.pop();
|
|
28
|
+
let target = store;
|
|
29
|
+
|
|
30
|
+
for (const key of parts) {
|
|
31
|
+
// create intermediate objects as needed
|
|
32
|
+
if (typeof target[key] !== 'object' || target[key] === null) {
|
|
33
|
+
target[key] = {};
|
|
34
|
+
}
|
|
35
|
+
target = target[key];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
target[last] = value;
|
|
39
|
+
|
|
40
|
+
if (!destroyed) {
|
|
41
|
+
// exact path
|
|
42
|
+
bus.dispatchEvent(new CustomEvent(path, { detail: value }));
|
|
43
|
+
|
|
44
|
+
// parent wildcards: a, a.b -> 'a.*', 'a.b.*'
|
|
45
|
+
for (let i = 1; i <= parts.length; i++) {
|
|
46
|
+
const parent = parts.slice(0, i).join('.');
|
|
47
|
+
bus.dispatchEvent(new CustomEvent(`${parent}.*`, { detail: { path, value } }));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// root wildcard
|
|
51
|
+
bus.dispatchEvent(new CustomEvent('*', { detail: { path, value } }));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return value;
|
|
55
|
+
},
|
|
56
|
+
subscribe(path, handler) {
|
|
57
|
+
if (destroyed) throw new Error('store destroyed');
|
|
58
|
+
if (!path || typeof handler !== 'function') {
|
|
59
|
+
throw new TypeError('subscribe(path, handler) requires a string path and function handler');
|
|
60
|
+
}
|
|
61
|
+
const onEvent = (evt) => handler(evt.detail, evt);
|
|
62
|
+
bus.addEventListener(path, onEvent);
|
|
63
|
+
|
|
64
|
+
return function unsubscribe() {
|
|
65
|
+
bus.removeEventListener(path, onEvent);
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
off(unsubscribe) {
|
|
69
|
+
if (typeof unsubscribe !== 'function') {
|
|
70
|
+
throw new TypeError('off(unsubscribe) requires a function returned by subscribe');
|
|
71
|
+
}
|
|
72
|
+
return unsubscribe();
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
destroy() {
|
|
76
|
+
if (!destroyed) {
|
|
77
|
+
destroyed = true;
|
|
78
|
+
// EventTarget has no parentNode - just mark as destroyed
|
|
79
|
+
// Future sets/subscribes will be blocked by destroyed flag
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export default createEventState;
|
|
86
|
+
export { createEventState };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>008 Undo/Redo - EventState</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<h1>Counter with Time Travel</h1>
|
|
10
|
+
|
|
11
|
+
<h2>Count: <span id="count">0</span></h2>
|
|
12
|
+
|
|
13
|
+
<button id="increment">+1</button>
|
|
14
|
+
<button id="decrement">-1</button>
|
|
15
|
+
<button id="double">×2</button>
|
|
16
|
+
|
|
17
|
+
<hr>
|
|
18
|
+
|
|
19
|
+
<button id="undo" disabled>⟲ Undo</button>
|
|
20
|
+
<button id="redo" disabled>⟳ Redo</button>
|
|
21
|
+
|
|
22
|
+
<p><small>History: <span id="historyInfo">0 states</span></small></p>
|
|
23
|
+
|
|
24
|
+
<script type="module">
|
|
25
|
+
import { createEventState } from './eventState.js';
|
|
26
|
+
|
|
27
|
+
// Create store
|
|
28
|
+
const store = createEventState({ count: 0 });
|
|
29
|
+
|
|
30
|
+
// History tracking
|
|
31
|
+
let history = [0]; // Start with initial state
|
|
32
|
+
let historyIndex = 0;
|
|
33
|
+
let isTimeTravel = false; // Flag to prevent recording during undo/redo
|
|
34
|
+
|
|
35
|
+
// Update UI
|
|
36
|
+
const updateUI = () => {
|
|
37
|
+
const count = store.get('count');
|
|
38
|
+
document.getElementById('count').textContent = count;
|
|
39
|
+
|
|
40
|
+
// Update button states
|
|
41
|
+
document.getElementById('undo').disabled = historyIndex === 0;
|
|
42
|
+
document.getElementById('redo').disabled = historyIndex === history.length - 1;
|
|
43
|
+
|
|
44
|
+
// Update history info
|
|
45
|
+
document.getElementById('historyInfo').textContent =
|
|
46
|
+
`${historyIndex + 1} of ${history.length} states`;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// Subscribe to count changes and record history
|
|
50
|
+
store.subscribe('count', (value) => {
|
|
51
|
+
updateUI();
|
|
52
|
+
|
|
53
|
+
// Only record if not time traveling
|
|
54
|
+
if (!isTimeTravel) {
|
|
55
|
+
// Remove any "future" states if we're in the middle of history
|
|
56
|
+
history = history.slice(0, historyIndex + 1);
|
|
57
|
+
|
|
58
|
+
// Add new state
|
|
59
|
+
history.push(value);
|
|
60
|
+
historyIndex = history.length - 1;
|
|
61
|
+
|
|
62
|
+
updateUI();
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// Counter operations
|
|
67
|
+
document.getElementById('increment').onclick = () => {
|
|
68
|
+
store.set('count', store.get('count') + 1);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
document.getElementById('decrement').onclick = () => {
|
|
72
|
+
store.set('count', store.get('count') - 1);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
document.getElementById('double').onclick = () => {
|
|
76
|
+
store.set('count', store.get('count') * 2);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// Undo
|
|
80
|
+
document.getElementById('undo').onclick = () => {
|
|
81
|
+
if (historyIndex > 0) {
|
|
82
|
+
historyIndex--;
|
|
83
|
+
isTimeTravel = true;
|
|
84
|
+
store.set('count', history[historyIndex]);
|
|
85
|
+
isTimeTravel = false;
|
|
86
|
+
updateUI();
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// Redo
|
|
91
|
+
document.getElementById('redo').onclick = () => {
|
|
92
|
+
if (historyIndex < history.length - 1) {
|
|
93
|
+
historyIndex++;
|
|
94
|
+
isTimeTravel = true;
|
|
95
|
+
store.set('count', history[historyIndex]);
|
|
96
|
+
isTimeTravel = false;
|
|
97
|
+
updateUI();
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
// Initial UI update
|
|
102
|
+
updateUI();
|
|
103
|
+
</script>
|
|
104
|
+
</body>
|
|
105
|
+
</html>
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# 009 localStorage Side Effects - Persistent State
|
|
2
|
+
|
|
3
|
+
Demonstrates how to sync state with localStorage for persistence across page reloads.
|
|
4
|
+
|
|
5
|
+
## What's Here
|
|
6
|
+
|
|
7
|
+
- Counter that persists across browser sessions
|
|
8
|
+
- Automatic save on every state change
|
|
9
|
+
- Load saved state on page load
|
|
10
|
+
- **No persistence library needed** - just localStorage API
|
|
11
|
+
|
|
12
|
+
## How It Works
|
|
13
|
+
|
|
14
|
+
```javascript
|
|
15
|
+
// 1. Load initial state from localStorage
|
|
16
|
+
const savedCount = localStorage.getItem('counter');
|
|
17
|
+
const initialCount = savedCount !== null ? parseInt(savedCount, 10) : 0;
|
|
18
|
+
|
|
19
|
+
// 2. Create store with saved state
|
|
20
|
+
const store = createEventState({ count: initialCount });
|
|
21
|
+
|
|
22
|
+
// 3. Side effect: save on every change
|
|
23
|
+
store.subscribe('count', (value) => {
|
|
24
|
+
localStorage.setItem('counter', value);
|
|
25
|
+
// Also update UI
|
|
26
|
+
document.getElementById('count').textContent = value;
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Key Insight
|
|
31
|
+
|
|
32
|
+
**Side effects are just subscribers.**
|
|
33
|
+
|
|
34
|
+
Subscriptions aren't only for UI updates. They're for:
|
|
35
|
+
- ✅ Saving to localStorage
|
|
36
|
+
- ✅ Logging to analytics
|
|
37
|
+
- ✅ Syncing to server
|
|
38
|
+
- ✅ Triggering other actions
|
|
39
|
+
|
|
40
|
+
Other frameworks need:
|
|
41
|
+
- React: `useEffect` with dependency arrays
|
|
42
|
+
- Vue: `watch` or `watchEffect`
|
|
43
|
+
- Svelte: Reactive statements with side effects
|
|
44
|
+
|
|
45
|
+
**EventState:** Just subscribe and do whatever you want. That's it.
|
|
46
|
+
|
|
47
|
+
## Pattern: Separation of Concerns
|
|
48
|
+
|
|
49
|
+
Notice the subscriber does TWO things:
|
|
50
|
+
1. Saves to localStorage (side effect)
|
|
51
|
+
2. Updates the DOM (UI effect)
|
|
52
|
+
|
|
53
|
+
You could split these into separate subscribers:
|
|
54
|
+
```javascript
|
|
55
|
+
store.subscribe('count', (value) => {
|
|
56
|
+
localStorage.setItem('counter', value);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
store.subscribe('count', (value) => {
|
|
60
|
+
document.getElementById('count').textContent = value;
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
This is the **single responsibility principle** in action.
|
|
65
|
+
|
|
66
|
+
## Run It
|
|
67
|
+
|
|
68
|
+
Open `index.html`, increment the counter, then reload the page. Your count persists!
|
|
69
|
+
|
|
70
|
+
## Try This
|
|
71
|
+
|
|
72
|
+
Open DevTools → Application → Local Storage to see the saved value update in real-time.
|