Skip to content

Commit 084e932

Browse files
committed
refactor: simplify lifecycle hook call logic
1 parent b58bb16 commit 084e932

File tree

1 file changed

+15
-43
lines changed

1 file changed

+15
-43
lines changed

packages/runtime-core/src/componentOptions.ts

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -816,59 +816,31 @@ function callSyncHook(
816816
instance: ComponentInternalInstance,
817817
globalMixins: ComponentOptions[]
818818
) {
819-
callHookFromMixins(name, type, globalMixins, instance)
820-
const { extends: base, mixins } = options
821-
if (base) {
822-
callHookFromExtends(name, type, base, instance)
823-
}
824-
if (mixins) {
825-
callHookFromMixins(name, type, mixins, instance)
826-
}
827-
const selfHook = options[name]
828-
if (selfHook) {
829-
callWithAsyncErrorHandling(selfHook.bind(instance.proxy!), instance, type)
819+
for (let i = 0; i < globalMixins.length; i++) {
820+
callHookWithMixinAndExtends(name, type, globalMixins[i], instance)
830821
}
822+
callHookWithMixinAndExtends(name, type, options, instance)
831823
}
832824

833-
function callHookFromExtends(
825+
function callHookWithMixinAndExtends(
834826
name: 'beforeCreate' | 'created',
835827
type: LifecycleHooks,
836-
base: ComponentOptions,
828+
options: ComponentOptions,
837829
instance: ComponentInternalInstance
838830
) {
839-
if (base.extends) {
840-
callHookFromExtends(name, type, base.extends, instance)
841-
}
842-
const chainedMixins = base.mixins
843-
if (chainedMixins) {
844-
callHookFromMixins(name, type, chainedMixins, instance)
845-
}
846-
const baseHook = base[name]
847-
if (baseHook) {
848-
callWithAsyncErrorHandling(baseHook.bind(instance.proxy!), instance, type)
831+
const { extends: base, mixins } = options
832+
const selfHook = options[name]
833+
if (base) {
834+
callHookWithMixinAndExtends(name, type, base, instance)
849835
}
850-
}
851-
852-
function callHookFromMixins(
853-
name: 'beforeCreate' | 'created',
854-
type: LifecycleHooks,
855-
mixins: ComponentOptions[],
856-
instance: ComponentInternalInstance
857-
) {
858-
for (let i = 0; i < mixins.length; i++) {
859-
const chainedMixins = mixins[i].mixins
860-
const chainedExtends = mixins[i].extends
861-
if (chainedExtends) {
862-
callHookFromExtends(name, type, chainedExtends, instance)
863-
}
864-
if (chainedMixins) {
865-
callHookFromMixins(name, type, chainedMixins, instance)
866-
}
867-
const fn = mixins[i][name]
868-
if (fn) {
869-
callWithAsyncErrorHandling(fn.bind(instance.proxy!), instance, type)
836+
if (mixins) {
837+
for (let i = 0; i < mixins.length; i++) {
838+
callHookWithMixinAndExtends(name, type, mixins[i], instance)
870839
}
871840
}
841+
if (selfHook) {
842+
callWithAsyncErrorHandling(selfHook.bind(instance.proxy!), instance, type)
843+
}
872844
}
873845

874846
function applyMixins(

0 commit comments

Comments
 (0)