1
1
import { reactive } from 'vue' ;
2
2
import { TargetName , PassOnToCallback } from './types' ;
3
3
import { setCurrentHookName , setCurrentHookTarget , Context } from './context' ;
4
- import { SETUP_OPTIONS_NAME , SETUP_NAME } from './config' ;
5
- import { onComputed } from './on- computed' ;
4
+ import { SETUP_OPTIONS_NAME , SETUP_NAME , SETUP_PROPERTY_DESCRIPTOR } from './config' ;
5
+ import { onComputed , initComputed } from './computed' ;
6
6
import { getOptions , getSetupOptions , setOptions } from './options' ;
7
7
import { initDefine } from './define' ;
8
8
import { setupReference } from './setup-reference' ;
9
+ import { getPropertyDescriptors } from './property-descriptors' ;
9
10
10
11
export type TargetConstructor = {
11
12
inject : typeof Context [ 'inject' ]
12
13
setup : typeof Context [ 'setup' ]
13
14
setupOptions : typeof Context [ 'setupOptions' ]
15
+ setupPropertyDescriptor : Map < string , PropertyDescriptor > ;
14
16
new ( ...args : any [ ] ) : any ;
15
17
} ;
16
18
17
- interface Item {
18
- name : TargetName ;
19
- hook : PassOnToCallback ;
20
- }
19
+
21
20
22
21
function initHook < T extends object > ( target : T ) {
23
22
setCurrentHookTarget ( target ) ;
24
- const set = new Set < TargetName > ( ) ;
25
- const options = getOptions ( target . constructor ) ;
26
- const special : Item [ ] = [ ] ;
27
- const plainArr : Item [ ] = [ ] ;
28
- options . forEach ( ( names , hook ) => {
29
- if ( hook !== onComputed ) {
30
- return names . forEach ( ( name ) => {
31
- plainArr . push ( {
32
- hook,
33
- name,
34
- } ) ;
35
- } ) ;
23
+ const Target : TargetConstructor = target . constructor as any ;
24
+ const options = getOptions ( Target ) ;
25
+ const propertyDescriptor = Target . setupPropertyDescriptor ;
26
+
27
+ // bind this
28
+ propertyDescriptor . forEach ( ( { value, writable } , key ) => {
29
+ if ( typeof value === 'function' && writable ) {
30
+ target [ key ] = value . bind ( target ) ;
36
31
}
37
- names . forEach ( ( name ) => {
38
- special . push ( {
39
- hook,
40
- name,
41
- } ) ;
42
- } ) ;
43
- } ) ;
32
+ } )
44
33
45
34
// init props
46
35
if ( target . constructor [ 'setupDefine' ] ) {
47
36
initDefine ( target ) ;
48
37
}
49
38
50
39
// init computed
51
- special . forEach ( ( item ) => {
52
- initName ( item ) ;
53
- } ) ;
40
+ initComputed ( target , propertyDescriptor ) ;
54
41
55
- // init plain hooks
56
- plainArr . forEach ( ( item ) => {
57
- initName ( item ) ;
58
- } ) ;
42
+ // init PassOnTo
43
+ options . forEach ( ( names , hook ) => {
44
+ return names . forEach ( ( name ) => {
45
+ initName ( name , hook ) ;
46
+ } ) ;
47
+ } )
59
48
setCurrentHookTarget ( null ) ;
60
49
61
- function initName ( { name, hook } : Item ) {
62
- if ( ! set . has ( name ) && typeof target [ name ] === 'function' ) {
63
- target [ name ] = target [ name ] . bind ( target ) ;
64
- set . add ( name ) ;
65
- }
50
+ function initName ( name : TargetName , hook : PassOnToCallback ) {
66
51
setCurrentHookName ( name ) ;
67
52
hook ( target [ name ] ) ;
68
53
setCurrentHookName ( null ) ;
@@ -71,17 +56,10 @@ function initHook<T extends object>(target: T) {
71
56
}
72
57
73
58
function Setup < T extends TargetConstructor > ( Target : T ) {
74
- const descriptors = Object . getOwnPropertyDescriptors ( Target . prototype ) ;
75
-
76
- Object . keys ( descriptors ) . filter ( ( k ) => {
77
- const descriptor = descriptors [ k ] ;
78
- if ( descriptor . get ) {
79
- setOptions ( onComputed , k ) ;
80
- }
81
- } ) ;
82
59
class Setup extends Target {
83
60
public static [ SETUP_OPTIONS_NAME ] = getSetupOptions ( Target ) ;
84
61
public static [ SETUP_NAME ] = true ;
62
+ public static [ SETUP_PROPERTY_DESCRIPTOR ] = getPropertyDescriptors ( Target ) ;
85
63
public constructor ( ...args : any [ ] ) {
86
64
setupReference . count ( ) ;
87
65
super ( ...args ) ;
0 commit comments