-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathTransactionalDataSourceUpdates.ts
More file actions
148 lines (135 loc) · 4.53 KB
/
TransactionalDataSourceUpdates.ts
File metadata and controls
148 lines (135 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { internal } from '@launchdarkly/js-sdk-common';
import { DataKind } from '../api/interfaces';
import {
LDFeatureStoreDataStorage,
LDFeatureStoreItem,
LDKeyedFeatureStoreItem,
LDTransactionalDataSourceUpdates,
LDTransactionalFeatureStore,
} from '../api/subsystems';
import VersionedDataKinds from '../store/VersionedDataKinds';
import { computeDependencies } from './DataSourceUpdates';
import DependencyTracker from './DependencyTracker';
import NamespacedDataSet from './NamespacedDataSet';
/**
* @internal
*/
export default class TransactionalDataSourceUpdates implements LDTransactionalDataSourceUpdates {
private readonly _dependencyTracker = new DependencyTracker();
constructor(
private readonly _featureStore: LDTransactionalFeatureStore,
private readonly _hasEventListeners: () => boolean,
private readonly _onChange: (key: string) => void,
) {}
init(
allData: LDFeatureStoreDataStorage,
callback: () => void,
initMetadata?: internal.InitMetadata,
): void {
this.applyChanges(true, allData, callback, initMetadata); // basis is true for init
}
upsert(kind: DataKind, data: LDKeyedFeatureStoreItem, callback: () => void): void {
this.applyChanges(
false, // basis is false for upserts
{
[kind.namespace]: {
[data.key]: data,
},
},
callback,
);
}
applyChanges(
basis: boolean,
data: LDFeatureStoreDataStorage,
callback: () => void,
initMetadata?: internal.InitMetadata,
selector?: string,
): void {
const checkForChanges = this._hasEventListeners();
const doApplyChanges = (oldData: LDFeatureStoreDataStorage) => {
this._featureStore.applyChanges(
basis,
data,
() => {
// Defer change events so they execute after the callback.
Promise.resolve().then(() => {
if (basis) {
this._dependencyTracker.reset();
}
Object.entries(data).forEach(([namespace, items]) => {
Object.keys(items || {}).forEach((key) => {
const item = items[key];
this._dependencyTracker.updateDependenciesFrom(
namespace,
key,
computeDependencies(namespace, item),
);
});
});
if (checkForChanges) {
const updatedItems = new NamespacedDataSet<boolean>();
Object.keys(data).forEach((namespace) => {
const oldDataForKind = oldData[namespace];
const newDataForKind = data[namespace];
let iterateData;
if (basis) {
// for basis, need to iterate on all keys
iterateData = { ...oldDataForKind, ...newDataForKind };
} else {
// for non basis, only need to iterate on keys in incoming data
iterateData = { ...newDataForKind };
}
Object.keys(iterateData).forEach((key) => {
this.addIfModified(
namespace,
key,
oldDataForKind && oldDataForKind[key],
newDataForKind && newDataForKind[key],
updatedItems,
);
});
});
this.sendChangeEvents(updatedItems);
}
});
callback?.();
},
initMetadata,
selector,
);
};
let oldData = {};
if (checkForChanges) {
// record old data before making changes to use for change calculations
this._featureStore.all(VersionedDataKinds.Features, (oldFlags) => {
this._featureStore.all(VersionedDataKinds.Segments, (oldSegments) => {
oldData = {
[VersionedDataKinds.Features.namespace]: oldFlags,
[VersionedDataKinds.Segments.namespace]: oldSegments,
};
});
});
}
doApplyChanges(oldData);
}
addIfModified(
namespace: string,
key: string,
oldValue: LDFeatureStoreItem | null | undefined,
newValue: LDFeatureStoreItem,
toDataSet: NamespacedDataSet<boolean>,
) {
if (newValue && oldValue && newValue.version <= oldValue.version) {
return;
}
this._dependencyTracker.updateModifiedItems(toDataSet, namespace, key);
}
sendChangeEvents(dataSet: NamespacedDataSet<boolean>) {
dataSet.enumerate((namespace, key) => {
if (namespace === VersionedDataKinds.Features.namespace) {
this._onChange(key);
}
});
}
}