1
- import { IRBSegment , MaybeThenable } from '../../dtos/types' ;
1
+ import { IExcludedSegment , IRBSegment , MaybeThenable } from '../../dtos/types' ;
2
2
import { IStorageAsync , IStorageSync } from '../../storages/types' ;
3
3
import { ILogger } from '../../logger/types' ;
4
4
import { IDependencyMatcherValue , ISplitEvaluator } from '../types' ;
@@ -11,6 +11,7 @@ import { STANDARD_SEGMENT, RULE_BASED_SEGMENT, LARGE_SEGMENT } from '../../utils
11
11
export function ruleBasedSegmentMatcherContext ( segmentName : string , storage : IStorageSync | IStorageAsync , log : ILogger ) {
12
12
13
13
return function ruleBasedSegmentMatcher ( { key, attributes } : IDependencyMatcherValue , splitEvaluator : ISplitEvaluator ) : MaybeThenable < boolean > {
14
+ const matchingKey = getMatching ( key ) ;
14
15
15
16
function matchConditions ( rbsegment : IRBSegment ) {
16
17
const conditions = rbsegment . conditions || [ ] ;
@@ -30,28 +31,29 @@ export function ruleBasedSegmentMatcherContext(segmentName: string, storage: ISt
30
31
evaluation ? true : false ;
31
32
}
32
33
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
+
33
44
function isExcluded ( rbSegment : IRBSegment ) {
34
- const matchingKey = getMatching ( key ) ;
35
45
const excluded = rbSegment . excluded || { } ;
36
46
37
47
if ( excluded . keys && excluded . keys . indexOf ( matchingKey ) !== - 1 ) return true ;
38
48
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 ) ;
52
54
}
53
55
54
- function isInSegment ( rbSegment : IRBSegment | null ) {
56
+ function isInRBSegment ( rbSegment : IRBSegment | null ) {
55
57
if ( ! rbSegment ) return false ;
56
58
const excluded = isExcluded ( rbSegment ) ;
57
59
@@ -63,7 +65,7 @@ export function ruleBasedSegmentMatcherContext(segmentName: string, storage: ISt
63
65
const rbSegment = storage . rbSegments . get ( segmentName ) ;
64
66
65
67
return thenable ( rbSegment ) ?
66
- rbSegment . then ( isInSegment ) :
67
- isInSegment ( rbSegment ) ;
68
+ rbSegment . then ( isInRBSegment ) :
69
+ isInRBSegment ( rbSegment ) ;
68
70
} ;
69
71
}
0 commit comments