@@ -14,13 +14,13 @@ import { build as optionAccessor } from './option/accessor'
1414import type { SetupContext } from 'vue' ;
1515import type { OptionBuilder } from './optionBuilder'
1616import type { VueCons } from './index'
17+ import * as DecoratorCompatible from './deco3/utils'
1718export type Cons = VueCons
1819
1920type SetupFunction < T > = ( this : void , props : Readonly < any > , ctx : SetupContext < any > ) => T | Promise < T >
2021export type OptionSetupFunction = SetupFunction < any >
2122export type ComponentSetupFunction = SetupFunction < Record < string , any > >
2223function ComponentOption ( cons : Cons , extend ?: any ) {
23-
2424 const optionBuilder : OptionBuilder = { }
2525 optionSetup ( cons , optionBuilder )
2626 optionVModel ( cons , optionBuilder )
@@ -36,6 +36,7 @@ function ComponentOption(cons: Cons, extend?: any) {
3636 const setupFunction : OptionSetupFunction | undefined = optionBuilder . setup ? function ( props , ctx ) {
3737 return optionBuilder . setup ! ( props , ctx )
3838 } : undefined
39+
3940 const raw = {
4041 setup : setupFunction ,
4142 data ( ) {
@@ -85,29 +86,34 @@ function buildComponent(cons: Cons, arg: ComponentOption, extend?: any): any {
8586 emits = Array . from ( new Set ( [ ...emits , ...arg . emits ] ) )
8687 }
8788 option . emits = emits
88- if ( arg . setup ) {
89- if ( ! option . setup ) {
90- option . setup = arg . setup
91- } else {
92- const oldSetup : OptionSetupFunction = option . setup
93- const newSetup : ComponentSetupFunction = arg . setup
89+ arg . setup ??= function ( ) { return { } }
9490
95- const setup : ComponentSetupFunction = function ( props , ctx ) {
96- const newRet = newSetup ( props , ctx )
97- const oldRet = oldSetup ( props , ctx )
98- if ( oldRet instanceof Promise || newRet instanceof Promise ) {
99- return Promise . all ( [ newRet , oldRet ] ) . then ( ( arr ) => {
100- return Object . assign ( { } , arr [ 0 ] , arr [ 1 ] )
101- } )
102- } else {
91+ if ( ! option . setup ) {
10392
104- return Object . assign ( { } , newRet , oldRet )
105- }
93+ option . setup = arg . setup
94+ } else {
95+
96+ const oldSetup : OptionSetupFunction = option . setup
97+ const newSetup : ComponentSetupFunction = arg . setup
10698
99+ const setup : ComponentSetupFunction = function ( props , ctx ) {
100+ const newRet = newSetup ( props , ctx )
101+ const oldRet = oldSetup ( props , ctx )
102+ if ( oldRet instanceof Promise || newRet instanceof Promise ) {
103+ return Promise . all ( [ newRet , oldRet ] ) . then ( ( arr ) => {
104+ const ret = Object . assign ( { } , arr [ 0 ] , arr [ 1 ] )
105+ return ret
106+ } )
107+ } else {
108+
109+ const ret = Object . assign ( { } , newRet , oldRet )
110+ return ret
107111 }
108- option . setup = setup
112+
109113 }
114+ option . setup = setup
110115 }
116+
111117 if ( arg . options ) {
112118 Object . assign ( option , arg . options )
113119 }
@@ -133,24 +139,33 @@ function build(cons: Cons, option: ComponentOption) {
133139 slot . cachedVueComponent = component
134140
135141}
136- function _Component ( arg : ComponentConsOption , cb : ( cons : Cons , option : ComponentOption ) => any ) {
142+ function _Component ( cb : ( cons : Cons , option : ComponentOption ) => any , arg : ComponentConsOption , ctx ?: ClassDecoratorContext ) {
137143 if ( typeof arg === 'function' ) {
138- return cb ( arg , { } )
144+ return DecoratorCompatible . compatibleClassDecorator ( function ( cons : Cons ) {
145+ return cb ( cons , { } )
146+ } ) ( arg , ctx )
139147 }
140- return function ( cons : Cons ) {
148+ return DecoratorCompatible . compatibleClassDecorator ( function ( cons : Cons ) {
141149 return cb ( cons , arg )
142- }
150+ } )
143151}
144- export function ComponentBase ( arg : ComponentConsOption ) : any {
145- return _Component ( arg , function ( cons : Cons , option : ComponentOption ) {
152+ export function ComponentBase ( arg : ComponentConsOption , ctx ?: ClassDecoratorContext ) : any {
153+ return _Component ( function ( cons : Cons , option : ComponentOption ) {
146154 build ( cons , option )
147155 return cons
148- } )
156+ } , arg , ctx )
149157}
150158
151- export function Component ( arg : ComponentConsOption ) : any {
152- return _Component ( arg , function ( cons : Cons , option : ComponentOption ) {
153- build ( cons , option )
154- return obtainSlot ( cons . prototype ) . cachedVueComponent
155- } )
159+ export const Component = ComponentBase
160+
161+ export function toNative < T extends Cons > ( cons : T ) : T {
162+ const slot = obtainSlot ( cons . prototype )
163+ if ( ! slot . inComponent ) {
164+ throw 'to native 1'
165+ }
166+ const cached = slot . cachedVueComponent
167+ if ( ! cached ) {
168+ throw 'to native 2'
169+ }
170+ return cached
156171}
0 commit comments