|
1 | 1 | import { SplitIO } from '../types';
|
2 |
| -import { ISegmentsCacheSync, ISplitsCacheSync, IStorageSync } from './types'; |
3 |
| -import { setToArray, ISet } from '../utils/lang/sets'; |
4 |
| -import { getMatching } from '../utils/key'; |
| 2 | +import { DEFAULT_CACHE_EXPIRATION_IN_MILLIS } from '../utils/constants/browser'; |
| 3 | +import { DataLoader, ISegmentsCacheSync, ISplitsCacheSync } from './types'; |
5 | 4 |
|
6 | 5 | /**
|
7 |
| - * Storage-agnostic adaptation of `loadDataIntoLocalStorage` function |
8 |
| - * (https://github.com/godaddy/split-javascript-data-loader/blob/master/src/load-data.js) |
| 6 | + * Factory of client-side storage loader |
9 | 7 | *
|
10 |
| - * @param preloadedData validated data following the format proposed in https://github.com/godaddy/split-javascript-data-loader and extended with a `mySegmentsData` property. |
11 |
| - * @param storage object containing `splits` and `segments` cache (client-side variant) |
12 |
| - * @param userKey user key (matching key) of the provided MySegmentsCache |
13 |
| - * |
14 |
| - * @TODO extend to load largeSegments |
15 |
| - * @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. |
16 |
| - * @TODO add logs, and input validation in this module, in favor of size reduction. |
17 |
| - * @TODO unit tests |
| 8 | + * @param preloadedData validated data following the format proposed in https://github.com/godaddy/split-javascript-data-loader |
| 9 | + * and extended with a `mySegmentsData` property. |
| 10 | + * @returns function to preload the storage |
18 | 11 | */
|
19 |
| -export function loadData(preloadedData: SplitIO.PreloadedData, storage: { splits?: ISplitsCacheSync, segments: ISegmentsCacheSync, largeSegments?: ISegmentsCacheSync }, matchingKey?: string) { |
20 |
| - // Do not load data if current preloadedData is empty |
21 |
| - if (Object.keys(preloadedData).length === 0) return; |
22 |
| - |
23 |
| - const { segmentsData = {}, since = -1, splitsData = [] } = preloadedData; |
| 12 | +export function dataLoaderFactory(preloadedData: SplitIO.PreloadedData): DataLoader { |
| 13 | + |
| 14 | + /** |
| 15 | + * Storage-agnostic adaptation of `loadDataIntoLocalStorage` function |
| 16 | + * (https://github.com/godaddy/split-javascript-data-loader/blob/master/src/load-data.js) |
| 17 | + * |
| 18 | + * @param storage object containing `splits` and `segments` cache (client-side variant) |
| 19 | + * @param userId user key string of the provided MySegmentsCache |
| 20 | + * |
| 21 | + * @TODO extend to support SegmentsCache (server-side variant) by making `userId` optional and adding the corresponding logic. |
| 22 | + * @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. |
| 23 | + */ |
| 24 | + return function loadData(storage: { splits: ISplitsCacheSync, segments: ISegmentsCacheSync }, userId: string) { |
| 25 | + // Do not load data if current preloadedData is empty |
| 26 | + if (Object.keys(preloadedData).length === 0) return; |
| 27 | + |
| 28 | + const { lastUpdated = -1, segmentsData = {}, since = -1, splitsData = {} } = preloadedData; |
24 | 29 |
|
25 |
| - if (storage.splits) { |
26 | 30 | const storedSince = storage.splits.getChangeNumber();
|
| 31 | + const expirationTimestamp = Date.now() - DEFAULT_CACHE_EXPIRATION_IN_MILLIS; |
27 | 32 |
|
28 |
| - // Do not load data if current data is more recent |
29 |
| - if (storedSince > since) return; |
| 33 | + // Do not load data if current localStorage data is more recent, |
| 34 | + // or if its `lastUpdated` timestamp is older than the given `expirationTimestamp`, |
| 35 | + if (storedSince > since || lastUpdated < expirationTimestamp) return; |
30 | 36 |
|
31 | 37 | // cleaning up the localStorage data, since some cached splits might need be part of the preloaded data
|
32 | 38 | storage.splits.clear();
|
33 | 39 | storage.splits.setChangeNumber(since);
|
34 | 40 |
|
35 | 41 | // splitsData in an object where the property is the split name and the pertaining value is a stringified json of its data
|
36 |
| - storage.splits.addSplits(splitsData.map(split => ([split.name, split]))); |
37 |
| - } |
| 42 | + storage.splits.addSplits(Object.keys(splitsData).map(splitName => JSON.parse(splitsData[splitName]))); |
38 | 43 |
|
39 |
| - if (matchingKey) { // add mySegments data (client-side) |
40 |
| - let mySegmentsData = preloadedData.mySegmentsData && preloadedData.mySegmentsData[matchingKey]; |
| 44 | + // add mySegments data |
| 45 | + let mySegmentsData = preloadedData.mySegmentsData && preloadedData.mySegmentsData[userId]; |
41 | 46 | if (!mySegmentsData) {
|
42 | 47 | // segmentsData in an object where the property is the segment name and the pertaining value is a stringified object that contains the `added` array of userIds
|
43 | 48 | mySegmentsData = Object.keys(segmentsData).filter(segmentName => {
|
44 |
| - const matchingKeys = segmentsData[segmentName]; |
45 |
| - return matchingKeys.indexOf(matchingKey) > -1; |
| 49 | + const userIds = JSON.parse(segmentsData[segmentName]).added; |
| 50 | + return Array.isArray(userIds) && userIds.indexOf(userId) > -1; |
46 | 51 | });
|
47 | 52 | }
|
48 | 53 | storage.segments.resetSegments({ k: mySegmentsData.map(s => ({ n: s })) });
|
49 |
| - } else { // add segments data (server-side) |
50 |
| - Object.keys(segmentsData).filter(segmentName => { |
51 |
| - const matchingKeys = segmentsData[segmentName]; |
52 |
| - storage.segments.addToSegment(segmentName, matchingKeys); |
53 |
| - }); |
54 |
| - } |
55 |
| -} |
56 |
| - |
57 |
| -export function getSnapshot(storage: IStorageSync, userKeys?: SplitIO.SplitKey[]): SplitIO.PreloadedData { |
58 |
| - return { |
59 |
| - // lastUpdated: Date.now(), |
60 |
| - since: storage.splits.getChangeNumber(), |
61 |
| - splitsData: storage.splits.getAll(), |
62 |
| - segmentsData: userKeys ? |
63 |
| - undefined : // @ts-ignore accessing private prop |
64 |
| - Object.keys(storage.segments.segmentCache).reduce((prev, cur) => { // @ts-ignore accessing private prop |
65 |
| - prev[cur] = setToArray(storage.segments.segmentCache[cur] as ISet<string>); |
66 |
| - return prev; |
67 |
| - }, {}), |
68 |
| - mySegmentsData: userKeys ? |
69 |
| - userKeys.reduce<Record<string, string[]>>((prev, userKey) => { |
70 |
| - prev[getMatching(userKey)] = storage.shared ? |
71 |
| - // Client-side segments |
72 |
| - // @ts-ignore accessing private prop |
73 |
| - Object.keys(storage.shared(userKey).segments.segmentCache) : |
74 |
| - // Server-side segments |
75 |
| - // @ts-ignore accessing private prop |
76 |
| - Object.keys(storage.segments.segmentCache).reduce<string[]>((prev, segmentName) => { // @ts-ignore accessing private prop |
77 |
| - return storage.segments.segmentCache[segmentName].has(userKey) ? |
78 |
| - prev.concat(segmentName) : |
79 |
| - prev; |
80 |
| - }, []); |
81 |
| - return prev; |
82 |
| - }, {}) : |
83 |
| - undefined |
84 | 54 | };
|
85 | 55 | }
|
0 commit comments