1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
local lgi = require("lgi")
local Astal = lgi.require("Astal", "0.1")
local GObject = lgi.require("GObject", "2.0")
local Binding = require("astal.binding")
local Time = require("astal.time")
local Process = require("astal.process")
---@class Variable
---@field private variable table
---@field private err_handler? function
---@field private _value any
---@field private _poll? table
---@field private _watch? table
---@field private poll_interval number
---@field private poll_exec? string[] | string
---@field private poll_transform? fun(next: any, prev: any): any
---@field private poll_fn? function
---@field private watch_transform? fun(next: any, prev: any): any
---@field private watch_exec? string[] | string
local Variable = {}
Variable.__index = Variable
---@param value any
---@return Variable
function Variable.new(value)
local v = Astal.VariableBase()
local variable = setmetatable({
variable = v,
_value = value,
}, Variable)
v.on_dropped = function()
variable:stop_watch()
variable:stop_watch()
end
v.on_error = function(_, err)
if variable.err_handler then
variable.err_handler(err)
end
end
return variable
end
---@param transform function
---@return Binding
function Variable:__call(transform)
if transform == nil then
transform = function(v)
return v
end
return Binding.new(self)
end
return Binding.new(self):as(transform)
end
function Variable:__tostring()
return "Variable<" .. tostring(self:get()) .. ">"
end
function Variable:get()
return self._value or nil
end
function Variable:set(value)
if value ~= self:get() then
self._value = value
self.variable:emit_changed()
end
end
function Variable:start_poll()
if self._poll ~= nil then
return
end
if self.poll_fn then
self._poll = Time.interval(self.poll_interval, function()
self:set(self.poll_fn(self:get()))
end)
elseif self.poll_exec then
self._poll = Time.interval(self.poll_interval, function()
Process.exec_async(self.poll_exec, function(out, err)
if err ~= nil then
return self.variable.emit_error(err)
end
self:set(self.poll_transform(out, self:get()))
end)
end)
end
end
function Variable:start_watch()
if self._watch then
return
end
self._watch = Process.subprocess(self.watch_exec, function(out)
self:set(self.watch_transform(out, self:get()))
end, function(err)
self.variable.emit_error(err)
end)
end
function Variable:stop_poll()
if self._poll then
self._poll.cancel()
end
self._poll = nil
end
function Variable:stop_watch()
if self._watch 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()
end
---@param callback function
---@return Variable
function Variable:on_dropped(callback)
self.variable.on_dropped = callback
return self
end
---@param callback function
---@return Variable
function Variable:on_error(callback)
self.err_handler = nil
self.variable.on_eror = function(_, err)
callback(err)
end
return self
end
---@param callback fun(value: any)
---@return function
function Variable:subscribe(callback)
local id = self.variable.on_changed:connect(function()
callback(self:get())
end)
return function()
GObject.signal_handler_disconnect(self.variable, id)
end
end
---@param interval number
---@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
end
self:stop_poll()
self.poll_interval = interval
self.poll_transform = transform
if type(exec) == "function" then
self.poll_fn = exec
self.poll_exec = nil
else
self.poll_exec = exec
self.poll_fn = nil
end
self:start_poll()
return self
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
end
self:stop_poll()
self.watch_exec = exec
self.watch_transform = transform
self:start_watch()
return self
end
---@param object table | table[]
---@param sigOrFn string | fun(...): any
---@param callback fun(...): any
---@return Variable
function Variable:observe(object, sigOrFn, callback)
local f
if type(sigOrFn) == "function" then
f = sigOrFn
elseif type(callback) == "function" then
f = callback
else
f = function()
return self:get()
end
end
local set = function(...)
self:set(f(...))
end
if type(sigOrFn) == "string" then
object["on_" .. sigOrFn]:connect(set)
else
for _, obj in ipairs(object) do
obj[1]["on_" .. obj[2]]:connect(set)
end
end
return self
end
---@param deps Variable | (Binding | Variable)[]
---@param transform? fun(...): any
---@return Variable
function Variable.derive(deps, transform)
if type(transform) == "nil" then
transform = function(...)
return { ... }
end
end
if getmetatable(deps) == Variable then
local var = Variable.new(transform(deps:get()))
deps:subscribe(function(v)
var:set(transform(v))
end)
return var
end
for i, var in ipairs(deps) do
if getmetatable(var) == Variable then
deps[i] = Binding.new(var)
end
end
local update = function()
local params = {}
for i, binding in ipairs(deps) do
params[i] = binding:get()
end
return transform(table.unpack(params), 1, #deps)
end
local var = Variable.new(update())
local unsubs = {}
for i, b in ipairs(deps) do
unsubs[i] = b:subscribe(update)
end
var.variable.on_dropped = function()
for _, unsub in ipairs(unsubs) do
unsub()
end
end
return var
end
return setmetatable(Variable, {
__call = function(_, v)
return Variable.new(v)
end,
})
|