Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: memoization when injecting magic #4276

Merged
merged 6 commits into from
Jul 1, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: memoization when injecting magic
  • Loading branch information
AbhiShake1 authored Jun 26, 2024
commit 7e91ecf363636a4b21506eed47900a3ee40d870c
25 changes: 10 additions & 15 deletions packages/alpinejs/src/magics.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,19 @@ export function magic(name, callback) {
}

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

memoizedUtilities = {interceptor, ...utilities}

onElRemoved(el, cleanup)
return memoizedUtilities;
}
}
let memoizedUtilities = null;

function getUtilities() {
let [utilities, cleanup] = getElementBoundUtilities(el)
memoizedUtilities = {interceptor, ...utilities}
onElRemoved(el, cleanup)
return memoizedUtilities;
}

Object.entries(magics).forEach(([name, callback]) => {
Object.defineProperty(obj, `$${name}`, {
get() {
return callback(el, getUtilities());
return callback(el, memoizedUtilities || getUtilities());
},
enumerable: false,
})
Expand Down
Loading