emit. An event contract names the event type it consumes, and its host runs it with each event of that type. Events need requestscript-js 1.6.0 or later.
The interpreter checks events and hands them to its host, but it doesn’t deliver them. Which scripts may emit an event, and which event contracts run, is up to the host. A Stellarium node, for example, delivers events across its network.
Declaring an event type
An event declaration is a script of its own. It lists the fields its events carry, and has no body:- The path, name and version work as they do for contracts. The version defaults to
1. - Fields are
name: typepairs, like contract parameters, with any type. Field names must be unique. - An event type without fields leaves out the parentheses:
event shop.Tick. - An event declaration can’t have a body or a return type. Either one is a parse error.
restricted to mark the event type restricted:
restricted is a flag for the host. Hosts deliver a restricted event only to event contracts deployed by the event type’s owner.
Emitting an event
A request or a contract publishes an event withemit, followed by the event type and a value for every field:
- Name the event type as
path.Name, orpath.Name@versionto pin a version. Without a version, the host picks one. - Pass every field by name, in any order. A missing field, or a field the event type doesn’t declare, is a runtime error.
- Each value is converted to its field’s type, as an argument to a resource function is. A value that doesn’t fit is a runtime error.
emitcan appear anywhere a statement can, including insideifbranches. Its tokens are counted like any statement’s.
Consuming events
An event contract declares the event type it consumes withconsumes after its name:
- An event contract declares no parameters. The event’s fields are its parameters, readable by name, like
constparameters. - It has no return type and can’t return a value. A bare
returnends it, andreturnwith a value is a parse error. consumesnames the event type aspath.Nameorpath.Name@version.- An event contract can keep state variables and call resources like any contract.
Reserved words
event, emit, consumes and restricted aren’t reserved words. They act as keywords only where they’re followed by a name, so you can still use them as variable names:
Hosting events
A host passes anevents resolver to interpret or interpretParsed. The interpreter calls it to look up the event type of each emit, and the type an event contract consumes. The run’s events come back in result.events:
dataholds each field’s value as JSON, encoded as in JSON encoding.- Without a resolver,
emitand running an event contract are runtime errors. - A resolver can refuse an event by throwing a
RuntimeError, which fails the script with its message. Hosts use this to decide who may emit what. emittedEvents(script)lists every event type a script’semitstatements name, including those in branches that may not run, so a host can check a script before it accepts it.
Running an event declaration is a runtime error: it declares a type and has nothing to run. The requestscript-js HTTP server has no event resolver, so scripts that emit or consume events fail there.