11import SplitIO from '../../types/splitio' ;
2- import { ISegmentsCacheSync , ISplitsCacheSync , IStorageSync } from './types' ;
2+ import { IRBSegmentsCacheSync , ISegmentsCacheSync , ISplitsCacheSync , IStorageSync } from './types' ;
33import { setToArray } from '../utils/lang/sets' ;
44import { getMatching } from '../utils/key' ;
5- import { IMembershipsResponse , IMySegmentsResponse , ISplit } from '../dtos/types' ;
5+ import { IMembershipsResponse , IMySegmentsResponse , IRBSegment , ISplit } from '../dtos/types' ;
66
77/**
88 *
99 * @param preloadedData - validated data
1010 * @param storage - object containing `splits` and `segments` cache (client-side variant)
1111 * @param userKey - user key (matching key) of the provided MySegmentsCache
1212 *
13+ * @TODO load data even if current data is more recent?
1314 * @TODO extend to load largeSegments
1415 * @TODO extend to load data on shared mySegments storages. Be specific when emitting SDK_READY_FROM_CACHE on shared clients. Maybe the serializer should provide the `useSegments` flag.
1516 * @TODO add logs, and input validation in this module, in favor of size reduction.
1617 * @TODO unit tests
1718 */
18- export function loadData ( preloadedData : SplitIO . PreloadedData , storage : { splits ?: ISplitsCacheSync , segments : ISegmentsCacheSync , largeSegments ?: ISegmentsCacheSync } , matchingKey ?: string ) {
19+ export function loadData ( preloadedData : SplitIO . PreloadedData , storage : { splits ?: ISplitsCacheSync , rbSegments ?: IRBSegmentsCacheSync , segments : ISegmentsCacheSync , largeSegments ?: ISegmentsCacheSync } , matchingKey ?: string ) {
1920 // Do not load data if current preloadedData is empty
2021 if ( Object . keys ( preloadedData ) . length === 0 ) return ;
2122
22- const { segments = { } , since = - 1 , flags = [ ] } = preloadedData ;
23+ const { segments = { } , since = - 1 , flags = [ ] , rbSince = - 1 , rbSegments = [ ] } = preloadedData ;
2324
2425 if ( storage . splits ) {
2526 const storedSince = storage . splits . getChangeNumber ( ) ;
@@ -34,6 +35,19 @@ export function loadData(preloadedData: SplitIO.PreloadedData, storage: { splits
3435 storage . splits . update ( flags as ISplit [ ] , [ ] , since ) ;
3536 }
3637
38+ if ( storage . rbSegments ) {
39+ const storedSince = storage . rbSegments . getChangeNumber ( ) ;
40+
41+ // Do not load data if current data is more recent
42+ if ( storedSince > rbSince ) return ;
43+
44+ // cleaning up the localStorage data, since some cached splits might need be part of the preloaded data
45+ storage . rbSegments . clear ( ) ;
46+
47+ // splitsData in an object where the property is the split name and the pertaining value is a stringified json of its data
48+ storage . rbSegments . update ( rbSegments as IRBSegment [ ] , [ ] , rbSince ) ;
49+ }
50+
3751 if ( matchingKey ) { // add memberships data (client-side)
3852 let memberships = preloadedData . memberships && preloadedData . memberships [ matchingKey ] ;
3953 if ( ! memberships && segments ) {
@@ -61,9 +75,10 @@ export function loadData(preloadedData: SplitIO.PreloadedData, storage: { splits
6175
6276export function getSnapshot ( storage : IStorageSync , userKeys ?: SplitIO . SplitKey [ ] ) : SplitIO . PreloadedData {
6377 return {
64- // lastUpdated: Date.now(),
6578 since : storage . splits . getChangeNumber ( ) ,
6679 flags : storage . splits . getAll ( ) ,
80+ rbSince : storage . rbSegments . getChangeNumber ( ) ,
81+ rbSegments : storage . rbSegments . getAll ( ) ,
6782 segments : userKeys ?
6883 undefined : // @ts -ignore accessing private prop
6984 Object . keys ( storage . segments . segmentCache ) . reduce ( ( prev , cur ) => { // @ts -ignore accessing private prop
0 commit comments