@@ -2,7 +2,7 @@ import type { Cons } from '../component'
22import type { OptionBuilder } from '../optionBuilder'
33import { obtainSlot , toComponentReverse , excludeNames , getValidNames , optionNullableMemberDecorator } from '../utils'
44
5- export const HookNames = [
5+ export const HookNames : ReadonlyArray < string > = [
66 "beforeCreate" ,
77 "created" ,
88 "beforeMount" ,
@@ -29,46 +29,35 @@ export const decorator = optionNullableMemberDecorator(function (proto: any, nam
2929 map . set ( name , null )
3030} )
3131
32-
3332export function build ( cons : Cons , optionBuilder : OptionBuilder ) {
3433 const slot = obtainSlot ( cons . prototype )
3534 const protoArr = toComponentReverse ( cons . prototype )
36- const map = slot . obtainMap ( 'hooks' )
35+ const hookMap = slot . obtainMap ( 'hooks' )
36+ const methodsMap = slot . obtainMap ( 'methods' )
3737
3838 optionBuilder . hooks ??= { }
3939 optionBuilder . methods ??= { }
4040 const HookFunctions : Record < string , Function > = { }
4141 const MethodFunctions : Record < string , Function > = { }
4242 protoArr . forEach ( proto => {
4343 let names = getValidNames ( proto , ( des , name ) => {
44-
45- if ( name === 'constructor' ) {
46- return false
47- }
48- if ( typeof des . value === 'function' ) {
49-
50- return true
51- }
52- return false
44+ return typeof des . value === 'function' && name !== 'constructor'
5345 } )
5446 names = excludeNames ( names , slot , ( mapName ) => {
5547 //include these names:
5648 //watch, user may call watch method directly
5749 //hooks, user may call hook method directly
5850 //emits, user may have a method name which is same as one of event names
59- return ! [ 'watch' , 'hooks' , 'emits' , 'provide' ] . includes ( mapName )
51+ return ! [ 'methods' , ' watch', 'hooks' , 'emits' , 'provide' ] . includes ( mapName )
6052 } ) ;
6153 names . forEach ( name => {
62- if ( HookNames . includes ( name as any ) || map . has ( name ) ) {
63-
54+ if ( HookNames . includes ( name ) || hookMap . has ( name ) ) {
6455 HookFunctions [ name ] = proto [ name ]
65- }
66- else {
56+ } else {
6757 MethodFunctions [ name ] = proto [ name ]
58+ methodsMap . set ( name , proto [ name ] )
6859 }
6960 } )
70-
71-
7261 } )
7362
7463 Object . assign ( optionBuilder . methods , MethodFunctions )
@@ -83,5 +72,4 @@ export function build(cons: Cons, optionBuilder: OptionBuilder) {
8372 }
8473 }
8574 Object . assign ( optionBuilder . hooks , HookFunctions )
86-
8775}
0 commit comments