Skip to content

Commit a95edb9

Browse files
refactor type definitions
1 parent b937db5 commit a95edb9

File tree

3 files changed

+44
-39
lines changed

3 files changed

+44
-39
lines changed

src/storages/dataLoader.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,44 @@ import { getMatching } from '../utils/key';
55
import { IMembershipsResponse, IMySegmentsResponse, IRBSegment, ISplit } from '../dtos/types';
66
import { ILogger } from '../logger/types';
77

8+
export type RolloutPlan = {
9+
/**
10+
* Change number of feature flags.
11+
*/
12+
since: number;
13+
/**
14+
* List of feature flags.
15+
*/
16+
flags: ISplit[];
17+
/**
18+
* Change number of rule-based segments.
19+
*/
20+
rbSince?: number;
21+
/**
22+
* List of rule-based segments.
23+
*/
24+
rbSegments?: IRBSegment[];
25+
/**
26+
* Optional map of user keys to their memberships.
27+
*/
28+
memberships?: {
29+
[key: string]: IMembershipsResponse;
30+
};
31+
/**
32+
* Optional map of standard segments to their list of keys.
33+
* This property is ignored if `memberships` is provided.
34+
*/
35+
segments?: {
36+
[segmentName: string]: string[];
37+
};
38+
};
39+
840
/**
941
* Sets the given synchronous storage with the provided rollout plan snapshot.
1042
* If `matchingKey` is provided, the storage is handled as a client-side storage (segments and largeSegments are instances of MySegmentsCache).
1143
* Otherwise, the storage is handled as a server-side storage (segments is an instance of SegmentsCache).
1244
*/
13-
export function setRolloutPlan(log: ILogger, rolloutPlan: SplitIO.RolloutPlan, storage: { splits?: ISplitsCacheSync, rbSegments?: IRBSegmentsCacheSync, segments: ISegmentsCacheSync, largeSegments?: ISegmentsCacheSync }, matchingKey?: string) {
45+
export function setRolloutPlan(log: ILogger, rolloutPlan: RolloutPlan, storage: { splits?: ISplitsCacheSync, rbSegments?: IRBSegmentsCacheSync, segments: ISegmentsCacheSync, largeSegments?: ISegmentsCacheSync }, matchingKey?: string) {
1446
// Do not load data if current rollout plan is empty
1547
if (Object.keys(rolloutPlan).length === 0) return;
1648

@@ -20,12 +52,12 @@ export function setRolloutPlan(log: ILogger, rolloutPlan: SplitIO.RolloutPlan, s
2052

2153
if (splits) {
2254
splits.clear();
23-
splits.update(rolloutPlan.flags as ISplit[] || [], [], rolloutPlan.since || -1);
55+
splits.update(rolloutPlan.flags || [], [], rolloutPlan.since || -1);
2456
}
2557

2658
if (rbSegments) {
2759
rbSegments.clear();
28-
rbSegments.update(rolloutPlan.rbSegments as IRBSegment[] || [], [], rolloutPlan.rbSince || -1);
60+
rbSegments.update(rolloutPlan.rbSegments || [], [], rolloutPlan.rbSince || -1);
2961
}
3062

3163
const segmentsData = rolloutPlan.segments || {};
@@ -43,8 +75,8 @@ export function setRolloutPlan(log: ILogger, rolloutPlan: SplitIO.RolloutPlan, s
4375
}
4476

4577
if (memberships) {
46-
if ((memberships as IMembershipsResponse).ms) segments.resetSegments((memberships as IMembershipsResponse).ms!);
47-
if ((memberships as IMembershipsResponse).ls && largeSegments) largeSegments.resetSegments((memberships as IMembershipsResponse).ls!);
78+
if (memberships.ms) segments.resetSegments(memberships.ms!);
79+
if (memberships.ls && largeSegments) largeSegments.resetSegments(memberships.ls!);
4880
}
4981
} else { // add segments data (server-side)
5082
Object.keys(segmentsData).forEach(segmentName => {
@@ -59,7 +91,7 @@ export function setRolloutPlan(log: ILogger, rolloutPlan: SplitIO.RolloutPlan, s
5991
* If `keys` are provided, the memberships for those keys is returned, to protect segments data.
6092
* Otherwise, the segments data is returned.
6193
*/
62-
export function getRolloutPlan(log: ILogger, storage: IStorageSync, keys?: SplitIO.SplitKey[]): SplitIO.RolloutPlan {
94+
export function getRolloutPlan(log: ILogger, storage: IStorageSync, keys?: SplitIO.SplitKey[]): RolloutPlan {
6395

6496
log.debug(`storage: get feature flags and segments${keys ? ` for keys ${keys}` : ''}`);
6597

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import SplitIO from '../types/splitio';
22
import { ISplitFiltersValidation } from './dtos/types';
33
import { ILogger } from './logger/types';
4+
import { RolloutPlan } from './storages/dataLoader';
45

56
/**
67
* SplitIO.ISettings interface extended with private properties for internal use
@@ -10,6 +11,7 @@ export interface ISettings extends SplitIO.ISettings {
1011
__splitFiltersValidation: ISplitFiltersValidation;
1112
};
1213
readonly log: ILogger;
14+
readonly initialRolloutPlan?: RolloutPlan;
1315
}
1416

1517
/**

types/splitio.d.ts

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,8 @@ interface IClientSideSyncSharedSettings extends IClientSideSharedSettings, ISync
351351
*/
352352
features?: SplitIO.MockedFeaturesMap;
353353
/**
354-
* Data to initialize the SDK storage with. If provided and valid, the SDK will be ready from cache immediately.
354+
* Rollout plan object (i.e., feature flags and segment definitions) to initialize the SDK storage with. If provided and valid, the SDK will be ready from cache immediately.
355+
* This object is derived from calling the Node.js SDK’s `getRolloutPlan` method.
355356
*/
356357
initialRolloutPlan?: SplitIO.RolloutPlan;
357358
/**
@@ -1027,39 +1028,9 @@ declare namespace SplitIO {
10271028
options?: Object;
10281029
};
10291030
/**
1030-
* Defines the format of rollout plan data to preload the SDK cache.
1031+
* A JSON-serializable plain object that defines the format of rollout plan data to preload the SDK cache with feature flags and segments.
10311032
*/
1032-
type RolloutPlan = {
1033-
/**
1034-
* Change number of feature flags.
1035-
*/
1036-
since: number;
1037-
/**
1038-
* List of feature flags.
1039-
*/
1040-
flags: Object[];
1041-
/**
1042-
* Change number of rule-based segments.
1043-
*/
1044-
rbSince?: number;
1045-
/**
1046-
* List of rule-based segments.
1047-
*/
1048-
rbSegments?: Object[];
1049-
/**
1050-
* Optional map of user keys to their memberships.
1051-
*/
1052-
memberships?: {
1053-
[key: string]: Object;
1054-
};
1055-
/**
1056-
* Optional map of segments to their list of keys.
1057-
* This property is ignored if `memberships` is provided.
1058-
*/
1059-
segments?: {
1060-
[segmentName: string]: string[];
1061-
};
1062-
};
1033+
type RolloutPlan = Object;
10631034
/**
10641035
* Impression listener interface. This is the interface that needs to be implemented
10651036
* by the element you provide to the SDK as impression listener.

0 commit comments

Comments
 (0)