Pricing Blog

How does Nordcraft handle cleaning up event listeners?

  • lennartblmn-1399037144818909274

    lennartblmn

    3 months ago

    I tried to analyse the memory tab to the best of my knowledge, but I'd like to know how Nordcraft cleans up unused elements and event listeners. For example, if I add a scroll event listener on a div, then use the hide/show feature to remove it from the DOM - what happens to the scroll event listener?

    And in comparison to that, i actually use a passive scroll event listener using a small custom action, does Nordcraft do anything to that if it's a custom action?

    Thanks for your help!
  • jacobkofoed-1399042163131547718

    Jacob Kofoed

    3 months ago

    The Nordcraft engine just removes the element on show/hide to clean up events such as scroll listeners on the element self. However, some extra steps may happen just before:
    - An abort trigger is sent to all pending APIs. This just tells the browser to stop anything that can still be cancelled.
    - Secondly all previously active actions with a cleanup callback have their cleanup called. You can create a cleanup function in code formulas by making it return a function. It should be documented in the docs. Some of the standard actions does cleanup such as cancelling timeouts in the “Sleep” action etc.
    ❤️1
  • lennartblmn-1399045135894970522

    lennartblmn

    3 months ago

    Thanks for the fast reply, sorry I didn't check the docs first.

    Is there a specific format or name or anything like that I need to give the cleanup function? How does Nordcraft know that it's a cleanup that should be called?
  • jacobkofoed-1399050291373932694

    Jacob Kofoed

    3 months ago

    It is whatever optional argumentless function that is returned. The syntax is like here for example: https://editor.nordcraft.com/projects/silver_wat_tambor_remote_alligator/branches/start/actions/actionWithCleanupFunction?mode=design&rightpanel=attributes&canvas-width=800&canvas-height=800

    You can try the example here where you try to show/hide the component with the toggle button. The cleanup function is called when the element is destroyed:

    https://start-silver_wat_tambor_remote_alligator.toddle.site/
    ❤️1
  • lennartblmn-1399050725434331148

    lennartblmn

    3 months ago

    This is amazing, thank you so much!
  • jacobkofoed-1399051476797489343

    Jacob Kofoed

    3 months ago

    I sometimes have had the need for a pure cleanup action to perform something just before a component is removed. Like saving state to local-storage etc.

    https://editor.nordcraft.com/projects/silver_wat_tambor_remote_alligator/branches/start/actions/onUnmount?mode=design&rightpanel=attributes&canvas-width=800&canvas-height=800
    🙏1
  • lennartblmn-1399054761147371642

    lennartblmn

    3 months ago

    How would i use this onUnmount function?
  • lennartblmn-1399077639980847185

    lennartblmn

    3 months ago

    oh i got it. thank you so much, this makes a huge difference.