Skip to content

Commit

Permalink
fix: fix via detection instead
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Mar 11, 2024
1 parent c41ae45 commit e44770c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ function replaceRelatedRecordsLocal(graph: Graph, op: ReplaceRelatedRecordsOpera
const identifiers = op.value;
const relationship = graph.get(op.record, op.field);
assert(`expected hasMany relationship`, isHasMany(relationship));

// relationships for newly created records begin in the dirty state, so if updated
// before flushed we would fail to notify. This check helps us avoid that.
const isMaybeFirstUpdate =
relationship.remoteState.length === 0 &&
relationship.localState === null &&
relationship.state.hasReceivedData === false;
relationship.state.hasReceivedData = true;
const { additions, removals } = relationship;
const { inverseKey, type } = relationship.definition;
Expand Down Expand Up @@ -151,7 +158,10 @@ function replaceRelatedRecordsLocal(graph: Graph, op: ReplaceRelatedRecordsOpera
relationship.localState = diff.finalState;
relationship.isDirty = wasDirty;

if (!wasDirty /*&& becameDirty // TODO to guard like this we need to detect reorder when diffing local */) {
if (
isMaybeFirstUpdate ||
!wasDirty /*&& becameDirty // TODO to guard like this we need to detect reorder when diffing local */
) {
notifyChange(graph, op.record, op.field);
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/model/src/-private/many-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export default class RelatedCollection<T = unknown> extends RecordArray<T> {
const [start, deleteCount, ...adds] = args as [number, number, ...OpaqueRecordInstance[]];

// detect a full replace
if (start === 0 && deleteCount === this[SOURCE].length && this[SOURCE].length > 0) {
if (start === 0 && deleteCount === this[SOURCE].length) {
const newValues = extractIdentifiersFromRecords(adds);

assertNoDuplicates(
Expand Down

0 comments on commit e44770c

Please sign in to comment.