From 6ae87fe433a9a994494a3d808661bd8b0eda483f Mon Sep 17 00:00:00 2001 From: Mert Sincan Date: Mon, 13 Jan 2025 11:22:54 +0000 Subject: [PATCH] Refactor #6715 --- packages/core/src/basecomponent/BaseComponent.vue | 14 +++++++++----- packages/core/src/basedirective/BaseDirective.js | 8 ++++---- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/core/src/basecomponent/BaseComponent.vue b/packages/core/src/basecomponent/BaseComponent.vue index 1f308debe..15ba25cd9 100644 --- a/packages/core/src/basecomponent/BaseComponent.vue +++ b/packages/core/src/basecomponent/BaseComponent.vue @@ -37,6 +37,8 @@ export default { isUnstyled: { immediate: true, handler(newValue) { + ThemeService.off('theme:change', this._loadCoreStyles); + if (!newValue) { this._loadCoreStyles(); this._themeChangeListener(this._loadCoreStyles); // update styles with theme settings @@ -46,9 +48,7 @@ export default { dt: { immediate: true, handler(newValue, oldValue) { - if (oldValue) { - ThemeService.off('theme:change', this._themeScopedListener); - } + ThemeService.off('theme:change', this._themeScopedListener); if (newValue) { this._loadScopedThemeStyles(newValue); @@ -105,8 +105,7 @@ export default { this._hook('onBeforeUnmount'); }, unmounted() { - ThemeService.off('theme:change', this._loadCoreStyles); - ThemeService.off('theme:change', this._load); + this._removeThemeListeners(); this._unloadScopedThemeStyles(); this._hook('onUnmounted'); }, @@ -208,6 +207,11 @@ export default { Base.clearLoadedStyleNames(); ThemeService.on('theme:change', callback); }, + _removeThemeListeners() { + ThemeService.off('theme:change', this._loadCoreStyles); + ThemeService.off('theme:change', this._load); + ThemeService.off('theme:change', this._themeScopedListener); + }, _getHostInstance(instance) { return instance ? (this.$options.hostName ? (instance.$.type.name === this.$options.hostName ? instance : this._getHostInstance(instance.$parentInstance)) : instance.$parentInstance) : undefined; }, diff --git a/packages/core/src/basedirective/BaseDirective.js b/packages/core/src/basedirective/BaseDirective.js index 501016ab9..ef9df6176 100644 --- a/packages/core/src/basedirective/BaseDirective.js +++ b/packages/core/src/basedirective/BaseDirective.js @@ -197,7 +197,7 @@ const BaseDirective = { el.$pd[name] = { ...el.$pd?.[name], name, instance: el.$instance }; }; - const handleWatch = (el) => { + const handleWatchers = (el) => { const watchers = el.$instance?.watch; const handleWatchConfig = ({ newValue, oldValue }) => watchers?.['config']?.call(el.$instance, newValue, oldValue); @@ -214,7 +214,7 @@ const BaseDirective = { PrimeVueService.on('config:ripple:change', handleWatchConfigRipple); }; - const removeWatch = (el) => { + const stopWatchers = (el) => { const watchers = el.$instance.$watchersCallback; if (watchers) { @@ -232,7 +232,7 @@ const BaseDirective = { beforeMount: (el, binding, vnode, prevVnode) => { BaseDirective._loadStyles(el, binding, vnode); handleHook('beforeMount', el, binding, vnode, prevVnode); - handleWatch(el); + handleWatchers(el); }, mounted: (el, binding, vnode, prevVnode) => { BaseDirective._loadStyles(el, binding, vnode); @@ -246,7 +246,7 @@ const BaseDirective = { handleHook('updated', el, binding, vnode, prevVnode); }, beforeUnmount: (el, binding, vnode, prevVnode) => { - removeWatch(el); + stopWatchers(el); BaseDirective._removeThemeListeners(el.$instance); handleHook('beforeUnmount', el, binding, vnode, prevVnode); },