diff options
| author | Sam Nystrom <sam@samnystrom.dev> | 2025-11-27 17:35:48 -0500 |
|---|---|---|
| committer | Sam Nystrom <sam@samnystrom.dev> | 2025-11-27 17:35:48 -0500 |
| commit | e521805e7f3f52214056720e3a280c9e9f2ec6e9 (patch) | |
| tree | 22e01c53c43b3fda9b7c7f214fc35f29536cc135 /.config/waywall/waywork/core.lua | |
| parent | 77b295b8b0f9b73ecbda809e7843812c5f4c3737 (diff) | |
| parent | 747b8a465a501211ebe41d5892eb9262f26743dc (diff) | |
Merge commit '747b8a465a501211ebe41d5892eb9262f26743dc' as '.config/waywall/waywork'
Diffstat (limited to '.config/waywall/waywork/core.lua')
| -rw-r--r-- | .config/waywall/waywork/core.lua | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/.config/waywall/waywork/core.lua b/.config/waywall/waywork/core.lua new file mode 100644 index 0000000..f8cd76b --- /dev/null +++ b/.config/waywall/waywork/core.lua @@ -0,0 +1,61 @@ +local core = {} + +--- Create a boolean toggle with on/off callbacks. +function core.toggle(on, off) + local state = false + return { + set = function(v) + if v == state then + return state + end + state = not not v + if state then + on() + else + off() + end + return state + end, + get = function() + return state + end, + toggle = function(self) + return self.set(not state) + end, + } +end + +--- Create a resettable timeout using blocking sleep (matches waywall execution model). +--- Calls `f()` only if this invocation is the last one. +function core.resettable_timeout(sleep, f) + local gen = 0 + return function(delay_ms) + gen = gen + 1 + local my = gen + sleep(delay_ms) + if my == gen then + f() + end + end +end + +--- Shallow table copy +function core.copy(t) + local r = {} + for k, v in pairs(t) do + r[k] = v + end + return r +end + +--- Merge (dst gets missing fields from src) +function core.merge(dst, src) + for k, v in pairs(src or {}) do + if dst[k] == nil then + dst[k] = v + end + end + return dst +end + +return core |
