@@ -93,51 +93,9 @@ export class ComponentStore<T> {
9393 return cached ;
9494 }
9595
96- for ( let p = 0 ; p < fields . length ; p ++ ) {
97- for ( let t = 0 ; t < oTypes . length ; t ++ ) {
98- const exact = this . lookup ( fields [ p ] , oTypes [ t ] , topology ) ;
99- if ( exact !== undefined ) {
100- return this . lookupCache . add ( exact , key ) ;
101- }
102- }
103- }
104-
105- const possibleClasses = this . registeredClasses ( fields , topology ) ;
106- if ( possibleClasses . length === 0 ) {
107- if ( topology === DEFAULT_TOPOLOGY . value ) {
108- return this . lookupCache . add ( null , key ) ;
109- }
110- const foundComponent = this . getRenderComponent (
111- oTypes ,
112- fields ,
113- DEFAULT_TOPOLOGY . value ,
114- defaultType ,
115- ) ;
116- if ( ! foundComponent ) {
117- return this . lookupCache . add ( null , key ) ;
118- }
96+ const match = this . findMatch ( oTypes , fields , topology , defaultType ) ;
11997
120- return this . lookupCache . add ( foundComponent , key ) ;
121- }
122- for ( let i = 0 ; i < fields . length ; i ++ ) {
123- const bestClass = this . bestClass ( possibleClasses , oTypes ) ;
124- const component = bestClass && this . lookup (
125- fields [ i ] ,
126- bestClass ,
127- topology ,
128- ) ;
129- if ( component ) {
130- return this . lookupCache . add ( component , key ) ;
131- }
132- }
133- for ( let i = 0 ; i < fields . length ; i ++ ) {
134- const component = this . lookup ( fields [ i ] , defaultType , topology ) ;
135- if ( component ) {
136- return this . lookupCache . add ( component , key ) ;
137- }
138- }
139-
140- return this . lookupCache . add ( null , key ) ;
98+ return this . lookupCache . add ( match , key ) ;
14199 }
142100
143101 /**
@@ -219,7 +177,90 @@ export class ComponentStore<T> {
219177 return components . find ( ( c ) => chain . indexOf ( c ) > 0 ) ;
220178 }
221179
222- // interface ComponentMapping<T> { [type: string]: { [obj: string]: { [topology: string]: T } }; }
180+ private classMatch ( possibleClasses : Id [ ] , types : Id [ ] , fields : Id [ ] , topology : Id ) : T | undefined {
181+ for ( let i = 0 ; i < fields . length ; i ++ ) {
182+ const bestClass = this . bestClass ( possibleClasses , types ) ;
183+ const component = bestClass && this . lookup (
184+ fields [ i ] ,
185+ bestClass ,
186+ topology ,
187+ ) ;
188+
189+ if ( component ) {
190+ return component ;
191+ }
192+ }
193+
194+ return undefined ;
195+ }
196+
197+ private defaultMatch ( fields : Id [ ] , topology : Id , defaultType : Id ) : T | undefined {
198+ for ( let i = 0 ; i < fields . length ; i ++ ) {
199+ const component = this . lookup ( fields [ i ] , defaultType , topology ) ;
200+ if ( component ) {
201+ return component ;
202+ }
203+ }
204+
205+ return undefined ;
206+ }
207+
208+ private exactMatch ( types : Id [ ] , fields : Id [ ] , topology : Id ) : T | undefined {
209+ for ( let p = 0 ; p < fields . length ; p ++ ) {
210+ for ( let t = 0 ; t < types . length ; t ++ ) {
211+ const exact = this . lookup ( fields [ p ] , types [ t ] , topology ) ;
212+ if ( exact !== undefined ) {
213+ return exact ;
214+ }
215+ }
216+ }
217+
218+ return undefined ;
219+ }
220+
221+ private findMatch ( types : Id [ ] , fields : Id [ ] , topology : Id , defaultType : Id ) : T | null {
222+ let match : T | null | undefined = this . exactMatch ( types , fields , topology ) ;
223+ if ( match !== undefined ) {
224+ return match ;
225+ }
226+
227+ const possibleClasses = this . registeredClasses ( fields , topology ) ;
228+
229+ if ( possibleClasses . length === 0 ) {
230+ return this . noClassesMatch ( types , fields , topology , defaultType ) ;
231+ }
232+
233+ match = this . classMatch ( possibleClasses , types , fields , topology ) ;
234+ if ( match !== undefined ) {
235+ return match ;
236+ }
237+
238+ match = this . defaultMatch ( fields , topology , defaultType ) ;
239+ if ( match !== undefined ) {
240+ return match ;
241+ }
242+
243+ return null ;
244+ }
245+
246+ private noClassesMatch ( types : Id [ ] , fields : Id [ ] , topology : Id , defaultType : Id ) : T | null {
247+ if ( topology === DEFAULT_TOPOLOGY . value ) {
248+ return null ;
249+ }
250+
251+ const foundComponent = this . getRenderComponent (
252+ types ,
253+ fields ,
254+ DEFAULT_TOPOLOGY . value ,
255+ defaultType ,
256+ ) ;
257+
258+ if ( foundComponent ) {
259+ return foundComponent ;
260+ }
261+
262+ return null ;
263+ }
223264
224265 /**
225266 * Returns a list of classes which have registrations for a combination of {fields} and {topology}.
0 commit comments