nupp.host.pump

nupp.host.pump runs one operation at a time for an application that embeds the runtime and owns its own loop. Reach for it from the exports such an application calls, so that an operation which waits -- on a nupp.host request, a timer, the network -- parks instead of blocking the call that started it.

start runs an operation until it finishes or parks, and says which; the application keeps calling poll from its loop, after answering whatever it was asked, until the operation finishes. Each poll is one non-blocking pass over the runtime's readiness sources, and an operation they make ready resumes inside that pass.

local pump = nupp.host.pump

local session = pump.newPump()

export function frame(dt: number): pump.State
    if session:parked() then
        return session:poll()
    end
    return session:start(function(): boolean
        return update(dt)
    end)
end

An operation returns whether the application should keep running: false reports "stopped", anything else "continue". A failure is raised from the start or poll that observes it. cancel unwinds a parked operation, raising a cancellation inside it, so its cleanup runs before the application shuts down.

Module contents

Types

TypeKindDescription
PumprecordRuns one parked operation at a time for a host-owned loop.
StatetypeWhat a start or poll left the operation doing.

Functions

FunctionKindDescription
newPumpfunctionCreates an idle pump.

Types#

Pumprecord#

record Pump ...

Runs one parked operation at a time for a host-owned loop.

Members

NameKindDescription
handlerfield
activefield
resultfield
failurefield
startmethodRuns body until it finishes or parks.
pollmethodMakes one non-blocking pass over the readiness sources, resuming the parked operation if they make it ready.
parkedmethodWhether an operation is parked between the host's turns.
cancelmethodUnwinds a parked operation, raising a cancellation inside it.

handlerfield#

handler: suspensionContract.Handler
@private

activefield#

active: thread?
@private

resultfield#

result: boolean?
@private

failurefield#

failure: any?
@private

startmethod#

start: function start(self: Pump, body: function(): boolean?): State

Runs body until it finishes or parks.

Arguments
NameTypeDescription
selfPump
bodyfunction(): boolean?
Returns
TypeDescription
State
Raises
TypeCondition
string

when another operation is still parked, or what body raised

pollmethod#

poll: function poll(self: Pump): State

Makes one non-blocking pass over the readiness sources, resuming the parked operation if they make it ready.

Arguments
NameTypeDescription
selfPump
Returns
TypeDescription
State
Raises
TypeCondition
string

when nothing is parked, or what the operation raised

parkedmethod#

parked: function parked(self: Pump): boolean

Whether an operation is parked between the host's turns.

Arguments
NameTypeDescription
selfPump
Returns
TypeDescription
boolean

cancelmethod#

cancel: function cancel(self: Pump): nil

Unwinds a parked operation, raising a cancellation inside it.

Arguments
NameTypeDescription
selfPump
Returns
TypeDescription
nil
Raises
TypeCondition
string

when the operation does not unwind

Statetype#

type State = "parked" | "continue" | "stopped"

What a start or poll left the operation doing.

Functions#

newPumpfunction#

function newPump(): Pump

Creates an idle pump.

Returns

TypeDescription
Pump