β° Scheduled tasks API¶
Something to do a little later? Or work finished on a thread of your own that now needs the API on the server thread? Use a scheduled task.
Scheduling¶
On the server thread, as soon as possible¶
Rust: ctx.host().schedule(task)
Go: levilamina.Schedule(task)
Zig: levilamina.schedule(task)
- Parameters:
- task : function
the work, run on the server thread
- task : function
- Return value: the task id, to cancel it later
- Return type: Rust
Result<TaskId>, Go(TaskID, error), Ziglevilamina.Error!TaskId- Callable from any thread: this is how work gets back from a background thread to the server thread.
- Slot:
schedule_for -
Example:
- Rust
- Go
Go - Zig
After a while¶
Rust: ctx.host().schedule_after(delay, task)
Go: levilamina.ScheduleAfter(delay, task)
Zig: levilamina.scheduleAfter(delay_ms, task)
- Parameters:
- delay : duration
how long to wait: RustDuration, Gotime.Duration, Zig milliseconds - task : function
the work
- delay : duration
- Return value: the task id
- Return type: Rust
Result<TaskId>, Go(TaskID, error), Ziglevilamina.Error!TaskId - Slot:
schedule_after_for -
Example:
- Rust
Rust - Go
Go - Zig
Zig
A task belongs to your mod
When your mod is unloaded before a task runs, Pier takes the task off the queue. When its time comes, nothing is called.
Managing tasks¶
Cancelling a task¶
Rust: ctx.host().cancel(id)
Go: levilamina.Cancel(id)
Zig: levilamina.cancel(id)
- Parameters:
- id : task id
the id scheduling returned
- id : task id
- Return value: whether it was cancelled. A task that already ran, was already cancelled, or is not your mod's returns
false - Return type: Rust
bool, Go(bool, error), Ziglevilamina.Error!bool - Slot:
schedule_cancel -
Example:
- Rust
Rust - Go
Go - Zig
Zig
How many tasks are waiting¶
Rust: ctx.host().try_pending_tasks()
Go: levilamina.PendingTasks()
Zig: levilamina.pendingTasks()
- Return value: how many of your mod's tasks have not run
- Return type: Rust
Result<u32>, Go(uint32, error), Ziglevilamina.Error!u32- A host that cannot count returns an error. A check for 0 before unloading receives that error, not a 0 that lets the check pass.
- Slot:
schedule_pending_count -
Example:
- Rust
Rust - Go
Go - Zig
Zig