1- import { IRBSegment , MaybeThenable } from '../../dtos/types' ;
1+ import { IExcludedSegment , IRBSegment , MaybeThenable } from '../../dtos/types' ;
22import { IStorageAsync , IStorageSync } from '../../storages/types' ;
33import { ILogger } from '../../logger/types' ;
44import { IDependencyMatcherValue , ISplitEvaluator } from '../types' ;
@@ -11,6 +11,7 @@ import { STANDARD_SEGMENT, RULE_BASED_SEGMENT, LARGE_SEGMENT } from '../../utils
1111export function ruleBasedSegmentMatcherContext ( segmentName : string , storage : IStorageSync | IStorageAsync , log : ILogger ) {
1212
1313 return function ruleBasedSegmentMatcher ( { key, attributes } : IDependencyMatcherValue , splitEvaluator : ISplitEvaluator ) : MaybeThenable < boolean > {
14+ const matchingKey = getMatching ( key ) ;
1415
1516 function matchConditions ( rbsegment : IRBSegment ) {
1617 const conditions = rbsegment . conditions || [ ] ;
@@ -30,28 +31,29 @@ export function ruleBasedSegmentMatcherContext(segmentName: string, storage: ISt
3031 evaluation ? true : false ;
3132 }
3233
34+ function isInExcludedSegment ( { type, name } : IExcludedSegment ) {
35+ return type === STANDARD_SEGMENT ?
36+ storage . segments . isInSegment ( name , matchingKey ) :
37+ type === RULE_BASED_SEGMENT ?
38+ ruleBasedSegmentMatcherContext ( name , storage , log ) ( { key, attributes } , splitEvaluator ) :
39+ type === LARGE_SEGMENT && ( storage as IStorageSync ) . largeSegments ?
40+ ( storage as IStorageSync ) . largeSegments ! . isInSegment ( name , matchingKey ) :
41+ false ;
42+ }
43+
3344 function isExcluded ( rbSegment : IRBSegment ) {
34- const matchingKey = getMatching ( key ) ;
3545 const excluded = rbSegment . excluded || { } ;
3646
3747 if ( excluded . keys && excluded . keys . indexOf ( matchingKey ) !== - 1 ) return true ;
3848
39- const isInSegment = ( excluded . segments || [ ] ) . map ( ( { type, name } ) => {
40- return type === STANDARD_SEGMENT ?
41- storage . segments . isInSegment ( name , matchingKey ) :
42- type === RULE_BASED_SEGMENT ?
43- ruleBasedSegmentMatcherContext ( name , storage , log ) ( { key, attributes } , splitEvaluator ) :
44- type === LARGE_SEGMENT && ( storage as IStorageSync ) . largeSegments ?
45- ( storage as IStorageSync ) . largeSegments ! . isInSegment ( name , matchingKey ) :
46- false ;
47- } ) ;
48-
49- return isInSegment . length && thenable ( isInSegment [ 0 ] ) ?
50- Promise . all ( isInSegment ) . then ( results => results . some ( result => result ) ) :
51- isInSegment . some ( result => result ) ;
49+ return ( excluded . segments || [ ] ) . reduce < MaybeThenable < boolean > > ( ( result , excludedSegment ) => {
50+ return thenable ( result ) ?
51+ result . then ( result => result || isInExcludedSegment ( excludedSegment ) ) :
52+ result || isInExcludedSegment ( excludedSegment ) ;
53+ } , false ) ;
5254 }
5355
54- function isInSegment ( rbSegment : IRBSegment | null ) {
56+ function isInRBSegment ( rbSegment : IRBSegment | null ) {
5557 if ( ! rbSegment ) return false ;
5658 const excluded = isExcluded ( rbSegment ) ;
5759
@@ -63,7 +65,7 @@ export function ruleBasedSegmentMatcherContext(segmentName: string, storage: ISt
6365 const rbSegment = storage . rbSegments . get ( segmentName ) ;
6466
6567 return thenable ( rbSegment ) ?
66- rbSegment . then ( isInSegment ) :
67- isInSegment ( rbSegment ) ;
68+ rbSegment . then ( isInRBSegment ) :
69+ isInRBSegment ( rbSegment ) ;
6870 } ;
6971}
0 commit comments