-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate-management.js
More file actions
137 lines (116 loc) · 2.86 KB
/
Copy pathstate-management.js
File metadata and controls
137 lines (116 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Example: State Management with RailJS
* Similar to Redux but with isolated modules
*/
import { Rail } from '../rail.js';
const rail = new Rail({ debug: false });
// State Store Module
const storeModule = {
name: 'store',
state: {
todos: [],
user: null,
theme: 'light'
},
connect(rail) {
// Handle state updates
rail.on('state.update', (data) => {
const { key, value } = data;
const oldValue = this.state[key];
this.state[key] = value;
// Emit state change event
rail.emit('state.changed', {
key,
oldValue,
newValue: value,
state: { ...this.state }
});
}, 'store');
// Handle state queries
rail.on('state.get', (data) => {
const { key } = data;
rail.emit('state.value', {
key,
value: this.state[key]
});
}, 'store');
}
};
// Todo Module
const todoModule = {
name: 'todos',
connect(rail) {
rail.on('todo.add', (data) => {
rail.on('state.value', (stateData) => {
if (stateData.key === 'todos') {
const newTodos = [...stateData.value, {
id: Date.now(),
text: data.text,
completed: false
}];
rail.emit('state.update', { key: 'todos', value: newTodos });
}
}, 'todos-temp');
rail.emit('state.get', { key: 'todos' });
}, 'todos');
rail.on('todo.toggle', (data) => {
rail.on('state.value', (stateData) => {
if (stateData.key === 'todos') {
const newTodos = stateData.value.map(todo =>
todo.id === data.id ? { ...todo, completed: !todo.completed } : todo
);
rail.emit('state.update', { key: 'todos', value: newTodos });
}
}, 'todos-temp');
rail.emit('state.get', { key: 'todos' });
}, 'todos');
}
};
// UI Module (simulated)
const uiModule = {
name: 'ui',
connect(rail) {
rail.on('state.changed', (data) => {
console.log('\n🎨 UI Update:');
console.log(` ${data.key} changed`);
console.log(` Old:`, data.oldValue);
console.log(` New:`, data.newValue);
}, 'ui');
}
};
// Logger Module
const loggerModule = {
name: 'logger',
history: [],
connect(rail) {
rail.on('state.changed', (data) => {
const entry = {
timestamp: new Date(),
action: `${data.key}.update`,
oldValue: data.oldValue,
newValue: data.newValue
};
this.history.push(entry);
console.log('📝 Action logged:', entry.action);
}, 'logger');
}
};
// Attach modules
rail.attach(storeModule);
rail.attach(todoModule);
rail.attach(uiModule);
rail.attach(loggerModule);
// Simulate user interactions
console.log('=== State Management Example ===\n');
console.log('Adding todos...');
rail.emit('todo.add', { text: 'Learn RailJS' });
setTimeout(() => {
rail.emit('todo.add', { text: 'Build an app' });
}, 500);
setTimeout(() => {
console.log('\nToggling first todo...');
rail.emit('todo.toggle', { id: Date.now() - 500 });
}, 1000);
setTimeout(() => {
rail.emit('state.update', { key: 'theme', value: 'dark' });
}, 1500);