Library Checkout
Two members request the last copy of a book. Catalog decides each request. LoanDesk receives the result through the shared Event Bus.
Catalog owns availability
Catalog keeps available in its Context. If a Source read the Data Model before dispatch, both requests could carry available: 1. A Model Predicate could block the second write, but LoanDesk would already have approved a loan that the Data Model never records.
LoanDesk carries the member ID
Each checkout Event carries memberId in its Event Meta so the reply identifies the member even after another request.
Project both outcomes into one Data Model
Generate the diagrams as TypeScript modules named Catalog and LoanDesk, in that order. Each Slice factory takes a Model Transformer for its declared Effect. stockChanged copies the count from Event Meta, and loanApproved records the member's loan.
TLibraryEvent combines the Event IDs from Catalog and LoanDesk. The CoreLoop accepts both generated Slices and rejects an Event ID from another diagram at compile time. TLibraryEventMeta combines their metadata contracts. Pass it to both Slice factories so each generated FSM accepts the Event Meta that the other diagram produces. The diagram declares the memberId field, and the loanApproved handler checks that its value is a string.
ts
import { AbstractBaseClass, CoreLoop, createEventBus, EffectScheduler, ModelStore, type TEventBusOrder } from '@yantrix/core';
import {
eventDictionary as catalogEvents,
createCatalogSlice,
type TCatalogEvent,
type TCatalogEventMeta,
} from './generated/Catalog.js';
import {
createLoanDeskSlice,
eventDictionary as loanDeskEvents,
type TLoanDeskEvent,
type TLoanDeskEventMeta,
} from './generated/LoanDesk.js';
interface ILibraryModel {
available: number;
loans: Record<string, { memberId: string }>;
}
type TLibraryEvent = TCatalogEvent | TLoanDeskEvent;
type TLibraryEventMeta = TCatalogEventMeta & TLoanDeskEventMeta;
const LibraryBus = createEventBus<TLibraryEvent, TLibraryEventMeta>()(AbstractBaseClass);
function createLibrary(order: TEventBusOrder = 'fifo') {
const modelStore = new ModelStore<ILibraryModel>({ available: 0, loans: {} });
const effectScheduler = new EffectScheduler<ILibraryModel, TLibraryEvent, TLibraryEventMeta>({ store: modelStore });
const coreLoop = new CoreLoop<TLibraryEvent, TLibraryEventMeta, ILibraryModel>({
bus: new LibraryBus(order),
effectScheduler,
});
const catalogSlice = createCatalogSlice<ILibraryModel, TLibraryEventMeta>({
stockChanged: (event, model) => {
const available = event.meta?.available;
if (typeof available !== 'number' || available === model.available) return model;
return { ...model, available };
},
});
const loanDeskSlice = createLoanDeskSlice<ILibraryModel, TLibraryEventMeta>({
loanApproved: (event, model) => {
const memberId = event.meta?.memberId;
if (typeof memberId !== 'string') return model;
return { ...model, loans: { ...model.loans, [memberId]: { memberId } } };
},
});
coreLoop
.registerSlice(catalogSlice)
.registerSlice(loanDeskSlice)
.start();
function restock(copies: number) {
if (!Number.isSafeInteger(copies) || copies < 0) throw new RangeError('Expected a nonnegative copy count');
coreLoop.getBus().dispatch({ event: catalogEvents.restock, meta: { copies } });
}
function requestCheckout(memberId: string) {
coreLoop.getBus().dispatch({ event: loanDeskEvents.checkoutRequested, meta: { memberId } });
}
const catalog = catalogSlice.getMachines().Catalog;
const loanDesk = loanDeskSlice.getMachines().LoanDesk;
return { coreLoop, modelStore, catalog, loanDesk, restock, requestCheckout };
}Queue two requests for the last copy
The Effect Scheduler applies both Effects in one batch. Subscribers see the stock and loan change together.
ts
const library = createLibrary();
try {
library.restock(1);
await library.coreLoop.whenIdle();
const bus = library.coreLoop.getBus();
bus.pause();
library.requestCheckout('A');
library.requestCheckout('B');
bus.resume();
await library.coreLoop.whenIdle();
console.info(library.modelStore.get());
} finally {
library.coreLoop.stop();
}With A queued before B, A receives the last copy under both orders. FIFO preserves A then B at both steps. Under LIFO, LoanDesk handles B then A. The Event Bus then delivers the resulting copyRequested Events newest first, so Catalog handles A first.