Skip to content

Commit

Permalink
some more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Apr 30, 2021
1 parent 489b527 commit fc731d3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 82 deletions.
8 changes: 5 additions & 3 deletions packages/record-data/addon/-private/record-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,17 +362,19 @@ export default class RecordDataDefault implements RelationshipRecordData {
setDirtyHasMany(key: string, recordDatas: RecordData[]) {
let relationship = graphFor(this.storeWrapper).get(this.identifier, key);
relationship.clear();
relationship.addRecordDatas(recordDatas.map(recordIdentifierFor));
(relationship as ManyRelationship).addRecordDatas(recordDatas.map(recordIdentifierFor));
}

// append to "current state" via RecordDatas
addToHasMany(key: string, recordDatas: RecordData[], idx) {
graphFor(this.storeWrapper).get(this.identifier, key).addRecordDatas(recordDatas.map(recordIdentifierFor), idx);
let relationship = graphFor(this.storeWrapper).get(this.identifier, key);
(relationship as ManyRelationship).addRecordDatas(recordDatas.map(recordIdentifierFor), idx);
}

// remove from "current state" via RecordDatas
removeFromHasMany(key: string, recordDatas: RecordData[]) {
graphFor(this.storeWrapper).get(this.identifier, key).removeRecordDatas(recordDatas.map(recordIdentifierFor));
let relationship = graphFor(this.storeWrapper).get(this.identifier, key);
(relationship as ManyRelationship).removeRecordDatas(recordDatas.map(recordIdentifierFor));
}

commitWasRejected(identifier?, errors?: JsonApiValidationError[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assert, inspect, warn } from '@ember/debug';

import { CUSTOM_MODEL_CLASS } from '@ember-data/canary-features';
import { assertPolymorphicType } from '@ember-data/store/-debug';
import { identifierCacheFor, recordDataFor as peekRecordData } from '@ember-data/store/-private';
import { identifierCacheFor } from '@ember-data/store/-private';

import { createState } from '../../graph/-state';
import _normalizeLink from '../../normalize-link';
Expand Down Expand Up @@ -94,31 +94,30 @@ export default class BelongsToRelationship {
}

const inverseKey = this.definition.inverseKey;

this.forAllMembers((inverseIdentifier) => {
let recordData = inverseIdentifier && peekRecordData(inverseIdentifier);
if (!recordData || !inverseIdentifier) {
const callback = (inverseIdentifier) => {
if (!inverseIdentifier || !this.graph.has(inverseIdentifier, inverseKey)) {
return;
}

let relationship = this.graph.get(inverseIdentifier, inverseKey);
// TODO DO we need to grab implicit inverse and do this?

// For canonical members, it is possible that inverseRecordData has already been associated to
// to another record. For such cases, do not dematerialize the inverseRecordData
if (
!relationship || //we are implicit
relationship.definition.kind === 'implicit' ||
relationship.definition.kind === 'hasMany' || // the inverse is a hasMany
relationship.definition.kind !== 'belongsTo' ||
!(relationship as BelongsToRelationship).localState ||
this.identifier === (relationship as BelongsToRelationship).localState
) {
if (!relationship) {
return; // TODO wtf happened here in all the rebasing
}
relationship.inverseDidDematerialize(this.identifier);
}
});
};

if (this.remoteState) {
callback(this.remoteState);
}
if (this.localState && this.localState !== this.remoteState) {
callback(this.localState);
}
}

inverseDidDematerialize() {
Expand All @@ -130,8 +129,13 @@ export default class BelongsToRelationship {
// if the record being unloaded only exists on the client, we similarly
// treat it as a client side delete
this.removeRecordDataFromOwn(inverseRecordData!);
this.removeCanonicalRecordDataFromOwn(inverseRecordData!);
this.setRelationshipIsEmpty(true);
if (this.remoteState === inverseRecordData && inverseRecordData !== null) {
this.remoteState = null;
this.setHasReceivedData(true);
this.setRelationshipIsEmpty(true);
this.flushCanonicalLater();
this.setRelationshipIsEmpty(true);
}
} else {
this.setHasDematerializedInverse(true);
}
Expand Down Expand Up @@ -275,15 +279,6 @@ export default class BelongsToRelationship {
this.flushCanonicalLater();
}

forAllMembers(callback: (im: StableRecordIdentifier | null) => void) {
if (this.remoteState) {
callback(this.remoteState);
}
if (this.localState && this.localState !== this.remoteState) {
callback(this.localState);
}
}

/**
* External method for updating local state
*/
Expand Down Expand Up @@ -350,24 +345,16 @@ export default class BelongsToRelationship {
this.notifyBelongsToChange();
}

removeRecordData(recordData: StableRecordIdentifier | null) {
if (this.localState === recordData && recordData !== null) {
removeRecordData(inverseIdentifier: StableRecordIdentifier | null) {
if (this.localState === inverseIdentifier && inverseIdentifier !== null) {
const { inverseKey } = this.definition;
this.localState = null;
this.notifyBelongsToChange();
if (!this.definition.inverseIsImplicit) {
let inverseRelationship = this.graph.get(recordData, this.definition.inverseKey);
//Need to check for existence, as the record might unloading at the moment
if (inverseRelationship) {
inverseRelationship.removeRecordDataFromOwn(this.identifier);
}
} else {
if (!recordData) {
return;
}
const { inverseKey } = this.definition;
// TODO is this check ever false?
if (this.graph.has(recordData, inverseKey)) {
this.graph.get(recordData, inverseKey).removeRecordData(this.identifier);
if (this.graph.has(inverseIdentifier, inverseKey)) {
if (!this.definition.inverseIsImplicit) {
this.graph.get(inverseIdentifier, inverseKey).removeRecordDataFromOwn(this.identifier);
} else {
this.graph.get(inverseIdentifier, inverseKey).removeRecordData(this.identifier);
}
}
}
Expand Down Expand Up @@ -410,16 +397,6 @@ export default class BelongsToRelationship {
this.notifyBelongsToChange();
}

removeCanonicalRecordDataFromOwn(recordData: StableRecordIdentifier) {
if (this.remoteState !== recordData || recordData === null) {
return;
}
this.remoteState = null;
this.setHasReceivedData(true);
this.setRelationshipIsEmpty(true);
this.flushCanonicalLater();
}

/**
* can be called by the graph
*/
Expand All @@ -431,20 +408,18 @@ export default class BelongsToRelationship {
/*
Can be called from the other side
*/
removeCanonicalRecordData(recordData: StableRecordIdentifier | null) {
if (this.remoteState === recordData && recordData !== null) {
this.removeCanonicalRecordDataFromOwn(recordData);

if (!recordData || this.definition.isImplicit) {
return;
}
removeCanonicalRecordData(inverseIdentifier: StableRecordIdentifier | null) {
if (this.remoteState === inverseIdentifier && inverseIdentifier !== null) {
this.remoteState = null;
this.setHasReceivedData(true);
this.setRelationshipIsEmpty(true);
this.flushCanonicalLater();
this.setRelationshipIsEmpty(true);

const { inverseKey } = this.definition;
if (this.graph.has(recordData, inverseKey)) {
this.graph.get(recordData, inverseKey).removeCanonicalRecordData(this.identifier);
if (!this.definition.isImplicit && this.graph.has(inverseIdentifier, inverseKey)) {
this.graph.get(inverseIdentifier, inverseKey).removeCanonicalRecordData(this.identifier);
}

this.flushCanonicalLater(); // TODO does this need to be in the outer context
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,36 +86,28 @@ export default class Relationship {
}

const inverseKey = this.definition.inverseKey;

// we actually want a union of members and canonicalMembers
// they should be disjoint but currently are not due to a bug
this.forAllMembers((inverseIdentifier) => {
let recordData = inverseIdentifier && peekRecordData(inverseIdentifier);
if (!recordData || !inverseIdentifier) {
inverseIdentifier;
if (!inverseIdentifier || !this.graph.has(inverseIdentifier, inverseKey)) {
return;
}

let relationship = this.graph.get(inverseIdentifier, inverseKey);
// TODO DO we need to grab implicit inverse and do this?

// For canonical members, it is possible that inverseRecordData has already been associated to
// to another record. For such cases, do not dematerialize the inverseRecordData
if (
!relationship || //we are implicit
relationship.definition.kind === 'implicit' ||
relationship.definition.kind === 'hasMany' || // the inverse is a hasMany
relationship.definition.kind !== 'belongsTo' ||
!(relationship as BelongsToRelationship).localState ||
this.identifier === (relationship as BelongsToRelationship).localState
) {
if (!relationship) {
return; // TODO wtf happened here in all the rebasing
}
relationship.inverseDidDematerialize(this.identifier);
}
});
}

forAllMembers(callback: (im: StableRecordIdentifier | null) => void) {
// ensure we don't walk anything twice if an entry is
// in both members and canonicalMembers
let seen = Object.create(null);

for (let i = 0; i < this.members.list.length; i++) {
Expand Down Expand Up @@ -205,16 +197,12 @@ export default class Relationship {
this.graph.registerPolymorphicType(this.definition.type, recordData.type);
}
this.canonicalMembers.add(recordData);
this.setupInverseRelationship(recordData);
this.graph.get(recordData, this.definition.inverseKey).addCanonicalRecordData(this.identifier);
}
this.flushCanonicalLater();
this.setHasReceivedData(true);
}

setupInverseRelationship(recordData: StableRecordIdentifier) {
this.graph.get(recordData, this.definition.inverseKey).addCanonicalRecordData(this.identifier);
}

removeCanonicalRecordDatas(recordDatas: StableRecordIdentifier[], idx?: number) {
for (let i = 0; i < recordDatas.length; i++) {
if (idx !== undefined) {
Expand Down

0 comments on commit fc731d3

Please sign in to comment.