11import { OwlError } from "../common/owl_error" ;
22import { version } from "../version" ;
3- import { Component , ComponentConstructor , Props } from "./component" ;
3+ import { Component , ComponentConstructor } from "./component" ;
44import { ComponentNode , saveCurrent } from "./component_node" ;
55import { handleError , nodeErrorHandlers } from "./rendering/error_handling" ;
66import { Fiber , MountOptions , RootFiber } from "./rendering/fibers" ;
@@ -9,21 +9,20 @@ import { proxy, toRaw } from "./reactivity/proxy";
99import { Scheduler } from "./rendering/scheduler" ;
1010import { TemplateSet , TemplateSetConfig } from "./template_set" ;
1111import { validateTarget } from "./utils" ;
12+ import { GetProps } from "./props" ;
1213
1314// reimplement dev mode stuff see last change in 0f7a8289a6fb8387c3c1af41c6664b2a8448758f
1415
15- export interface Env {
16- [ key : string ] : any ;
17- }
16+ type ComponentInstance < C extends ComponentConstructor > = C extends new ( ... args : any ) => infer T
17+ ? T
18+ : never ;
1819
19- interface RootConfig < P , E > {
20- env ?: E ;
20+ interface RootConfig < P > {
2121 pluginManager ?: PluginManager ;
2222 props ?: P ;
2323}
2424
25- export interface AppConfig < E > extends TemplateSetConfig {
26- env ?: E ;
25+ export interface AppConfig extends TemplateSetConfig {
2726 name ?: string ;
2827 pluginManager ?: PluginManager ;
2928 test ?: boolean ;
@@ -47,27 +46,26 @@ declare global {
4746
4847type MountTarget = HTMLElement | ShadowRoot ;
4948
50- interface Root < P extends Props , E , T extends abstract new ( ... args : any ) => any = any > {
51- node : ComponentNode < P , E > ;
52- promise : Promise < any > ;
53- mount ( target : MountTarget , options ?: MountOptions ) : Promise < Component < P , E > & InstanceType < T > > ;
49+ interface Root < T extends ComponentConstructor > {
50+ node : ComponentNode ;
51+ promise : Promise < ComponentInstance < T > > ;
52+ mount ( target : MountTarget , options ?: MountOptions ) : Promise < ComponentInstance < T > > ;
5453 destroy ( ) : void ;
5554}
5655
5756window . __OWL_DEVTOOLS__ ||= { apps, Fiber, RootFiber, toRaw, proxy } ;
5857
59- export class App < E = any > extends TemplateSet {
58+ export class App extends TemplateSet {
6059 static validateTarget = validateTarget ;
6160 static apps = apps ;
6261 static version = version ;
6362
6463 name : string ;
65- env : E ;
6664 scheduler = new Scheduler ( ) ;
67- roots : Set < Root < any , any > > = new Set ( ) ;
65+ roots : Set < Root < any > > = new Set ( ) ;
6866 pluginManager : PluginManager ;
6967
70- constructor ( config : AppConfig < E > = { } ) {
68+ constructor ( config : AppConfig = { } ) {
7169 super ( config ) ;
7270 this . name = config . name || "" ;
7371 apps . add ( this ) ;
@@ -79,31 +77,18 @@ export class App<E = any> extends TemplateSet {
7977 console . info ( `Owl is running in 'dev' mode.` ) ;
8078 hasBeenLogged = true ;
8179 }
82- const env = config . env || { } ;
83- const descrs = Object . getOwnPropertyDescriptors ( env ) ;
84- this . env = Object . freeze ( Object . create ( Object . getPrototypeOf ( env ) , descrs ) ) ;
8580 }
8681
87- createRoot < Props extends object , SubEnv = any > (
88- Root : ComponentConstructor < Props , E > ,
89- config : RootConfig < Props , SubEnv > = { }
90- ) : Root < Props , SubEnv > {
91- const props = config . props || ( { } as Props ) ;
92- // hack to make sure the sub root get the sub env if necessary. for owl 3,
93- // would be nice to rethink the initialization process to make sure that
94- // we can create a ComponentNode and give it explicitely the env, instead
95- // of looking it up in the app
96- const env = this . env ;
97- if ( config . env ) {
98- this . env = config . env as any ;
99- }
82+ createRoot < T extends ComponentConstructor > (
83+ Root : T ,
84+ config : RootConfig < GetProps < ComponentInstance < T > > > = { }
85+ ) : Root < T > {
86+ const props = config . props || ( { } as any ) ;
10087
10188 const restore = saveCurrent ( ) ;
10289 const node = this . makeNode ( Root , props ) ;
10390 restore ( ) ;
104- if ( config . env ) {
105- this . env = env ;
106- }
91+
10792 let resolve ! : ( value : any ) => void ;
10893 let reject ! : ( reason ?: any ) => void ;
10994 const promise = new Promise < any > ( ( res , rej ) => {
@@ -128,7 +113,10 @@ export class App<E = any> extends TemplateSet {
128113 return root ;
129114 }
130115
131- makeNode ( Component : ComponentConstructor , props : any ) : ComponentNode {
116+ makeNode < T extends ComponentConstructor > (
117+ Component : T ,
118+ props : GetProps < ComponentInstance < T > >
119+ ) : ComponentNode {
132120 return new ComponentNode ( Component , props , this , null , null ) ;
133121 }
134122
@@ -169,20 +157,20 @@ export class App<E = any> extends TemplateSet {
169157 apps . delete ( this ) ;
170158 }
171159
172- createComponent < P extends Props > (
160+ createComponent < P extends Record < string , any > > (
173161 name : string | null ,
174162 isStatic : boolean ,
175163 hasSlotsProp : boolean ,
176164 hasDynamicPropList : boolean ,
177165 propList : string [ ]
178166 ) {
179167 const isDynamic = ! isStatic ;
180- let arePropsDifferent : ( p1 : Object , p2 : Object ) => boolean ;
168+ let arePropsDifferent : ( p1 : P , p2 : P ) => boolean ;
181169 const hasNoProp = propList . length === 0 ;
182170 if ( hasSlotsProp ) {
183171 arePropsDifferent = ( _1 , _2 ) => true ;
184172 } else if ( hasDynamicPropList ) {
185- arePropsDifferent = function ( props1 : Props , props2 : Props ) {
173+ arePropsDifferent = function ( props1 : P , props2 : P ) {
186174 for ( let k in props1 ) {
187175 if ( props1 [ k ] !== props2 [ k ] ) {
188176 return true ;
@@ -193,7 +181,7 @@ export class App<E = any> extends TemplateSet {
193181 } else if ( hasNoProp ) {
194182 arePropsDifferent = ( _1 : any , _2 : any ) => false ;
195183 } else {
196- arePropsDifferent = function ( props1 : Props , props2 : Props ) {
184+ arePropsDifferent = function ( props1 : P , props2 : P ) {
197185 for ( let p of propList ) {
198186 if ( props1 [ p ] !== props2 [ p ] ) {
199187 return true ;
@@ -250,20 +238,10 @@ export class App<E = any> extends TemplateSet {
250238 }
251239}
252240
253- type ComponentInstance < C extends ComponentConstructor < any , any > > = C extends new (
254- ...args : any
255- ) => infer T
256- ? T
257- : never ;
258-
259- export async function mount <
260- T extends ComponentConstructor < any , any > ,
261- P extends object = any ,
262- E = any
263- > (
264- C : T & ComponentConstructor < P , E > ,
241+ export async function mount < T extends ComponentConstructor > (
242+ C : T ,
265243 target : MountTarget ,
266- config : AppConfig < E > & RootConfig < P , E > & MountOptions = { }
244+ config : AppConfig & RootConfig < GetProps < ComponentInstance < T > > > & MountOptions = { }
267245) : Promise < ComponentInstance < T > > {
268246 const app = new App ( config ) ;
269247 const root = app . createRoot ( C , config ) ;
0 commit comments