Ver código fonte

Fix $watch Livewire loop issues

Caleb Porzio 3 anos atrás
pai
commit
d061f2d660

+ 7 - 1
packages/alpinejs/src/magics/$watch.js

@@ -7,7 +7,7 @@ magic('watch', (el, { evaluateLater, effect }) => (key, callback) => {
 
 
     let oldValue
     let oldValue
 
 
-    effect(() => evaluate(value => {
+    let effectReference = effect(() => evaluate(value => {
         // JSON.stringify touches every single property at any level enabling deep watching
         // JSON.stringify touches every single property at any level enabling deep watching
         JSON.stringify(value)
         JSON.stringify(value)
 
 
@@ -25,4 +25,10 @@ magic('watch', (el, { evaluateLater, effect }) => (key, callback) => {
 
 
         firstTime = false
         firstTime = false
     }))
     }))
+
+    // We want to remove this effect from the list of effects
+    // stored on an element. Livewire uses that list to
+    // "re-run" Alpine effects after a page load. A "watcher"
+    // shuldn't be re-run like that. It will cause infinite loops.
+    el._x_effects.delete(effectReference)
 })
 })

+ 2 - 0
packages/alpinejs/src/reactivity.js

@@ -49,6 +49,8 @@ export function elementBoundEffect(el) {
 
 
             release(effectReference)
             release(effectReference)
         }
         }
+
+        return effectReference
     }
     }
 
 
     return [wrappedEffect, () => { cleanup() }]
     return [wrappedEffect, () => { cleanup() }]