Watchers and modules vs resource efficiency #3437
Replies: 2 comments 18 replies
-
Yep, this is one of the most significant architectural shortcomings in Hammerspoon, and frankly is one of the big reasons why I've been reluctant to just call a 1.0 release. What I would like to do is rewrite all of the modules that have watchers to use http://www.hammerspoon.org/docs/hs.watchable.html in such a way that the watchers are started and stopped automatically depending on whether there are any subscribers. I don't know if we'll ever actually get to that though, and if we did, even though we're <1.0 I would expect to get a lot of whinging about broken configs! |
Beta Was this translation helpful? Give feedback.
-
See #3440 As an example of its use in the console: edit: stupid it's... -- for this example, note that I have a function `finspect` that takes an arbitrary number
-- of arguments and runs `hs.inspect({ ... }, { newline = " ", indent = "" })` on the list
> obj = hs.watchable.new("test")
w1 = hs.watchable.watch("test.key", function(...) print("w1", finspect(...)) end)
w2 = hs.watchable.watch("test.key", function(...) print("w2", finspect(...)) end)
-- Loading extension: watchable
> obj.key = "hello"
w1 { { -- hs.watchable.watcher for path test.key _active = true, _alwaysNotify = false, _callback = <function 1>, _objKey = "key", _objPath = "test", _path = "test.key" }, "test", "key", nil, "hello" }
w2 { { -- hs.watchable.watcher for path test.key _active = true, _alwaysNotify = false, _callback = <function 1>, _objKey = "key", _objPath = "test", _path = "test.key" }, "test", "key", nil, "hello" }
> obj.key = "hello"
> w1:alwaysNotify(true)
hs.watchable.watcher for path test.key
> obj.key = "hello"
w1 { { -- hs.watchable.watcher for path test.key _active = true, _alwaysNotify = true, _callback = <function 1>, _objKey = "key", _objPath = "test", _path = "test.key" }, "test", "key", "hello", "hello" }
> obj.key = "hello"
w1 { { -- hs.watchable.watcher for path test.key _active = true, _alwaysNotify = true, _callback = <function 1>, _objKey = "key", _objPath = "test", _path = "test.key" }, "test", "key", "hello", "hello" }
> obj.key = "goober"
w1 { { -- hs.watchable.watcher for path test.key _active = true, _alwaysNotify = true, _callback = <function 1>, _objKey = "key", _objPath = "test", _path = "test.key" }, "test", "key", "hello", "goober" }
w2 { { -- hs.watchable.watcher for path test.key _active = true, _alwaysNotify = false, _callback = <function 1>, _objKey = "key", _objPath = "test", _path = "test.key" }, "test", "key", "hello", "goober" }
|
Beta Was this translation helpful? Give feedback.
-
I've recently moved toward putting things into separate modules. This is helpful for readability and keeping things separate, but the fun ends when the need arise for watchers that are shared between several modules.
The natural thing to do is adding a seperate watcher object to every module, but will I use up extra resources this way rather than having a single watcher object and sending signals to modules (or however I would resolve it)?
Beta Was this translation helpful? Give feedback.
All reactions