Skip to content

Commit

Permalink
rework (#3929)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebporzio authored Dec 15, 2023
1 parent 0effdae commit 018e9ce
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions packages/alpinejs/src/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,24 @@ export function stopObservingMutations() {
currentlyObserving = false
}

let recordQueue = []
let willProcessRecordQueue = false
let queuedMutations = []

export function flushObserver() {
recordQueue = recordQueue.concat(observer.takeRecords())
let records = observer.takeRecords()

if (recordQueue.length && ! willProcessRecordQueue) {
willProcessRecordQueue = true
queuedMutations.push(() => records.length > 0 && onMutate(records))

queueMicrotask(() => {
processRecordQueue()
let queueLengthWhenTriggered = queuedMutations.length

willProcessRecordQueue = false
})
}
}

function processRecordQueue() {
onMutate(recordQueue)

recordQueue.length = 0
queueMicrotask(() => {
// If these two lengths match, then we KNOW that this is the LAST
// flush in the current event loop. This way, we can process
// all mutations in one batch at the end of everything...
if (queuedMutations.length === queueLengthWhenTriggered) {
// Now Alpine can process all the mutations...
while (queuedMutations.length > 0) queuedMutations.shift()()
}
})
}

export function mutateDom(callback) {
Expand Down

0 comments on commit 018e9ce

Please sign in to comment.