Skip to content

support abortcontroller / abortsignal to all event handler / life hooks #14407

@jimmywarting

Description

@jimmywarting

Describe the problem

when leaving the current page in a SPA, i would like to clean things up myself, using onDestroy works great for such wonders.
But if i'm finish with that listener that i have added then i would also like to remove that event listener also.

I think it's complicated to learn all new non-standard event handler that all frameworks try to re-invent... ideally i would just simply want to have a standard EventTarget instead that do support the { once, signal } options as a third argument.

being able to use abortController would help tremendously. even for fetching data that needs to be used on a particular page.

Describe the proposed solution

import { onDestroy } from 'svelte'

const ctrl = new AbortController()
const signal = ctrl.signal

signal.addEventlistener('abort', {
  elm.remove()
  elm = null // remove a element since we don't need it anymore
}, { once: true, signal })

something.addEventlistener('keyup', evt => {
  if (evt.key === 'escape') {
    ctrl.abort()
  }
}, { signal })

// unless this listener isn't removed it will still keep a references to some
// variable and prevent garbage collection if the added listener can't be removed...
onDestroy(() => { 
  elm.remove() // Error: can't call remove() of null
}, { signal}) // <-- add support for this

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting submitterneeds a reproduction, or clarification

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions