Coder Social home page Coder Social logo

Comments (3)

jamesb93 avatar jamesb93 commented on June 10, 2024 1

Thanks for this – it makes sense to me. I'll assign them to some variables.

from hammerspoon.

jamesb93 avatar jamesb93 commented on June 10, 2024 1

It worked. Thank you.

from hammerspoon.

Rhys-T avatar Rhys-T commented on June 10, 2024

The short version is that you need to save the objects returned by hs.timer.doEvery(...) and hs.network.ping(...) so that they don't get garbage-collected before they can do their job.

One quirk of Hammerspoon that often trips people up is that a lot of its objects have visible side effects when they are garbage-collected. Timers stop running, canvases and webviews close, event taps stop intercepting events, etc. To prevent that, you have to save a reference to the object somewhere, like a global variable, the return value of a required module, or a table inside one of those. hs.timer.doEvery, .doAfter, and hs.network.ping need the same treatment, even though their names sound more like 'do something' than 'create something'.

What makes this more annoying is that garbage collection in Lua is non-deterministic: Objects aren't guaranteed to get GC'd immediately when the last reference goes away, but can stick around for a while until Lua decides it needs to free up some memory. So a doEvery timer might seem to be working, then randomly stop itself some time later. I'm actually surprised it's failing that consistently for you. I run a GC pass at the very end of my config to make sure things break as quickly and consistently as possible.

See also: https://github.com/asmagill/hammerspoon/wiki/Variable-Scope-and-Garbage-Collection

from hammerspoon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.