@mostajs/trigger
v0.0.1
Published
Declarative event→action binding (the TRIGGER of the State·Rule·Trigger triptych). Decoupled pub/sub hub with injected rule gate (when) and injected scheduler for deferred actions. Idempotent, fire-and-forget by default.
Downloads
192
Maintainers
Readme
@mostajs/trigger
Liaison déclarative événement → action (la brique TRIGGER du triptyque State · Rule · Trigger, motif Drupal Trigger / reaction rule). Hub pub/sub découplé, gate
wheninjecté (→@mostajs/rules), différé injecté (→@mostajs/queue).
Auteur : Dr Hamid MADANI [email protected] · Licence : AGPL-3.0-or-later
Installation
npm i @mostajs/triggerCœur sans dépendance. Le gate (@mostajs/rules) et le scheduler (@mostajs/queue) sont injectés.
Exemple
import { createTriggerHub } from "@mostajs/trigger";
const hub = createTriggerHub();
hub.define({ on: "enter:validee", do: (ctx) => sendEmail(ctx) });
hub.define({ on: "enter:validee", when: (ctx) => ctx.notify === true, do: (ctx) => audit(ctx), weight: 1 });
await hub.fire("enter:validee", { orderId: 42, notify: true }); // exécute, par poids, les actions dont `when` tientComposition
| Capacité | Injection | Module |
|---|---|---|
| condition (when) | gate | @mostajs/rules |
| différé / planifié (schedule) | scheduler | @mostajs/queue |
| action courante (notify) | dans le do | @mostajs/notifications |
const hub = createTriggerHub({ scheduler: (event, at, ctx) => queue.enqueueAt(event, at, ctx) });
await hub.schedule("subscription.expiring", new Date(2026, 5, 10), { userId: 7 });API
createTriggerHub({ scheduler? }) → TriggerHub. define/register(def), on(event, action, extra?), unregister(name), listTriggers(event?), fire(event, ctx?, { onError? }) → Promise<number>, schedule(event, at, ctx?). TriggerDef = { name?, on, when?, do, weight? }.
Comportement
fireest séquentiel (ordre parweight, déterministe) et fire-and-forget : une action qui échoue n'interrompt pas les autres (erreur viaonError).- Actions idempotentes (un événement peut être rejoué —
@mostajs/queueest at-least-once). when= condition pure.
Statut
v0.0.1 — cœur implémenté et testé (hub pub/sub, define/fire/schedule/unregister, gate, poids, fire-and-forget). Feuille de route : docs/03-PLAN-DEV-TRIGGER.md (0.1 Scheduler réel @mostajs/queue + idempotence, 1.0 = 14 livrables). Étude : docs/01-ETUDE-ETAT-ART-TRIGGER-07062026.md.
