Fixed #5957 - [Core]: PrimeVue 4 is causing a memory leak

pull/5981/head
Mert Sincan 2024-06-27 12:21:38 +01:00
parent 064e172520
commit c78afb0767
1 changed files with 23 additions and 6 deletions

View File

@ -172,11 +172,21 @@ export function setup(app, options) {
app.config.globalProperties.$primevue = PrimeVue; app.config.globalProperties.$primevue = PrimeVue;
app.provide(PrimeVueSymbol, PrimeVue); app.provide(PrimeVueSymbol, PrimeVue);
clearConfig();
setupConfig(app, PrimeVue); setupConfig(app, PrimeVue);
return PrimeVue; return PrimeVue;
} }
let stopWatchers = [];
export function clearConfig() {
ThemeService.clear();
stopWatchers.forEach((fn) => fn?.());
stopWatchers = [];
}
export function setupConfig(app, PrimeVue) { export function setupConfig(app, PrimeVue) {
const isThemeChanged = ref(false); const isThemeChanged = ref(false);
@ -196,12 +206,14 @@ export function setupConfig(app, PrimeVue) {
}; };
ThemeService.on('theme:change', function (newTheme) { ThemeService.on('theme:change', function (newTheme) {
isThemeChanged.value = true; if (!isThemeChanged.value) {
app.config.globalProperties.$primevue.config.theme = newTheme; app.config.globalProperties.$primevue.config.theme = newTheme;
isThemeChanged.value = true;
}
}); });
/*** Watchers ***/ /*** Watchers ***/
watch( const stopConfigWatcher = watch(
PrimeVue.config, PrimeVue.config,
(newValue, oldValue) => { (newValue, oldValue) => {
PrimeVueService.emit('config:change', { newValue, oldValue }); PrimeVueService.emit('config:change', { newValue, oldValue });
@ -209,7 +221,7 @@ export function setupConfig(app, PrimeVue) {
{ immediate: true, deep: true } { immediate: true, deep: true }
); );
watch( const stopRippleWatcher = watch(
() => PrimeVue.config.ripple, () => PrimeVue.config.ripple,
(newValue, oldValue) => { (newValue, oldValue) => {
PrimeVueService.emit('config:ripple:change', { newValue, oldValue }); PrimeVueService.emit('config:ripple:change', { newValue, oldValue });
@ -217,7 +229,7 @@ export function setupConfig(app, PrimeVue) {
{ immediate: true, deep: true } { immediate: true, deep: true }
); );
watch( const stopThemeWatcher = watch(
() => PrimeVue.config.theme, () => PrimeVue.config.theme,
(newValue, oldValue) => { (newValue, oldValue) => {
if (!isThemeChanged.value) { if (!isThemeChanged.value) {
@ -234,7 +246,7 @@ export function setupConfig(app, PrimeVue) {
{ immediate: true, deep: true } { immediate: true, deep: true }
); );
watch( const stopUnstyledWatcher = watch(
() => PrimeVue.config.unstyled, () => PrimeVue.config.unstyled,
(newValue, oldValue) => { (newValue, oldValue) => {
if (!newValue && PrimeVue.config.theme) { if (!newValue && PrimeVue.config.theme) {
@ -245,6 +257,11 @@ export function setupConfig(app, PrimeVue) {
}, },
{ immediate: true, deep: true } { immediate: true, deep: true }
); );
stopWatchers.push(stopConfigWatcher);
stopWatchers.push(stopRippleWatcher);
stopWatchers.push(stopThemeWatcher);
stopWatchers.push(stopUnstyledWatcher);
} }
export default { export default {