createPlayBookUtils returns a pair of functions — playBookEvent and playBookEvents — that drive a game book: an ordered list of events returned by the RGS (e.g. reveal, winInfo, setWin). Each event is dispatched to its matching handler and awaited before the next one starts.
Function signature
Parameters
A map of
bookEvent.type strings to async handler functions. The generic type parameter TBookEventHandlerMap must extend BookEventHandlerMap<TBookEvent, THandlerContext>, which gives TypeScript the information it needs to infer TBookEvent and the context shape from a single source of truth.Each handler has the signature:When
true, logs each bookEvent object to the console via console.log before its handler is called. Useful during development to trace book playback.Return value
Plays a single book event. Looks up
bookEvent.type in bookEventHandlerMap, then awaits the matching handler. If no handler is found, logs an error to the console — it does not throw.TBookEventContext is the handler context type merged with { bookEvents: TBookEvent[] }, so every handler receives the full book alongside the current event.Plays a list of book events in sequence — one at a time, in order — using an internal If
sequence() utility (not Promise.all). This is intentional: game events have strict ordering requirements. A reel must finish spinning before wins are shown; free-spin triggers must complete before the next spin begins.bookEventContext is omitted, an empty object is used. The full bookEvents array is automatically injected into the context passed to each handler, so handlers can inspect the complete book (e.g. to detect bonus rounds).TypeScript types
These types are exported fromutils-book.
GetBookEventFromMap and GetBookEventContextFromMap let you derive types from the map without repeating yourself:
createMultiBookUtils
For games that play multiple books simultaneously (e.g. side-by-side bonus rounds),createMultiBookUtils provides two utilities:
splitBookToRounds— splits a single flat book array into rounds, breaking on a specified set of event types.booksToRounds/roundsToBooks— zips multiple books into interleaved rounds so that round 1 from each book plays together, then round 2, and so on.
createMultiBookUtils when you need to synchronise multiple books round-by-round rather than playing them serially. For single-book games, createPlayBookUtils is sufficient.
Usage example
playBookEvents uses sequence() internally — a sequential async iterator. This is not Promise.all. Each handler must fully resolve before the next event begins, preserving the visual order of the game.