11import { OwlError } from "../common/owl_error" ;
22import { getCurrent } from "./component_node" ;
3- import { derived } from "./reactivity/derived" ;
4- import { proxy } from "./reactivity/proxy" ;
53
64let currentPluginManager : PluginManager | null = null ;
75
@@ -10,67 +8,43 @@ export const _getCurrentPluginManager = () => currentPluginManager;
108export interface PluginConstructor {
119 new ( ) : Plugin ;
1210 id : string ;
13- resources : Record < string , any > ;
1411}
1512
1613export class Plugin {
1714 static id : string = "" ;
1815
19- // can define the type of resources, and some information, such as, is the
20- // resource global or not
21- static resources = { } ;
22-
23- resources : Record < string , any > = { } ;
24-
2516 setup ( ) { }
2617}
2718
2819export class PluginManager {
2920 private children : PluginManager [ ] = [ ] ;
3021 private parent : PluginManager | null ;
3122 private plugins : Record < string , Plugin > ;
32- private resources : Record < string , any > ;
33-
34- onDestroyCb : Function [ ] = [ ] ;
23+ private onDestroyCb : Function [ ] = [ ] ;
3524
3625 constructor ( parent : PluginManager | null ) {
3726 this . parent = parent ;
3827 this . parent ?. children . push ( this ) ;
3928 this . plugins = this . parent ? Object . create ( this . parent . plugins ) : { } ;
40- this . resources = this . parent ? Object . create ( this . parent . resources ) : { } ;
4129 }
4230
4331 destroy ( ) {
4432 for ( let children of this . children ) {
4533 children . destroy ( ) ;
4634 }
4735
48- const plugins : Plugin [ ] = [ ] ;
49- for ( let id in this . plugins ) {
50- if ( this . plugins . hasOwnProperty ( id ) ) {
51- const plugin = this . plugins [ id ] ;
52- // resources
53- for ( let r in plugin . resources ) {
54- delete this . resources [ r ] . sources [ id ] ;
55- }
56-
57- plugins . push ( this . plugins [ id ] ) ;
58- delete this . plugins [ id ] ;
59- }
60- }
61-
6236 const cbs = this . onDestroyCb ;
6337 while ( cbs . length ) {
6438 cbs . pop ( ) ! ( ) ;
6539 }
6640 }
6741
68- getPlugin < T extends Plugin > ( name : string ) : T | null {
69- return ( this . plugins [ name ] as T ) || null ;
42+ getPluginById < T extends Plugin > ( id : string ) : T | null {
43+ return ( this . plugins [ id ] as T ) || null ;
7044 }
7145
72- getResource ( name : string ) : any [ ] {
73- return this . resources [ name ] . fn ( ) ;
46+ getPlugin < T extends PluginConstructor > ( pluginType : T ) : InstanceType < T > | null {
47+ return this . getPluginById < InstanceType < T > > ( pluginType . id ) ;
7448 }
7549
7650 startPlugins ( pluginTypes : PluginConstructor [ ] ) : Plugin [ ] {
@@ -88,43 +62,11 @@ export class PluginManager {
8862 continue ;
8963 }
9064
91- for ( let r in pluginType . resources ) {
92- const sources : { [ key : string ] : Plugin } = proxy ( { } ) ;
93- const fn = derived ( ( ) => {
94- const result = [ ] ;
95- for ( let name in sources ) {
96- const plugin = sources [ name ] ;
97- const value = plugin . resources [ r ] ;
98- if ( Array . isArray ( value ) ) {
99- result . push ( ...value ) ;
100- } else {
101- result . push ( value ) ;
102- }
103- }
104- return result ;
105- } ) ;
106- this . resources [ r ] = { sources, fn } ;
107- }
108-
10965 const plugin = new pluginType ( ) ;
11066 this . plugins [ pluginType . id ] = plugin ;
11167 plugins . push ( plugin ) ;
11268 }
11369
114- // aggregate resources
115- for ( let name in this . plugins ) {
116- const p = this . plugins [ name ] ;
117- for ( let r in p . resources ) {
118- this . resources [ r ] . sources [ name ] = p ;
119- // const value = p.resources[r];
120- // if (Array.isArray(value)) {
121- // this.resources[r].push(...value);
122- // } else {
123- // this.resources[r].push(value);
124- // }
125- }
126- }
127-
12870 // setup phase
12971 for ( let p of plugins ) {
13072 p . setup ( ) ;
@@ -139,11 +81,11 @@ export function plugin<T extends PluginConstructor>(pluginType: T): InstanceType
13981 // getCurrent will throw if we're not in a component
14082 const manager = currentPluginManager || getCurrent ( ) . pluginManager ;
14183
142- let plugin = manager . getPlugin < InstanceType < T > > ( pluginType . id ) ;
84+ let plugin = manager . getPluginById < InstanceType < T > > ( pluginType . id ) ;
14385 if ( ! plugin ) {
14486 if ( manager === currentPluginManager ) {
14587 manager . startPlugins ( [ pluginType ] ) ;
146- plugin = manager . getPlugin < InstanceType < T > > ( pluginType . id ) ! ;
88+ plugin = manager . getPluginById < InstanceType < T > > ( pluginType . id ) ! ;
14789 } else {
14890 throw new OwlError ( `Unknown plugin "${ pluginType . id } "` ) ;
14991 }
0 commit comments