Lua library to describe concurrent blocks of code inspired by the Céu language (work in progress).
Refer to the comments in tasks.lua for more (and possibly more up to date) info.
task_tobjects hold code that will run in concurrently (in a coroutine).- Events allow tasks to send messages asynchronously or block waiting for them.
- Tasks can use the
await(<event_id>)function to block waiting for the<event_id>event.awaitreturns parameters sent toemit(minus the<event_id>). - The
emit(<event_id>, ...)function sends an event, unblocking all tasks waiting onawait(<event_id>).- If no tasks are currently blocked waiting for the event, it's discarded.
<event_id>can be any value that can be used as key in a table.
- Tasks can use the
- Tasks can have subtasks that are killed when the outer task finishes or is killed.
par_andandpar_orfunctions return a task that start subtasks concurrently.par_andends when all subtasks end.par_orends when any subtask end, killing the others.
listen(<event_id>, callback, [once])adds acallbackfunction as a listener of the corresponding event.- The
callbackwill be executed onemit(<event_id>, ...). - The parameters sent to the
emitwill be sent to thecallback, including the event object corresponding to the<event_id>identifier (unlike the return ofawait). - If
onceistrue, the callback will be executed only on the next time the event occurs, otherwise it will execute every time.
- The
stop_listening(<event_id>, callback)removes thecallbackfrom the listeners.future_tobjects represent a future event return value.future_t:new(<event_id>, [cancel_cb])creates a future object for an event.:get()returns the<event_id>result if avaliable (i.e.emit(<event_id>, ...)was executed already) or behaves asawait(<event_id>)otherwise.:cancel()stops waiting for the event and executes thecancel_cb. Unblocks all:get()s, returningnil. All:get()calls from now on returnnilimmediately.:is_canceled()returns true if the future was cancelled.:is_done()returns true if the<event_id>was emitted. If the future is done,:get()returns immediately.
timer_tallows callbacks to be executed based on time passage.timer_t:new(interval, callback, [cyclic])creates a timer that will execute thecallbackafterintervalmilliseconds.- If
cyclicistrue, the callback will be executed everyintervalinterval (instead of only once).
- If
:start()schedules the timer: Starts counting the interval.:stop()stops the timer.
now_ms()retuns the current timestamp in milliseconds (this is an internal counter in this lib, not the OS time)in_ms(interval, callback)executescallbackonce inintervalmilliseconds from now. Returns the timer object controlling this.every_ms(interval, callback)executescallbackeveryintervalmilliseconds, starting now. Returns the timer object controlling this.await_ms(interval)blocks the current task forintervalmilliseconds.update_time(dt)tells the lib thatdtmilliseconds have passed. This will unblock tasks and execute timer callbacks if needed.