init
This commit is contained in:
34
static/js/state.js
Normal file
34
static/js/state.js
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* static/js/state.js
|
||||
* Centralised reactive state store.
|
||||
* Components subscribe to state slices and are notified on change.
|
||||
*/
|
||||
|
||||
const _state = {
|
||||
instructions: [], // InstructionItem[]
|
||||
status: null, // StatusResponse | null
|
||||
config: null, // ConfigResponse | null
|
||||
serverOnline: false,
|
||||
};
|
||||
|
||||
const _listeners = {}; // key -> Set<fn>
|
||||
|
||||
export const state = {
|
||||
get(key) {
|
||||
return _state[key];
|
||||
},
|
||||
|
||||
set(key, value) {
|
||||
_state[key] = value;
|
||||
(_listeners[key] || new Set()).forEach(fn => fn(value));
|
||||
},
|
||||
|
||||
subscribe(key, fn) {
|
||||
if (!_listeners[key]) _listeners[key] = new Set();
|
||||
_listeners[key].add(fn);
|
||||
// Immediately call with current value
|
||||
fn(_state[key]);
|
||||
return () => _listeners[key].delete(fn); // unsubscribe
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user