An event is a name with listeners : functions which run when the event is emitted, each in the language it was written in. Your code emits it, a client emits it through the system API, or API Maker emits it on its own after an API you attach it to. Events keep the side effects of an operation (mails, stock, notifications, exports) out of the API which triggers them.
import*asTfrom'types';import*asdbfrom'db-interfaces';asyncfunctionmain(g:T.IAMGlobal){constdata=g.req.eventData;// what was emittedawaitg.sys.db.saveSingleOrMultiple({instance:'mongodb',database:'shop',collection:'notifications',saveData:{order_no:data.order_no,text:`Order ${data.order_no} placed`},});return{notified:true};// returned to the emitter}module.exports=main;
fromamimportGasyncdefmain(g:G):data=g.req.event_data# what was emittedawaitg.sys.db.save_single_or_multiple('mongodb','shop','notifications',save_data={'order_no':data.order_no,'text':f'Order {data.order_no} placed'},)return{'notified':True}# returned to the emitter
<?phpfunctionmain(AmG$g){$data=$g->req->eventData;// what was emitted$g->sys->db->saveSingleOrMultiple('mongodb','shop','notifications',saveData:['order_no'=>$data->order_no,'text'=>"Order {$data->order_no} placed"],);return['notified'=>true];// returned to the emitter}
filestaticclassApi{publicstaticasyncTask<object?>Main(Gg){varorderNo=g.Req.EventData?["order_no"]?.GetValue<long>();// what was emittedawaitg.Sys.Db.SaveSingleOrMultiple(newSave("mongodb","shop","notifications",new{order_no=orderNo,text=$"Order {orderNo} placed"}));returnnew{notified=true};// returned to the emitter}}
packagecodeimport("fmt""apimaker/am")funcMain(g*am.G)(any,error){data:=am.AsM(g.Req.EventData())// what was emitted_,err:=g.Sys.Db.SaveSingleOrMultiple(am.Save{Instance:"mongodb",Database:"shop",Collection:"notifications",SaveData:am.M{"order_no":data.Get("order_no"),"text":fmt.Sprintf("Order %v placed",data.Get("order_no"))},})iferr!=nil{returnnil,err}returnam.M{"notified":true},nil// returned to the emitter}
useam::{Errors,G,Save,Value,json};pubfnmain(g:&mutG)->Result<Value,Errors>{letorder_no=g.req.event_data()["order_no"].clone();// what was emittedg.sys.db.save_single_or_multiple(Save::new("mongodb","shop","notifications",json!({"order_no":order_no,"text":format!("Order {order_no} placed")}),))?;Ok(json!({"notified":true}))// returned to the emitter}
importam.G;importam.JsonObject;importam.Save;importjava.util.Map;classApi{staticObjectmain(Gg){JsonObjectdata=JsonObject.from(g.req.getEventData());// what was emittedg.sys.db.saveSingleOrMultiple(newSave("mongodb","shop","notifications",Map.of("order_no",data.getLong("order_no"),"text","Order "+data.getLong("order_no")+" placed")));returnMap.of("notified",true);// returned to the emitter}}
importam.Gimportam.JsonObjectimportam.Savefunmain(g:G):Any?{valdata=JsonObject.from(g.req.eventData)// what was emittedg.sys.db.saveSingleOrMultiple(Save("mongodb","shop","notifications",mapOf("order_no"todata.getLong("order_no"),"text"to"Order ${data.getLong("order_no")} placed")),)returnmapOf("notified"totrue)// returned to the emitter}
Every listener gets the same g as a custom API : g.req.eventData holds the emitted data, g.sys reaches every API, g.logger writes to the log table.
Listeners run one after the other, each within its timeout : in the sandbox and with runOnNativeProcess alike. Empty : sandboxReqTimeout of the configuration (13 seconds), whatever time the request which emitted the event has.
For an automatic trigger, g.req.eventData is the whole answer of the API which triggered it ({ success, statusCode, data, … }), so a listener sees what was saved or read.
A listener may emit another event, which may emit another. API Maker keeps the list of the events already running for the request : an emit which comes back to one of them is stopped, so a chain never becomes an infinite loop. Each level of depth is allowed ; only the cycle is refused.
Events and their listeners are files in Git and deploy with a pull. Listeners have versions.
Test a listener from the API testing page : pick the event, the listener, put the event data in the body and send.
On the Events page, Move up and Move down order the listeners, Grid View lists every listener of every event, Clone copies an event with its listeners and Copy full event name gives the name to emit.
On the native process, console.log is not captured : use g.logger.