Skip to content

Commit

Permalink
Revert "alpinejs: Remove the custom memory patch"
Browse files Browse the repository at this point in the history
This reverts commit 6ccb109.

The above removal didn't fix my problem, so keep the patch, as it solves a real problem.
  • Loading branch information
bep committed Nov 17, 2023
1 parent 6ccb109 commit af8d4a5
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 32 deletions.
16 changes: 12 additions & 4 deletions alpinejs/packages/alpinejs/dist/cdn.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,12 @@
enumerable: false
});
});
return obj;
return {
obj,
cleanup: () => {
el = null;
}
};
}

// packages/alpinejs/src/utils/error.js
Expand Down Expand Up @@ -563,7 +568,8 @@ ${expression ? 'Expression: "' + expression + '"\n\n' : ""}`, el);
}
function normalEvaluator(el, expression) {
let overriddenMagics = {};
injectMagics(overriddenMagics, el);
let cleanup2 = injectMagics(overriddenMagics, el).cleanup;
onAttributeRemoved(el, "evaluator", cleanup2);
let dataStack = [overriddenMagics, ...closestDataStack(el)];
let evaluator = typeof expression === "function" ? generateEvaluatorFromFunction(dataStack, expression) : generateEvaluatorFromString(dataStack, expression, el);
return tryCatch.bind(null, el, expression, evaluator);
Expand Down Expand Up @@ -2915,20 +2921,22 @@ ${expression ? 'Expression: "' + expression + '"\n\n' : ""}`, el);
return;
expression = expression === "" ? "{}" : expression;
let magicContext = {};
injectMagics(magicContext, el);
let cleanup1 = injectMagics(magicContext, el).cleanup;
let dataProviderContext = {};
injectDataProviders(dataProviderContext, magicContext);
let data2 = evaluate(el, expression, { scope: dataProviderContext });
if (data2 === void 0 || data2 === true)
data2 = {};
injectMagics(data2, el);
let cleanup22 = injectMagics(data2, el).cleanup;
let reactiveData = reactive(data2);
initInterceptors2(reactiveData);
let undo = addScopeToNode(el, reactiveData);
reactiveData["init"] && evaluate(el, reactiveData["init"]);
cleanup2(() => {
reactiveData["destroy"] && evaluate(el, reactiveData["destroy"]);
undo();
cleanup1();
cleanup22();
});
});

Expand Down
4 changes: 2 additions & 2 deletions alpinejs/packages/alpinejs/dist/cdn.min.js

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions alpinejs/packages/alpinejs/dist/module.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -1931,7 +1931,12 @@ function injectMagics(obj, el) {
enumerable: false
});
});
return obj;
return {
obj,
cleanup: () => {
el = null;
}
};
}

// packages/alpinejs/src/utils/error.js
Expand Down Expand Up @@ -1975,7 +1980,8 @@ function setEvaluator(newEvaluator) {
}
function normalEvaluator(el, expression) {
let overriddenMagics = {};
injectMagics(overriddenMagics, el);
let cleanup = injectMagics(overriddenMagics, el).cleanup;
onAttributeRemoved(el, "evaluator", cleanup);
let dataStack = [overriddenMagics, ...closestDataStack(el)];
let evaluator = typeof expression === "function" ? generateEvaluatorFromFunction(dataStack, expression) : generateEvaluatorFromString(dataStack, expression, el);
return tryCatch.bind(null, el, expression, evaluator);
Expand Down Expand Up @@ -3622,20 +3628,22 @@ directive("data", (el, { expression }, { cleanup }) => {
return;
expression = expression === "" ? "{}" : expression;
let magicContext = {};
injectMagics(magicContext, el);
let cleanup1 = injectMagics(magicContext, el).cleanup;
let dataProviderContext = {};
injectDataProviders(dataProviderContext, magicContext);
let data2 = evaluate(el, expression, { scope: dataProviderContext });
if (data2 === void 0 || data2 === true)
data2 = {};
injectMagics(data2, el);
let cleanup2 = injectMagics(data2, el).cleanup;
let reactiveData = reactive(data2);
initInterceptors2(reactiveData);
let undo = addScopeToNode(el, reactiveData);
reactiveData["init"] && evaluate(el, reactiveData["init"]);
cleanup(() => {
reactiveData["destroy"] && evaluate(el, reactiveData["destroy"]);
undo();
cleanup1();
cleanup2();
});
});

Expand Down
16 changes: 12 additions & 4 deletions alpinejs/packages/alpinejs/dist/module.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,12 @@ function injectMagics(obj, el) {
enumerable: false
});
});
return obj;
return {
obj,
cleanup: () => {
el = null;
}
};
}

// packages/alpinejs/src/utils/error.js
Expand Down Expand Up @@ -562,7 +567,8 @@ function setEvaluator(newEvaluator) {
}
function normalEvaluator(el, expression) {
let overriddenMagics = {};
injectMagics(overriddenMagics, el);
let cleanup2 = injectMagics(overriddenMagics, el).cleanup;
onAttributeRemoved(el, "evaluator", cleanup2);
let dataStack = [overriddenMagics, ...closestDataStack(el)];
let evaluator = typeof expression === "function" ? generateEvaluatorFromFunction(dataStack, expression) : generateEvaluatorFromString(dataStack, expression, el);
return tryCatch.bind(null, el, expression, evaluator);
Expand Down Expand Up @@ -2914,20 +2920,22 @@ directive("data", (el, { expression }, { cleanup: cleanup2 }) => {
return;
expression = expression === "" ? "{}" : expression;
let magicContext = {};
injectMagics(magicContext, el);
let cleanup1 = injectMagics(magicContext, el).cleanup;
let dataProviderContext = {};
injectDataProviders(dataProviderContext, magicContext);
let data2 = evaluate(el, expression, { scope: dataProviderContext });
if (data2 === void 0 || data2 === true)
data2 = {};
injectMagics(data2, el);
let cleanup22 = injectMagics(data2, el).cleanup;
let reactiveData = reactive(data2);
initInterceptors2(reactiveData);
let undo = addScopeToNode(el, reactiveData);
reactiveData["init"] && evaluate(el, reactiveData["init"]);
cleanup2(() => {
reactiveData["destroy"] && evaluate(el, reactiveData["destroy"]);
undo();
cleanup1();
cleanup22();
});
});

Expand Down
8 changes: 6 additions & 2 deletions alpinejs/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 alpinejs/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 alpinejs/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;
}
};
}
16 changes: 12 additions & 4 deletions alpinejs/packages/csp/dist/cdn.js
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,12 @@
enumerable: false
});
});
return obj;
return {
obj,
cleanup: () => {
el = null;
}
};
}

// packages/alpinejs/src/utils/error.js
Expand Down Expand Up @@ -563,7 +568,8 @@ ${expression ? 'Expression: "' + expression + '"\n\n' : ""}`, el);
}
function normalEvaluator(el, expression) {
let overriddenMagics = {};
injectMagics(overriddenMagics, el);
let cleanup2 = injectMagics(overriddenMagics, el).cleanup;
onAttributeRemoved(el, "evaluator", cleanup2);
let dataStack = [overriddenMagics, ...closestDataStack(el)];
let evaluator = typeof expression === "function" ? generateEvaluatorFromFunction(dataStack, expression) : generateEvaluatorFromString(dataStack, expression, el);
return tryCatch.bind(null, el, expression, evaluator);
Expand Down Expand Up @@ -2915,20 +2921,22 @@ ${expression ? 'Expression: "' + expression + '"\n\n' : ""}`, el);
return;
expression = expression === "" ? "{}" : expression;
let magicContext = {};
injectMagics(magicContext, el);
let cleanup1 = injectMagics(magicContext, el).cleanup;
let dataProviderContext = {};
injectDataProviders(dataProviderContext, magicContext);
let data2 = evaluate(el, expression, { scope: dataProviderContext });
if (data2 === void 0 || data2 === true)
data2 = {};
injectMagics(data2, el);
let cleanup22 = injectMagics(data2, el).cleanup;
let reactiveData = reactive(data2);
initInterceptors2(reactiveData);
let undo = addScopeToNode(el, reactiveData);
reactiveData["init"] && evaluate(el, reactiveData["init"]);
cleanup2(() => {
reactiveData["destroy"] && evaluate(el, reactiveData["destroy"]);
undo();
cleanup1();
cleanup22();
});
});

Expand Down
Loading

0 comments on commit af8d4a5

Please sign in to comment.