Skip to content

Commit fefcaf6

Browse files
Update InLocalStorage to avoid memory leak in localhost mode (#181)
1 parent 6af7ee0 commit fefcaf6

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/storages/inLocalStorage/index.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ImpressionsCacheInMemory } from '../inMemory/ImpressionsCacheInMemory';
22
import { ImpressionCountsCacheInMemory } from '../inMemory/ImpressionCountsCacheInMemory';
33
import { EventsCacheInMemory } from '../inMemory/EventsCacheInMemory';
4-
import { IStorageFactoryParams, IStorageSync, IStorageSyncFactory } from '../types';
4+
import { ISegmentsCacheSync, ISplitsCacheSync, IStorageFactoryParams, IStorageSync, IStorageSyncFactory } from '../types';
55
import { validatePrefix } from '../KeyBuilder';
66
import { KeyBuilderCS, myLargeSegmentsKeyBuilder } from '../KeyBuilderCS';
77
import { isLocalStorageAvailable } from '../../utils/env/isLocalStorageAvailable';
@@ -41,15 +41,15 @@ export function InLocalStorage(options: InLocalStorageOptions = {}): IStorageSyn
4141
const keys = new KeyBuilderCS(prefix, matchingKey);
4242
const expirationTimestamp = Date.now() - DEFAULT_CACHE_EXPIRATION_IN_MILLIS;
4343

44-
const splits = new SplitsCacheInLocal(settings, keys, expirationTimestamp);
45-
const segments = new MySegmentsCacheInLocal(log, keys);
46-
const largeSegments = new MySegmentsCacheInLocal(log, myLargeSegmentsKeyBuilder(prefix, matchingKey));
44+
const splits: ISplitsCacheSync = new SplitsCacheInLocal(settings, keys, expirationTimestamp);
45+
const segments: ISegmentsCacheSync = new MySegmentsCacheInLocal(log, keys);
46+
const largeSegments: ISegmentsCacheSync = new MySegmentsCacheInLocal(log, myLargeSegmentsKeyBuilder(prefix, matchingKey));
4747

4848
if (settings.mode === LOCALHOST_MODE || splits.getChangeNumber() > -1) {
4949
Promise.resolve().then(onReadyFromCacheCb);
5050
}
5151

52-
return {
52+
const storage = {
5353
splits,
5454
segments,
5555
largeSegments,
@@ -70,7 +70,7 @@ export function InLocalStorage(options: InLocalStorageOptions = {}): IStorageSyn
7070
},
7171

7272
// When using shared instanciation with MEMORY we reuse everything but segments (they are customer per key).
73-
shared(matchingKey: string) {
73+
shared(matchingKey: string): IStorageSync {
7474

7575
return {
7676
splits: this.splits,
@@ -89,6 +89,18 @@ export function InLocalStorage(options: InLocalStorageOptions = {}): IStorageSyn
8989
};
9090
},
9191
};
92+
93+
// @TODO revisit storage logic in localhost mode
94+
// No tracking data in localhost mode to avoid memory leaks
95+
if (params.settings.mode === LOCALHOST_MODE) {
96+
const noopTrack = () => true;
97+
storage.impressions.track = noopTrack;
98+
storage.events.track = noopTrack;
99+
if (storage.impressionCounts) storage.impressionCounts.track = noopTrack;
100+
if (storage.uniqueKeys) storage.uniqueKeys.track = noopTrack;
101+
}
102+
103+
return storage;
92104
}
93105

94106
InLocalStorageCSFactory.type = STORAGE_LOCALSTORAGE;

0 commit comments

Comments
 (0)