@@ -82,6 +82,8 @@ export let untracking = false;
8282
8383export let unsafe_mutations = false ;
8484
85+ export let unsafe_mutation_inside_effect = false ;
86+
8587/** @param {null | Reaction } reaction */
8688export function set_active_reaction ( reaction ) {
8789 active_reaction = reaction ;
@@ -517,8 +519,10 @@ export function update_effect(effect) {
517519
518520 var previous_effect = active_effect ;
519521 var previous_component_context = component_context ;
522+ var previous_unsafe_mutation_inside_effect = unsafe_mutation_inside_effect ;
520523
521524 active_effect = effect ;
525+ unsafe_mutation_inside_effect = false ;
522526
523527 if ( DEV ) {
524528 var previous_component_fn = dev_current_component_function ;
@@ -536,6 +540,18 @@ export function update_effect(effect) {
536540 execute_effect_teardown ( effect ) ;
537541 var teardown = update_reaction ( effect ) ;
538542 effect . teardown = typeof teardown === 'function' ? teardown : null ;
543+
544+ // If unsafe() has been used within the effect, then we might need
545+ // to schedule another update for this effect in case the unsafe mutation
546+ // has caused a this effect to become invalidated again
547+ if ( unsafe_mutation_inside_effect && ( effect . f & CLEAN ) !== 0 ) {
548+ set_signal_status ( effect , MAYBE_DIRTY ) ;
549+ if ( check_dirtiness ( effect ) ) {
550+ set_signal_status ( effect , DIRTY ) ;
551+ schedule_effect ( effect ) ;
552+ }
553+ }
554+
539555 effect . version = current_version ;
540556
541557 if ( DEV ) {
@@ -545,6 +561,7 @@ export function update_effect(effect) {
545561 handle_error ( error , effect , previous_effect , previous_component_context || effect . ctx ) ;
546562 } finally {
547563 active_effect = previous_effect ;
564+ unsafe_mutation_inside_effect = previous_unsafe_mutation_inside_effect ;
548565
549566 if ( DEV ) {
550567 dev_current_component_function = previous_component_fn ;
@@ -1047,7 +1064,13 @@ export function unsafe(fn) {
10471064 var previous_unsafe_mutations = unsafe_mutations ;
10481065 try {
10491066 unsafe_mutations = true ;
1050- return fn ( ) ;
1067+ var val = fn ( ) ;
1068+
1069+ if ( is_flushing_effect ) {
1070+ unsafe_mutation_inside_effect = true ;
1071+ }
1072+
1073+ return val ;
10511074 } finally {
10521075 unsafe_mutations = previous_unsafe_mutations ;
10531076 }
0 commit comments