Interface : magasin, routeur, brouillon de séance et modules purs testés
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
74db3014f0
commit
ba08daaa89
@@ -0,0 +1,64 @@
|
||||
// Le magasin de l'interface : il tient le journal (les événements), l'appareil
|
||||
// et l'état replié. Chaque intention passe par dispatch, qui crée UN événement
|
||||
// métier validé, l'ajoute au journal, recalcule l'état et prévient les abonnés.
|
||||
// Sans DOM : testé sous Node (tests/ui/store.test.js).
|
||||
//
|
||||
// À brancher : event-store.js (app/infra/, stockage IndexedDB, autre sous-agent).
|
||||
// Le journal vit pour l'instant en mémoire : il est perdu au rechargement.
|
||||
|
||||
import { createEvent } from "../domain/events.js";
|
||||
import { foldEvents } from "../domain/state.js";
|
||||
import { randomId } from "../domain/ids.js";
|
||||
|
||||
/** @typedef {import("../domain/events.js").DomainEvent} DomainEvent */
|
||||
/** @typedef {import("../domain/events.js").EventType} EventType */
|
||||
/** @typedef {import("../domain/state.js").State} State */
|
||||
|
||||
/**
|
||||
* @typedef {object} Store
|
||||
* @property {string} device
|
||||
* @property {() => State} getState
|
||||
* @property {() => readonly DomainEvent[]} getEvents
|
||||
* @property {<T extends EventType>(type: T, data: Extract<DomainEvent, {type: T}>["data"]) => Extract<DomainEvent, {type: T}>} dispatch
|
||||
* @property {(events: DomainEvent[]) => void} load remplace le journal (au démarrage)
|
||||
* @property {(fn: (state: State) => void) => () => void} subscribe renvoie de quoi se désabonner
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {{events?: DomainEvent[], device?: string, onAppend?: (event: DomainEvent) => void}} [options]
|
||||
* onAppend : appelé pour chaque événement créé (persistance, à brancher)
|
||||
* @returns {Store}
|
||||
*/
|
||||
export function createStore(options = {}) {
|
||||
// Provisoire : l'identifiant d'appareil sera gardé par event-store.js.
|
||||
const device = options.device ?? "browser-" + randomId();
|
||||
/** @type {DomainEvent[]} */
|
||||
let events = [...(options.events ?? [])];
|
||||
let state = foldEvents(events);
|
||||
/** @type {Set<(state: State) => void>} */
|
||||
const listeners = new Set();
|
||||
const notify = () => { for (const fn of [...listeners]) fn(state); };
|
||||
|
||||
return {
|
||||
device,
|
||||
getState: () => state,
|
||||
getEvents: () => events,
|
||||
dispatch(type, data) {
|
||||
const event = createEvent(type, /** @type {any} */ (data), { device });
|
||||
events = [...events, event];
|
||||
state = foldEvents(events);
|
||||
options.onAppend?.(event);
|
||||
notify();
|
||||
return event;
|
||||
},
|
||||
load(list) {
|
||||
events = [...list];
|
||||
state = foldEvents(events);
|
||||
notify();
|
||||
},
|
||||
subscribe(fn) {
|
||||
listeners.add(fn);
|
||||
return () => listeners.delete(fn);
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user