effect-active-derived-allow-unowned
Rich Harris 8 months ago
parent 1f76e87d85
commit 7987338ce5

@ -27,7 +27,8 @@ import {
BLOCK_EFFECT,
ROOT_EFFECT,
EFFECT_TRANSPARENT,
DERIVED
DERIVED,
UNOWNED
} from '../constants.js';
import { set } from './sources.js';
import { remove } from '../dom/reconciler.js';
@ -40,7 +41,7 @@ import { DEV } from 'esm-env';
* @returns {asserts effect}
*/
export function validate_effect(effect, rune) {
if (effect === null) {
if (effect === null && current_reaction === null) {
e.effect_orphan(rune);
}
@ -119,8 +120,14 @@ function create_effect(type, fn, sync) {
* @returns {boolean}
*/
export function effect_active() {
if (current_effect) return (current_effect.f & (BRANCH_EFFECT | ROOT_EFFECT)) === 0;
if (current_reaction) return (current_reaction.f & DERIVED) !== 0;
if (current_reaction && (current_reaction.f & DERIVED) !== 0) {
return (current_reaction.f & UNOWNED) === 0;
}
if (current_effect) {
return (current_effect.f & (BRANCH_EFFECT | ROOT_EFFECT)) === 0;
}
return false;
}

@ -1,6 +1,17 @@
<script>
let value = $state(false);
const fn = () => {
if ($effect.active()) {
$effect(() => {
value = true;
});
}
return value;
};
let foo = $state(false)
let bar = $derived(foo ? $effect.active() : false);
let bar = $derived(foo ? fn() : false);
</script>
<button onclick={() => foo = !foo}>

Loading…
Cancel
Save