Skip to content

Commit c17e3fe

Browse files
committed
refactoring code in InMemoryFeatureStore
1 parent 0ea135a commit c17e3fe

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

packages/shared/sdk-server/src/store/InMemoryFeatureStore.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,6 @@ export default class InMemoryFeatureStore implements LDFeatureStore {
1212

1313
private _initCalled = false;
1414

15-
private _addItem(kind: DataKind, key: string, item: LDFeatureStoreItem) {
16-
let items = this._allData[kind.namespace];
17-
if (!items) {
18-
items = {};
19-
this._allData[kind.namespace] = items;
20-
}
21-
if (Object.hasOwnProperty.call(items, key)) {
22-
const old = items[key];
23-
if (!old || old.version < item.version) {
24-
items[key] = item;
25-
}
26-
} else {
27-
items[key] = item;
28-
}
29-
}
30-
3115
get(kind: DataKind, key: string, callback: (res: LDFeatureStoreItem | null) => void): void {
3216
const items = this._allData[kind.namespace];
3317
if (items) {
@@ -95,9 +79,20 @@ export default class InMemoryFeatureStore implements LDFeatureStore {
9579
} else {
9680
Object.entries(data).forEach(([namespace, items]) => {
9781
Object.keys(items || {}).forEach((key) => {
82+
let existingItems = this._allData[namespace];
83+
if (!existingItems) {
84+
existingItems = {};
85+
this._allData[namespace] = existingItems;
86+
}
9887
const item = items[key];
99-
// TODO: optimize this section, perhaps get rid of _addItem
100-
this._addItem({ namespace }, key, item);
88+
if (Object.hasOwnProperty.call(existingItems, key)) {
89+
const old = existingItems[key];
90+
if (!old || old.version < item.version) {
91+
existingItems[key] = item;
92+
}
93+
} else {
94+
existingItems[key] = item;
95+
}
10196
});
10297
});
10398
}

0 commit comments

Comments
 (0)