|
5 | 5 | ComponentInternalOptions,
|
6 | 6 | Component,
|
7 | 7 | ConcreteComponent,
|
8 |
| - InternalRenderFunction |
| 8 | + InternalRenderFunction, |
| 9 | + LifecycleHooks |
9 | 10 | } from './component'
|
10 | 11 | import {
|
11 | 12 | isFunction,
|
@@ -55,6 +56,7 @@ import {
|
55 | 56 | } from './componentPublicInstance'
|
56 | 57 | import { warn } from './warning'
|
57 | 58 | import { VNodeChild } from './vnode'
|
| 59 | +import { callWithAsyncErrorHandling } from './errorHandling' |
58 | 60 |
|
59 | 61 | /**
|
60 | 62 | * Interface for declaring custom options.
|
@@ -472,7 +474,13 @@ export function applyOptions(
|
472 | 474 | // applyOptions is called non-as-mixin once per instance
|
473 | 475 | if (!asMixin) {
|
474 | 476 | isInBeforeCreate = true
|
475 |
| - callSyncHook('beforeCreate', options, publicThis, globalMixins) |
| 477 | + callSyncHook( |
| 478 | + 'beforeCreate', |
| 479 | + LifecycleHooks.BEFORE_CREATE, |
| 480 | + options, |
| 481 | + instance, |
| 482 | + globalMixins |
| 483 | + ) |
476 | 484 | isInBeforeCreate = false
|
477 | 485 | // global mixins are applied first
|
478 | 486 | applyMixins(instance, globalMixins, deferredData, deferredWatch)
|
@@ -662,7 +670,13 @@ export function applyOptions(
|
662 | 670 |
|
663 | 671 | // lifecycle options
|
664 | 672 | if (!asMixin) {
|
665 |
| - callSyncHook('created', options, publicThis, globalMixins) |
| 673 | + callSyncHook( |
| 674 | + 'created', |
| 675 | + LifecycleHooks.CREATED, |
| 676 | + options, |
| 677 | + instance, |
| 678 | + globalMixins |
| 679 | + ) |
666 | 680 | }
|
667 | 681 | if (beforeMount) {
|
668 | 682 | onBeforeMount(beforeMount.bind(publicThis))
|
@@ -707,52 +721,54 @@ export function applyOptions(
|
707 | 721 |
|
708 | 722 | function callSyncHook(
|
709 | 723 | name: 'beforeCreate' | 'created',
|
| 724 | + type: LifecycleHooks, |
710 | 725 | options: ComponentOptions,
|
711 |
| - ctx: ComponentPublicInstance, |
| 726 | + instance: ComponentInternalInstance, |
712 | 727 | globalMixins: ComponentOptions[]
|
713 | 728 | ) {
|
714 |
| - callHookFromMixins(name, globalMixins, ctx) |
715 |
| - |
| 729 | + callHookFromMixins(name, type, globalMixins, instance) |
716 | 730 | const { extends: base, mixins } = options
|
717 | 731 | if (base) {
|
718 |
| - callHookFromExtends(name, base, ctx) |
| 732 | + callHookFromExtends(name, type, base, instance) |
719 | 733 | }
|
720 | 734 | if (mixins) {
|
721 |
| - callHookFromMixins(name, mixins, ctx) |
| 735 | + callHookFromMixins(name, type, mixins, instance) |
722 | 736 | }
|
723 | 737 | const selfHook = options[name]
|
724 | 738 | if (selfHook) {
|
725 |
| - selfHook.call(ctx) |
| 739 | + callWithAsyncErrorHandling(selfHook.bind(instance.proxy!), instance, type) |
726 | 740 | }
|
727 | 741 | }
|
728 | 742 |
|
729 | 743 | function callHookFromExtends(
|
730 | 744 | name: 'beforeCreate' | 'created',
|
| 745 | + type: LifecycleHooks, |
731 | 746 | base: ComponentOptions,
|
732 |
| - ctx: ComponentPublicInstance |
| 747 | + instance: ComponentInternalInstance |
733 | 748 | ) {
|
734 | 749 | if (base.extends) {
|
735 |
| - callHookFromExtends(name, base.extends, ctx) |
| 750 | + callHookFromExtends(name, type, base.extends, instance) |
736 | 751 | }
|
737 | 752 | const baseHook = base[name]
|
738 | 753 | if (baseHook) {
|
739 |
| - baseHook.call(ctx) |
| 754 | + callWithAsyncErrorHandling(baseHook.bind(instance.proxy!), instance, type) |
740 | 755 | }
|
741 | 756 | }
|
742 | 757 |
|
743 | 758 | function callHookFromMixins(
|
744 | 759 | name: 'beforeCreate' | 'created',
|
| 760 | + type: LifecycleHooks, |
745 | 761 | mixins: ComponentOptions[],
|
746 |
| - ctx: ComponentPublicInstance |
| 762 | + instance: ComponentInternalInstance |
747 | 763 | ) {
|
748 | 764 | for (let i = 0; i < mixins.length; i++) {
|
749 | 765 | const chainedMixins = mixins[i].mixins
|
750 | 766 | if (chainedMixins) {
|
751 |
| - callHookFromMixins(name, chainedMixins, ctx) |
| 767 | + callHookFromMixins(name, type, chainedMixins, instance) |
752 | 768 | }
|
753 | 769 | const fn = mixins[i][name]
|
754 | 770 | if (fn) {
|
755 |
| - fn.call(ctx) |
| 771 | + callWithAsyncErrorHandling(fn.bind(instance.proxy!), instance, type) |
756 | 772 | }
|
757 | 773 | }
|
758 | 774 | }
|
|
0 commit comments