forked from thousandyears/Lexicon
-
Notifications
You must be signed in to change notification settings - Fork 0
Runtime Events
Oliver Atkinson edited this page May 31, 2026
·
2 revisions
SwiftLexicon is the runtime used by the swift generator. It provides generated values and async event streams keyed by generated lemmas.
Use it when vocabulary should drive event matching, analytics routing, UI state transitions, or reactive workflows.
import SwiftLexicon
let events = Events()
let subscription = commerce.ux.type.action >> events.then { event in
print(event.description)
}
commerce.ui.product.card.buy >> events
subscription.cancel()
events.finish()Generated lemmas can carry keyed values:
commerce.ui.product.card.buy["birthday_001"] >> eventsThe payload is JSON-backed. Decode it through JSONDecoder:
let subscription = commerce.ui.product.card.buy >> events.then { event in
let value: String = try event[type: String.self]
print(value)
}If a generated lemma is typed as commerce.ux.type.action, you can subscribe to the type:
let subscription = commerce.ux.type.action >> events.then { event in
print("action:", event.description)
}This lets different UI events share handling through vocabulary types instead of switch statements over strings.
Events expose a sendable snapshot:
let snapshot = event.snapshot
print(snapshot.id)
print(snapshot.description)
print(snapshot.lemma)
print(snapshot.values)Use snapshots at concurrency boundaries or when storing an event log.
- Define product actions and state transitions in
.lexicon. - Generate Swift with
--type swift. - Use generated lemmas when sending events.
- Subscribe by concrete lemma for narrow behavior.
- Subscribe by generated type for broad behavior.
- Persist exact IDs and JSON values, not generated class names.
The generated source is the compile-time surface. The dot-path ID is the stable runtime contract.