-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAbstractSplitsCacheSync.ts
More file actions
95 lines (75 loc) · 3.24 KB
/
AbstractSplitsCacheSync.ts
File metadata and controls
95 lines (75 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { ISplitsCacheSync } from './types';
import { ISplit } from '../dtos/types';
import { objectAssign } from '../utils/lang/objectAssign';
import { IN_SEGMENT, IN_LARGE_SEGMENT } from '../utils/constants';
/**
* This class provides a skeletal implementation of the ISplitsCacheSync interface
* to minimize the effort required to implement this interface.
*/
export abstract class AbstractSplitsCacheSync implements ISplitsCacheSync {
protected abstract setChangeNumber(changeNumber: number): boolean | void
protected abstract addSplit(split: ISplit): boolean
protected abstract removeSplit(name: string): boolean
update(addedFFs: ISplit[], removedFFs: ISplit[], changeNumber: number): boolean {
this.setChangeNumber(changeNumber);
const updated = addedFFs.map(addedFF => this.addSplit(addedFF)).some(result => result);
return removedFFs.map(removedFF => this.removeSplit(removedFF.name)).some(result => result) || updated;
}
abstract getSplit(name: string): ISplit | null
getSplits(names: string[]): Record<string, ISplit | null> {
const splits: Record<string, ISplit | null> = {};
names.forEach(name => {
splits[name] = this.getSplit(name);
});
return splits;
}
abstract getChangeNumber(): number
getAll(): ISplit[] {
return this.getSplitNames().map(key => this.getSplit(key) as ISplit);
}
abstract getSplitNames(): string[]
abstract trafficTypeExists(trafficType: string): boolean
abstract usesSegments(): boolean
abstract clear(): void
/**
* Check if the splits information is already stored in cache. This data can be preloaded.
* It is used as condition to emit SDK_SPLITS_CACHE_LOADED, and then SDK_READY_FROM_CACHE.
*/
checkCache(): boolean {
return false;
}
/**
* Kill `name` split and set `defaultTreatment` and `changeNumber`.
* Used for SPLIT_KILL push notifications.
*
* @returns `true` if the operation successed updating the split, or `false` if no split is updated,
* for instance, if the `changeNumber` is old, or if the split is not found (e.g., `/splitchanges` hasn't been fetched yet), or if the storage fails to apply the update.
*/
killLocally(name: string, defaultTreatment: string, changeNumber: number): boolean {
const split = this.getSplit(name);
if (split && (!split.changeNumber || split.changeNumber < changeNumber)) {
const newSplit = objectAssign({}, split);
newSplit.killed = true;
newSplit.defaultTreatment = defaultTreatment;
newSplit.changeNumber = changeNumber;
return this.addSplit(newSplit);
}
return false;
}
abstract getNamesByFlagSets(flagSets: string[]): Set<string>[]
}
/**
* Given a parsed split, it returns a boolean flagging if its conditions use segments matchers (rules & whitelists).
* This util is intended to simplify the implementation of `splitsCache::usesSegments` method
*/
export function usesSegments(split: ISplit) {
const conditions = split.conditions || [];
for (let i = 0; i < conditions.length; i++) {
const matchers = conditions[i].matcherGroup.matchers;
for (let j = 0; j < matchers.length; j++) {
const matcher = matchers[j].matcherType;
if (matcher === IN_SEGMENT || matcher === IN_LARGE_SEGMENT) return true;
}
}
return false;
}