Skip to main content

core.transactions

local transactions = require "core.transactions"

core.transactions.context​

The runner passes a fresh context to each handler. Plugins do not construct it.

__index​

(field) __index: core.transactions.context

The runner passes a fresh context to each handler. Plugins do not construct it.


detail​

(field) detail: string?

fraction​

(field) fraction: number?

last_message​

(field) last_message: string?

log​

(field) log: fun(message: string)

phase​

(field) phase: "startup"

Phase in which this handler is running.


previous_failure​

(field) previous_failure: (core.transactions.failure)?

Saved reason from the previous attempt, if any.


progress​

(method) core.transactions.context:progress(message: string, fraction?: number, detail?: string)

Report an activity and yield so the runner can redraw. Repeating the same message updates the progress indicator without adding another log entry.

@param fraction?: number — Fraction complete (0 to 1). Omit if the total is unknown.

@param detail?: string — Transient status, such as the number of bytes downloaded.


yield​

(method) core.transactions.context:yield()

Give the runner a chance to redraw without changing the current progress.


core.transactions.failure​

Second return value from a transaction handler when its first return value is nil.

message​

(field) message: string

Explanation shown to the user.


recovery_required​

(field) recovery_required: boolean?

Leave the request in progress for manual recovery, even if retry is set.


retry​

(field) retry: boolean?

Try again on the next full launch instead of marking the request failed.


core.transactions.handler​

Return true on success, or nil, failure on failure. Return nil, \{ message = "...", retry = true \} to retry on the next full startup.

core.transactions.key​

id​

(field) id: string?

Optional qualifier for multiple requests for the same script.


phase​

(field) phase: "startup"?

Defaults to "startup".


core.transactions.options​

id​

(field) id: string?

Optional qualifier for multiple requests for the same script.


payload​

(field) payload: (boolean|string|number|table)?

Plain serialized data.


phase​

(field) phase: "startup"?

Defaults to "startup".


title​

(field) title: string?

core.transactions.payload​

core.transactions.phase​

core.transactions.phase:
| "startup" -- Before editor initialization on a full launch; skipped on soft restart.

core.transactions.record​

id​

(field) id: string?

last_failure​

(field) last_failure: (core.transactions.failure)?

Second return value from a transaction handler when its first return value is nil.


payload​

(field) payload: (boolean|string|number|table)?

phase​

(field) phase: "startup"

script​

(field) script: string

state​

(field) state: "failed"|"in_progress"|"ready"

title​

(field) title: string

get​

function core.transactions.get(script: string, key?: core.transactions.key)
-> record: (core.transactions.record)?
2. err: string?

Return a detached snapshot, including a running or failed request.

@param key?: core.transactions.key

@return record: (core.transactions.record)?

@return err: string?


register​

function core.transactions.register(script: string, options?: core.transactions.options)
-> ok: boolean?
2. err: string?

Queue an installed handler. Does not overwrite an existing request. At execution time, plugins.my_plugin.apply resolves to plugins/my_plugin/apply.lua under USERDIR first, then DATADIR. The file must return a function:

---@type core.transactions.handler
local function apply(id, payload, ctx)
if system.get_file_info(payload.busy_marker) then
return nil, \{ message = "Close the app using the file, then relaunch Pragtical.", retry = true \}
end
ctx:progress("Moving staged file")
local ok, err = os.rename(payload.staged, payload.destination)
if not ok then return nil, \{ message = err \} end
return true
end
return apply

id is options.id or the dotted script name; payload is options.payload or nil. See core.transactions.context for progress reporting and retry information.

To retry, the handler must return two values: nil (not false) and a failure table with a message and retry = true. Do not register it again. The runner leaves this request and later requests queued. On the next full launch it calls the handler from the beginning with the same payload and the failure in ctx.previous_failure. Only request a retry if replay is safe. Without retry, the failure is marked failed; true completes the request. Use recovery_required = true when manual recovery is needed; it overrides retry.

@param script: string — Dotted handler path starting with plugins., without the .lua extension.

@param options?: core.transactions.options

@return ok: boolean?

@return err: string?


remove​

function core.transactions.remove(script: string, key?: core.transactions.key)
-> ok: boolean?
2. err: string?

Remove a pending or failed request, but never one claimed by a runner.

@param key?: core.transactions.key

@return ok: boolean?

@return err: string?


run​

function core.transactions.run(phase?: "startup")
-> ok: boolean?
2. err: string?

Run pending handlers before editor initialization. A handler may return nil, { message = "...", retry = true } when it is safe to start the editor and retry on the next launch. This also leaves later requests queued. An interrupted claim or recovery_required failure is never silently replayed.

phase:
| "startup" -- Before editor initialization on a full launch; skipped on soft restart.

@return ok: boolean?

@return err: string?