@@ -15,13 +15,13 @@ import { CustomRecords } from './custom/custom'
1515import type { SetupContext } from 'vue' ;
1616import type { OptionBuilder } from './optionBuilder'
1717import type { VueCons } from './index'
18+ import * as DecoratorCompatible from './deco3/utils'
1819export type Cons = VueCons
1920
2021type SetupFunction < T > = ( this : void , props : Readonly < any > , ctx : SetupContext < any > ) => T | Promise < T >
2122export type OptionSetupFunction = SetupFunction < any >
2223export type ComponentSetupFunction = SetupFunction < Record < string , any > >
2324function ComponentOption ( cons : Cons , extend ?: any ) {
24-
2525 const optionBuilder : OptionBuilder = { }
2626 optionSetup ( cons , optionBuilder )
2727 optionVModel ( cons , optionBuilder )
@@ -38,6 +38,7 @@ function ComponentOption(cons: Cons, extend?: any) {
3838 const setupFunction : OptionSetupFunction | undefined = optionBuilder . setup ? function ( props , ctx ) {
3939 return optionBuilder . setup ! ( props , ctx )
4040 } : undefined
41+
4142 const raw = {
4243 setup : setupFunction ,
4344 data ( ) {
@@ -89,33 +90,40 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
8990 option . emits = emits
9091
9192
93+
9294 CustomRecords . forEach ( rec => {
9395 rec . creator . apply ( { } , [ option , rec . key ] )
9496 } )
9597
96- if ( arg . setup ) {
97- if ( ! option . setup ) {
98- option . setup = arg . setup
99- } else {
100- const oldSetup : OptionSetupFunction = option . setup
101- const newSetup : ComponentSetupFunction = arg . setup
102-
103- const setup : ComponentSetupFunction = function ( props , ctx ) {
104- const newRet = newSetup ( props , ctx )
105- const oldRet = oldSetup ( props , ctx )
106- if ( oldRet instanceof Promise || newRet instanceof Promise ) {
107- return Promise . all ( [ newRet , oldRet ] ) . then ( ( arr ) => {
108- return Object . assign ( { } , arr [ 0 ] , arr [ 1 ] )
109- } )
110- } else {
111-
112- return Object . assign ( { } , newRet , oldRet )
113- }
11498
99+ arg . setup ??= function ( ) { return { } }
100+
101+ if ( ! option . setup ) {
102+
103+ option . setup = arg . setup
104+ } else {
105+
106+ const oldSetup : OptionSetupFunction = option . setup
107+ const newSetup : ComponentSetupFunction = arg . setup
108+
109+ const setup : ComponentSetupFunction = function ( props , ctx ) {
110+ const newRet = newSetup ( props , ctx )
111+ const oldRet = oldSetup ( props , ctx )
112+ if ( oldRet instanceof Promise || newRet instanceof Promise ) {
113+ return Promise . all ( [ newRet , oldRet ] ) . then ( ( arr ) => {
114+ const ret = Object . assign ( { } , arr [ 0 ] , arr [ 1 ] )
115+ return ret
116+ } )
117+ } else {
118+
119+ const ret = Object . assign ( { } , newRet , oldRet )
120+ return ret
115121 }
116- option . setup = setup
122+
117123 }
124+ option . setup = setup
118125 }
126+
119127 if ( arg . options ) {
120128 Object . assign ( option , arg . options )
121129 }
@@ -138,27 +146,36 @@ function build(cons: Cons, option: ComponentOption) {
138146 }
139147 const component = buildComponent ( cons , option , superSlot === null ? undefined : superSlot . cachedVueComponent )
140148 component . __vfdConstructor = cons
141- slot . cachedVueComponent = component
142-
149+ slot . cachedVueComponent = component ;
150+ ( cons as any ) . __vccOpts = component
143151}
144- function _Component ( arg : ComponentConsOption , cb : ( cons : Cons , option : ComponentOption ) => any ) {
152+ function _Component ( cb : ( cons : Cons , option : ComponentOption ) => any , arg : ComponentConsOption , ctx ?: ClassDecoratorContext ) {
145153 if ( typeof arg === 'function' ) {
146- return cb ( arg , { } )
154+ return DecoratorCompatible . compatibleClassDecorator ( function ( cons : Cons ) {
155+ return cb ( cons , { } )
156+ } ) ( arg , ctx )
147157 }
148- return function ( cons : Cons ) {
158+ return DecoratorCompatible . compatibleClassDecorator ( function ( cons : Cons ) {
149159 return cb ( cons , arg )
150- }
160+ } )
151161}
152- export function ComponentBase ( arg : ComponentConsOption ) : any {
153- return _Component ( arg , function ( cons : Cons , option : ComponentOption ) {
162+ export function ComponentBase ( arg : ComponentConsOption , ctx ?: ClassDecoratorContext ) : any {
163+ return _Component ( function ( cons : Cons , option : ComponentOption ) {
154164 build ( cons , option )
155165 return cons
156- } )
166+ } , arg , ctx )
157167}
158168
159- export function Component ( arg : ComponentConsOption ) : any {
160- return _Component ( arg , function ( cons : Cons , option : ComponentOption ) {
161- build ( cons , option )
162- return obtainSlot ( cons . prototype ) . cachedVueComponent
163- } )
169+ export const Component = ComponentBase
170+
171+ export function toNative < T extends Cons > ( cons : T ) : T {
172+ const slot = obtainSlot ( cons . prototype )
173+ if ( ! slot . inComponent ) {
174+ throw 'to native 1'
175+ }
176+ const cached = slot . cachedVueComponent
177+ if ( ! cached ) {
178+ throw 'to native 2'
179+ }
180+ return cached
164181}
0 commit comments