Skip to content

Class Util

Utility functions.

Fields

rectangle

rectangle = rectangle

Functions

function batch

function Util.batch(requests: fun(): T[])
    -> responses: T[]

Batch a set of requests that will be sent to the compositor all at once.

Normally, all API calls are blocking. For example, calling Window.get_all then calling WindowHandle.props on each returned window handle will block after each props call waiting for the compositor to respond:

local handles = Window.get_all()

 -- Collect all the props into this table
local props = {}

 -- This for loop will block after each call. If the compositor is running slowly
 -- for whatever reason, this will take a long time to complete as it requests
 -- properties sequentially.
for i, handle in ipairs(handles) do
    props[i] = handle:props()
end

In order to mitigate this issue, you can batch up a set of API calls using this function. This will send all requests to the compositor at once without blocking, then wait for the compositor to respond.

You must wrap each request in a function, otherwise they would just get evaluated at the callsite in a blocking manner.

Example

lua
local handles = window.get_all()

 ---@type (fun(): WindowProperties)[]
local requests = {}

 -- Wrap each request to `props` in another function
for i, handle in ipairs(handles) do
    requests[i] = function()
        return handle:props()
    end
end

 -- Batch send these requests
local props = require("pinnacle.util").batch(requests)
 -- `props` now contains the `WindowProperties` of all the windows above

Parameters

requests: fun(): T[] - The requests that you want to batch up, wrapped in a function.

Returns

  1. responses: T[] - The results of each request in the same order that they were in requests.

function deep_copy

function Util.deep_copy(obj: T)
    -> deep_copy: T

Creates a deep copy of an object.

Parameters

obj: T - The object to deep copy.

Returns

  1. deep_copy: T - A deep copy of obj

function bijective_table

function Util.bijective_table(key_value_pairs: T)
    -> bijective_table: T

Creates a table with entries key->value and value->key for all given pairs.

Parameters

key_value_pairs: T

Returns

  1. bijective_table: T - A table with pairs key->value and value->key

function make_bijective

function Util.make_bijective(table: table)

Makes a table bijective by inserting value = key entries for every key-value pair.

Parameters

table: table