Skip to content

Commit

Permalink
Attempt memory leak fix from using magics (#2832)
Browse files Browse the repository at this point in the history
* Attempt memory leak fix from using magics

* another potential fix for memory leak with magics

maybe safer for some types of consumption

---------

Co-authored-by: Byron Anderson <byron@geometer.io>
  • Loading branch information
byronanderson and Byron Anderson committed Apr 21, 2023
1 parent 6142a83 commit 61bef0d
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions packages/alpinejs/src/magics.js
Expand Up @@ -11,17 +11,24 @@ export function magic(name, callback) {

export function injectMagics(obj, el) {
Object.entries(magics).forEach(([name, callback]) => {
Object.defineProperty(obj, `$${name}`, {
get() {
let memoizedUtilities = null;
function getUtilities() {
if (memoizedUtilities) {
return memoizedUtilities;
} else {
let [utilities, cleanup] = getElementBoundUtilities(el)

utilities = {interceptor, ...utilities}
memoizedUtilities = {interceptor, ...utilities}

onElRemoved(el, cleanup)

return callback(el, utilities)
return memoizedUtilities;
}
}

Object.defineProperty(obj, `$${name}`, {
get() {
return callback(el, getUtilities());
},

enumerable: false,
})
})
Expand Down

0 comments on commit 61bef0d

Please sign in to comment.