Skip to content

Commit 2e2a865

Browse files
authored
fix: Dispatch change events after ready event. (#252)
1 parent e2e8971 commit 2e2a865

File tree

1 file changed

+41
-35
lines changed

1 file changed

+41
-35
lines changed

packages/shared/sdk-server/src/data_sources/DataSourceUpdates.ts

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -70,38 +70,40 @@ export default class DataSourceUpdates implements LDDataSourceUpdates {
7070
const checkForChanges = this.hasEventListeners();
7171
const doInit = (oldData?: LDFeatureStoreDataStorage) => {
7272
this.featureStore.init(allData, () => {
73-
this.dependencyTracker.reset();
73+
// Defer change events so they execute after the callback.
74+
Promise.resolve().then(() => {
75+
this.dependencyTracker.reset();
7476

75-
Object.entries(allData).forEach(([namespace, items]) => {
76-
Object.keys(items || {}).forEach((key) => {
77-
const item = items[key];
78-
this.dependencyTracker.updateDependenciesFrom(
79-
namespace,
80-
key,
81-
computeDependencies(namespace, item),
82-
);
83-
});
84-
});
85-
86-
if (checkForChanges) {
87-
const updatedItems = new NamespacedDataSet<boolean>();
88-
Object.keys(allData).forEach((namespace) => {
89-
const oldDataForKind = oldData?.[namespace] || {};
90-
const newDataForKind = allData[namespace];
91-
const mergedData = { ...oldDataForKind, ...newDataForKind };
92-
Object.keys(mergedData).forEach((key) => {
93-
this.addIfModified(
77+
Object.entries(allData).forEach(([namespace, items]) => {
78+
Object.keys(items || {}).forEach((key) => {
79+
const item = items[key];
80+
this.dependencyTracker.updateDependenciesFrom(
9481
namespace,
9582
key,
96-
oldDataForKind && oldDataForKind[key],
97-
newDataForKind && newDataForKind[key],
98-
updatedItems,
83+
computeDependencies(namespace, item),
9984
);
10085
});
10186
});
102-
this.sendChangeEvents(updatedItems);
103-
}
10487

88+
if (checkForChanges) {
89+
const updatedItems = new NamespacedDataSet<boolean>();
90+
Object.keys(allData).forEach((namespace) => {
91+
const oldDataForKind = oldData?.[namespace] || {};
92+
const newDataForKind = allData[namespace];
93+
const mergedData = { ...oldDataForKind, ...newDataForKind };
94+
Object.keys(mergedData).forEach((key) => {
95+
this.addIfModified(
96+
namespace,
97+
key,
98+
oldDataForKind && oldDataForKind[key],
99+
newDataForKind && newDataForKind[key],
100+
updatedItems,
101+
);
102+
});
103+
});
104+
this.sendChangeEvents(updatedItems);
105+
}
106+
});
105107
callback?.();
106108
});
107109
};
@@ -126,16 +128,20 @@ export default class DataSourceUpdates implements LDDataSourceUpdates {
126128
const checkForChanges = this.hasEventListeners();
127129
const doUpsert = (oldItem?: LDFeatureStoreItem | null) => {
128130
this.featureStore.upsert(kind, data, () => {
129-
this.dependencyTracker.updateDependenciesFrom(
130-
kind.namespace,
131-
key,
132-
computeDependencies(kind.namespace, data),
133-
);
134-
if (checkForChanges) {
135-
const updatedItems = new NamespacedDataSet<boolean>();
136-
this.addIfModified(kind.namespace, key, oldItem, data, updatedItems);
137-
this.sendChangeEvents(updatedItems);
138-
}
131+
// Defer change events so they execute after the callback.
132+
Promise.resolve().then(() => {
133+
this.dependencyTracker.updateDependenciesFrom(
134+
kind.namespace,
135+
key,
136+
computeDependencies(kind.namespace, data),
137+
);
138+
if (checkForChanges) {
139+
const updatedItems = new NamespacedDataSet<boolean>();
140+
this.addIfModified(kind.namespace, key, oldItem, data, updatedItems);
141+
this.sendChangeEvents(updatedItems);
142+
}
143+
});
144+
139145
callback?.();
140146
});
141147
};

0 commit comments

Comments
 (0)