diff options
Diffstat (limited to 'lang/lua')
-rw-r--r-- | lang/lua/astal/process.lua | 31 | ||||
-rw-r--r-- | lang/lua/astal/variable.lua | 71 |
2 files changed, 49 insertions, 53 deletions
diff --git a/lang/lua/astal/process.lua b/lang/lua/astal/process.lua index 800e83a..0031f4c 100644 --- a/lang/lua/astal/process.lua +++ b/lang/lua/astal/process.lua @@ -10,30 +10,29 @@ M.Process = Astal.Process ---@param on_stderr? fun(err: string): nil ---@return { kill: function } | nil proc function M.subprocess(commandline, on_stdout, on_stderr) - if on_stdout == nil then - on_stdout = function(out) - io.stdout:write(tostring(out) .. "\n") - end + on_stdout = on_stdout or function(out) + io.stdout:write(tostring(out) .. "\n") end - if on_stderr == nil then - on_stderr = function(err) - io.stderr:write(tostring(err) .. "\n") - end + on_stderr = on_stderr or function(err) + io.stderr:write(tostring(err) .. "\n") end + local proc, err + if type(commandline) == "table" then proc, err = Astal.Process.subprocessv(commandline) else proc, err = Astal.Process.subprocess(commandline) end + if err ~= nil then err(err) return nil end - proc.on_stdout = function(_, stdoud) - on_stdout(stdoud) + proc.on_stdout = function(_, stdout) + on_stdout(stdout) end proc.on_stderr = function(_, stderr) on_stderr(stderr) @@ -54,13 +53,11 @@ end ---@param commandline string | string[] ---@param callback? fun(out: string, err: string): nil function M.exec_async(commandline, callback) - if callback == nil then - callback = function(out, err) - if err ~= nil then - io.stdout:write(tostring(out) .. "\n") - else - io.stderr:write(tostring(err) .. "\n") - end + callback = callback or function(out, err) + if err ~= nil then + io.stdout:write(tostring(out) .. "\n") + else + io.stderr:write(tostring(err) .. "\n") end end diff --git a/lang/lua/astal/variable.lua b/lang/lua/astal/variable.lua index f9be161..2305a71 100644 --- a/lang/lua/astal/variable.lua +++ b/lang/lua/astal/variable.lua @@ -20,7 +20,7 @@ local Process = require("astal.process") local Variable = {} Variable.__index = Variable ----@param value any +---@param value? any ---@return Variable function Variable.new(value) local v = Astal.VariableBase() @@ -30,7 +30,7 @@ function Variable.new(value) }, Variable) v.on_dropped = function() variable:stop_watch() - variable:stop_watch() + variable:stop_poll() end v.on_error = function(_, err) if variable.err_handler then @@ -40,16 +40,14 @@ function Variable.new(value) return variable end ----@param transform function +---@param transform? fun(v: any): any ---@return Binding function Variable:__call(transform) - if transform == nil then - transform = function(v) - return v - end + if type(transform) == "nil" then return Binding.new(self) + else + return Binding.new(self):as(transform) end - return Binding.new(self):as(transform) end function Variable:__tostring() @@ -57,7 +55,7 @@ function Variable:__tostring() end function Variable:get() - return self._value or nil + return self._value end function Variable:set(value) @@ -67,8 +65,16 @@ function Variable:set(value) end end +function Variable:is_polling() + return self._poll ~= nil +end + +function Variable:is_watching() + return self._watch ~= nil +end + function Variable:start_poll() - if self._poll ~= nil then + if self:is_polling() then return end @@ -81,15 +87,16 @@ function Variable:start_poll() Process.exec_async(self.poll_exec, function(out, err) if err ~= nil then return self.variable.emit_error(err) + else + self:set(self.poll_transform(out, self:get())) end - self:set(self.poll_transform(out, self:get())) end) end) end end function Variable:start_watch() - if self._watch then + if self:is_watching() then return end @@ -100,27 +107,21 @@ function Variable:start_watch() end) end + function Variable:stop_poll() - if self._poll then + if self:is_polling() then self._poll.cancel() end self._poll = nil end function Variable:stop_watch() - if self._watch then + if self:is_watching() then self._watch.kill() end self._watch = nil end -function Variable:is_polling() - return self._poll ~= nil -end - -function Variable:is_watching() - return self._watch ~= nil -end function Variable:drop() self.variable.emit_dropped() @@ -137,7 +138,7 @@ end ---@return Variable function Variable:on_error(callback) self.err_handler = nil - self.variable.on_eror = function(_, err) + self.variable.on_error = function(_, err) callback(err) end return self @@ -158,11 +159,10 @@ end ---@param exec string | string[] | function ---@param transform? fun(next: any, prev: any): any function Variable:poll(interval, exec, transform) - if transform == nil then - transform = function(next) - return next - end + transform = transform or function(next) + return next end + self:stop_poll() self.poll_interval = interval self.poll_transform = transform @@ -181,12 +181,11 @@ end ---@param exec string | string[] ---@param transform? fun(next: any, prev: any): any function Variable:watch(exec, transform) - if transform == nil then - transform = function(next) - return next - end + transform = transform or function (next) + return next end - self:stop_poll() + + self:stop_watch() self.watch_exec = exec self.watch_transform = transform self:start_watch() @@ -208,6 +207,7 @@ function Variable:observe(object, sigOrFn, callback) return self:get() end end + local set = function(...) self:set(f(...)) end @@ -226,10 +226,8 @@ end ---@param transform? fun(...): any ---@return Variable function Variable.derive(deps, transform) - if type(transform) == "nil" then - transform = function(...) - return { ... } - end + transform = transform or function(...) + return { ... } end if getmetatable(deps) == Variable then @@ -246,7 +244,7 @@ function Variable.derive(deps, transform) end end - local update = function() + local function update() local params = {} for i, binding in ipairs(deps) do params[i] = binding:get() @@ -257,6 +255,7 @@ function Variable.derive(deps, transform) local var = Variable.new(update()) local unsubs = {} + for i, b in ipairs(deps) do unsubs[i] = b:subscribe(update) end |