Skip to content

feat: allow signal effect to clean itself up inside callback function#5

Open
felixranesberger wants to merge 1 commit into
jsebrech:mainfrom
felixranesberger:feature/effect-cleanup
Open

feat: allow signal effect to clean itself up inside callback function#5
felixranesberger wants to merge 1 commit into
jsebrech:mainfrom
felixranesberger:feature/effect-cleanup

Conversation

@felixranesberger
Copy link
Copy Markdown

// this doesn't work, since the effect runs before the cleanup function is returned
const cleanup = imageSize.effect(() => {
    if (imageSize.value <= 0)
        return

    loadImage()
    cleanup() // throws error
})

// this works now
imageSize.effect((cleanup) => {
    if (imageSize.value <= 0)
        return

    loadImage()
    cleanup()
})

Copy link
Copy Markdown
Owner

@jsebrech jsebrech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has the effect of adding two listeners if called twice. Also, it doesn't quite feel "tiny". Perhaps for this use case a setTimeout is an adequate solution?

const cleanup = imageSize.effect(() => {
    if (imageSize.value <= 0)
        return

    loadImage()
    setTimeout(() => cleanup(), 0);
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants