summaryrefslogtreecommitdiff
path: root/core.lua
diff options
context:
space:
mode:
authorSam Nystrom <sam@samnystrom.dev>2025-11-27 17:35:48 -0500
committerSam Nystrom <sam@samnystrom.dev>2025-11-27 17:35:48 -0500
commit747b8a465a501211ebe41d5892eb9262f26743dc (patch)
tree170ef89197a6648a378cec1ce45201442c3e163e /core.lua
Squashed '.config/waywall/waywork/' content from commit f0b8424
git-subtree-dir: .config/waywall/waywork git-subtree-split: f0b84240c63f4a13d6a8cb54a037424d5d64329a
Diffstat (limited to 'core.lua')
-rw-r--r--core.lua61
1 files changed, 61 insertions, 0 deletions
diff --git a/core.lua b/core.lua
new file mode 100644
index 0000000..f8cd76b
--- /dev/null
+++ b/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