Skip to content

Commit 075d70d

Browse files
authored
fix: prevent redundant pushes (#1005)
1 parent e94eef8 commit 075d70d

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

runner/src/storage/cache.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -798,10 +798,10 @@ export class Provider implements StorageProvider {
798798
return entity?.is as StorageValue<T> | undefined;
799799
}
800800
async send<T = any>(
801-
changes: { entityId: EntityId; value: StorageValue<T> }[],
801+
batch: { entityId: EntityId; value: StorageValue<T> }[],
802802
): Promise<
803803
Result<
804-
Commit,
804+
Unit,
805805
| ConflictError
806806
| TransactionError
807807
| ConnectionError
@@ -810,17 +810,31 @@ export class Provider implements StorageProvider {
810810
| StoreError
811811
>
812812
> {
813-
const { the } = this;
814-
815-
const result = await this.workspace.push(changes.map((change) => ({
816-
the,
817-
of: Provider.toEntity(change.entityId),
818-
// ⚠️ We do JSON roundtrips to strip of the undefined values that
819-
// cause problems with serialization.
820-
is: JSON.parse(JSON.stringify(change.value)),
821-
})));
813+
const { the, workspace } = this;
814+
815+
const changes = [];
816+
for (const { entityId, value } of batch) {
817+
const of = Provider.toEntity(entityId);
818+
const content = JSON.stringify(value);
819+
820+
const current = workspace.get({ the, of });
821+
if (JSON.stringify(current) !== content) {
822+
changes.push({
823+
the,
824+
of,
825+
// ⚠️ We do JSON roundtrips to strip of the undefined values that
826+
// cause problems with serialization.
827+
is: JSON.parse(content) as JSONValue,
828+
});
829+
}
830+
}
822831

823-
return result;
832+
if (changes.length > 0) {
833+
const result = await this.workspace.push(changes);
834+
return result.error ? result : { ok: {} };
835+
} else {
836+
return { ok: {} };
837+
}
824838
}
825839

826840
parse(source: string): Memory.ProviderCommand<Memory.Protocol> {

0 commit comments

Comments
 (0)