Skip to content

Commit

Permalink
Apply memleak patches
Browse files Browse the repository at this point in the history
 npm run build && cp -r packages /Users/bep/dev/go/gohugoio/hugo-mod-jslibs-dist/alpinejs
  • Loading branch information
bep committed Nov 10, 2023
1 parent 6cecc7d commit edbabb1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/alpinejs/src/directives/x-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ directive('data', ((el, { expression }, { cleanup }) => {
expression = expression === '' ? '{}' : expression

let magicContext = {}
injectMagics(magicContext, el)
let cleanup1 = injectMagics(magicContext, el).cleanup

let dataProviderContext = {}
injectDataProviders(dataProviderContext, magicContext)
Expand All @@ -25,7 +25,7 @@ directive('data', ((el, { expression }, { cleanup }) => {

if (data === undefined || data === true) data = {}

injectMagics(data, el)
let cleanup2 = injectMagics(data, el).cleanup

let reactiveData = reactive(data)

Expand All @@ -39,5 +39,9 @@ directive('data', ((el, { expression }, { cleanup }) => {
reactiveData['destroy'] && evaluate(el, reactiveData['destroy'])

undo()

// MemLeak1: Issue #2140
cleanup1()
cleanup2()
})
}))
6 changes: 5 additions & 1 deletion packages/alpinejs/src/evaluator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { closestDataStack, mergeProxies } from './scope'
import { injectMagics } from './magics'
import { tryCatch, handleError } from './utils/error'
import { onAttributeRemoved } from './mutation'

let shouldAutoEvaluateFunctions = true

Expand Down Expand Up @@ -37,7 +38,10 @@ export function setEvaluator(newEvaluator) {
export function normalEvaluator(el, expression) {
let overriddenMagics = {}

injectMagics(overriddenMagics, el)
let cleanup = injectMagics(overriddenMagics, el).cleanup

// MemLeak1: Issue #2140 (note: there are other uses of injectMagics)
onAttributeRemoved(el, "evaluator", cleanup)

let dataStack = [overriddenMagics, ...closestDataStack(el)]

Expand Down
8 changes: 7 additions & 1 deletion packages/alpinejs/src/magics.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,11 @@ export function injectMagics(obj, el) {
})
})

return obj
return {
obj,
cleanup: () => {
// MemLeak1: Issue #2140
el = null;
}
};
}

0 comments on commit edbabb1

Please sign in to comment.