Skip to content

Conversation

@trueadm
Copy link
Contributor

@trueadm trueadm commented Dec 18, 2024

We used to have this rune a while ago, back when it worked like $effect.tracking, this time it's back but operates differently. Now it will let you know if you're inside an active effect context. This is super useful for validation patterns where you want to warn people that some utility or API requires usage in specific parts of the codebase – such as a component or another effect.

function my_util() {
  if (!$effect.active()) {
    throw new Error('more useful error message');
  }

  $effect(() => {
    // do util stuff
  });
}

@changeset-bot
Copy link

changeset-bot bot commented Dec 18, 2024

🦋 Changeset detected

Latest commit: 0767c32

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

Playground

pnpm add https://pkg.pr.new/svelte@14757

@trueadm trueadm self-assigned this Dec 18, 2024
@leonidaz
Copy link
Contributor

leonidaz commented Dec 18, 2024

So, basically active() will always be true if code is running inside a component instance, including inside $effect, $effect.root, or anywhere in external code if called from a component instance or inside external $effect or $effect.root. Calling from the module level code in a component will return false.

tracking() will only be true if it runs inside user or template effects, and in effects inside external root. Inside $effect.root or module will always be false.

Very nice!

Arguably, this seems more useful than effect.tracking but I can see that both may be needed depending on a use case.

bunch of use cases

@trueadm
Copy link
Contributor Author

trueadm commented Dec 18, 2024

So, basically active() will always be true if code is running inside a component instance, including inside $effect, $effect.root, or anywhere in external code if called from a component instance or inside external $effect or $effect.root. Calling from the module level code in a component will return false.

tracking() will only be true if it runs inside user or template effects, and in effects inside external root. Inside $effect.root or module will always be false.

Very nice!

Arguably, this seems more useful than effect.tracking but I can see that both may be needed depending on use case.

bunch of use cases

Essentially, yes. There's also some edge cases it handles. Like used in the teardown of an effect, or inside a derived that is "unowned", i.e. created outside of an effect.

@webJose
Copy link
Contributor

webJose commented Dec 19, 2024

Hello. Will this cover #14329 ?

@leonidaz
Copy link
Contributor

leonidaz commented Dec 19, 2024

Essentially, yes. There's also some edge cases it handles. Like used in the teardown of an effect, or inside a derived that is "unowned", i.e. created outside of an effect.

cool, in those cases: teardowns and unowned deriveds, both would be false.

updated use cases: bunch of use cases

@leonidaz
Copy link
Contributor

leonidaz commented Dec 19, 2024

Hello. Will this cover #14329 ?

yeah, it covers anything within a component instance or outside of it if called from the instance. I created a playground with with console.log to compare tracking() vs active() for different use cases: bunch of use cases

@levibassey
Copy link

levibassey commented Dec 19, 2024

yeah, it covers anything within a component instance or outside of it if called from the instance. I created a playground with with console.log to compare tracking() vs active() for different use cases: bunch of use cases

Shouldn't $effect.active() return false if it's in the module scope?

<script module>
	console.log('active -- module component', $effect.active()) // true (Should be false)
	console.log('tracking -- module component', $effect.tracking()) // false
</script>

since there are no active effects

@leonidaz
Copy link
Contributor

yeah, it covers anything within a component instance or outside of it if called from the instance. I created a playground with with console.log to compare tracking() vs active() for different use cases: bunch of use cases

Shouldn't $effect.active() return false if it's in the module scope?

<script module>
	console.log('active -- module component', $effect.active()) // true (Should be false)
	console.log('tracking -- module component', $effect.tracking()) // false
</script>

since there are no active effects

sorry, this is just a typo in the comment, copy and paste. yes, both are always false in modules. updated

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.

5 participants