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 memory leaks #6889

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 21 additions & 15 deletions packages/core/src/basecomponent/BaseComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ export default {
},
dt: {
immediate: true,
handler(newValue) {
handler(newValue, oldValue) {
if (oldValue) {
ThemeService.off('theme:change', this._themeScopedListener);
}

if (newValue) {
this._loadScopedThemeStyles(newValue);
this._themeChangeListener(() => this._loadScopedThemeStyles(newValue));
this._themeScopedListener = () => this._loadScopedThemeStyles(newValue);
this._themeChangeListener(this._themeScopedListener);
} else {
this._unloadScopedThemeStyles();
}
Expand Down Expand Up @@ -100,6 +105,8 @@ export default {
this._hook('onBeforeUnmount');
},
unmounted() {
ThemeService.off('theme:change', this._loadCoreStyles);
ThemeService.off('theme:change', this._load);
this._unloadScopedThemeStyles();
this._hook('onUnmounted');
},
Expand All @@ -116,21 +123,20 @@ export default {
_mergeProps(fn, ...args) {
return isFunction(fn) ? fn(...args) : mergeProps(...args);
},
_loadStyles() {
const _load = () => {
// @todo
if (!Base.isStyleNameLoaded('base')) {
BaseStyle.loadCSS(this.$styleOptions);
this._loadGlobalStyles();

Base.setLoadedStyleName('base');
}
_load() {
// @todo
if (!Base.isStyleNameLoaded('base')) {
BaseStyle.loadCSS(this.$styleOptions);
this._loadGlobalStyles();

this._loadThemeStyles();
};
Base.setLoadedStyleName('base');
}

_load();
this._themeChangeListener(_load);
this._loadThemeStyles();
},
_loadStyles() {
this._load();
this._themeChangeListener(this._load);
},
_loadCoreStyles() {
if (!Base.isStyleNameLoaded(this.$style?.name) && this.$style?.name) {
Expand Down
29 changes: 26 additions & 3 deletions packages/core/src/basedirective/BaseDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ const BaseDirective = {
BaseDirective._loadThemeStyles(el.$instance, useStyleOptions);
BaseDirective._loadScopedThemeStyles(el.$instance, useStyleOptions);

BaseDirective._themeChangeListener(() => BaseDirective._loadThemeStyles(el.$instance, useStyleOptions));
BaseDirective._removeThemeListeners(el.$instance);

el.$instance.$loadStyles = () => BaseDirective._loadThemeStyles(el.$instance, useStyleOptions);

BaseDirective._themeChangeListener(el.$instance.$loadStyles);
},
_loadCoreStyles(instance = {}, useStyleOptions) {
if (!Base.isStyleNameLoaded(instance.$style?.name) && instance.$style?.name) {
Expand Down Expand Up @@ -134,6 +138,9 @@ const BaseDirective = {
Base.clearLoadedStyleNames();
ThemeService.on('theme:change', callback);
},
_removeThemeListeners(instance) {
ThemeService.off('theme:change', instance.$loadStyles);
},
_hook: (directiveName, hookName, el, binding, vnode, prevVnode) => {
const name = `on${toCapitalCase(hookName)}`;
const config = BaseDirective._getConfig(binding, vnode);
Expand Down Expand Up @@ -193,13 +200,27 @@ const BaseDirective = {
const handleWatch = (el) => {
const watchers = el.$instance?.watch;

const handleWatchConfig = ({ newValue, oldValue }) => watchers?.['config']?.call(el.$instance, newValue, oldValue);
const handleWatchConfigRipple = ({ newValue, oldValue }) => watchers?.['config.ripple']?.call(el.$instance, newValue, oldValue);

el.$instance.$watchersCallback = { config: handleWatchConfig, 'config.ripple': handleWatchConfigRipple };

// for 'config'
watchers?.['config']?.call(el.$instance, el.$instance?.$primevueConfig);
PrimeVueService.on('config:change', ({ newValue, oldValue }) => watchers?.['config']?.call(el.$instance, newValue, oldValue));
PrimeVueService.on('config:change', handleWatchConfig);

// for 'config.ripple'
watchers?.['config.ripple']?.call(el.$instance, el.$instance?.$primevueConfig?.ripple);
PrimeVueService.on('config:ripple:change', ({ newValue, oldValue }) => watchers?.['config.ripple']?.call(el.$instance, newValue, oldValue));
PrimeVueService.on('config:ripple:change', handleWatchConfigRipple);
};

const removeWatch = (el) => {
const watchers = el.$instance.$watchersCallback;

if (watchers) {
PrimeVueService.off('config:change', watchers.config);
PrimeVueService.off('config:ripple:change', watchers['config.ripple']);
}
};

return {
Expand All @@ -225,6 +246,8 @@ const BaseDirective = {
handleHook('updated', el, binding, vnode, prevVnode);
},
beforeUnmount: (el, binding, vnode, prevVnode) => {
removeWatch(el);
BaseDirective._removeThemeListeners(el.$instance);
handleHook('beforeUnmount', el, binding, vnode, prevVnode);
},
unmounted: (el, binding, vnode, prevVnode) => {
Expand Down
Loading