Welcome to mirror list, hosted at ThFree Co, Russian Federation.

$watch.js « magics « src « alpinejs « packages « alpinejs - github.com/gohugoio/hugo-mod-jslibs-dist.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 865e2dba3479b64e1b03714096ec2c79e5e98eec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { evaluateLater } from '../evaluator'
import { elementBoundEffect } from '../reactivity'
import { magic } from '../magics'
import { onAttributeRemoved } from '../mutation'

magic('watch', el => (key, callback) => {
    let evaluate = evaluateLater(el, key)

    let firstTime = true

    let oldValue

    let [effect, cleanupEffect] = elementBoundEffect(el)

    // MemLeak1: Issue #2140
    onAttributeRemoved(el, key, cleanupEffect)

    effect(() => evaluate(value => {
        // This is a hack to force deep reactivity for things like "items.push()".
        let div = document.createElement('div')
        div.dataset.throwAway = value

        if (! firstTime) {
            // We have to queue this watcher as a microtask so that
            // the watcher doesn't pick up its own dependencies.
            queueMicrotask(() => {
                callback(value, oldValue)

                oldValue = value
            })
        } else {
            oldValue = value
        }

        firstTime = false
    }))
})