Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make emergent unsupported behaviors safer in prod #9120

Merged
merged 5 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# compiled output
**/dist/
**/dist-*/
**/dist-control/
**/dist-experiment/
**/tmp/
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/packages/-ember-data/docs/
concat-stats-for
dist
dist-*
tmp
packages/tracking/addon
packages/request/addon
Expand Down
29 changes: 26 additions & 3 deletions packages/json-api/src/-private/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,8 @@ export default class JSONAPICache implements Cache {
const rels = graph.identifiers.get(identifier);
if (rels) {
Object.keys(rels).forEach((key) => {
const rel = rels[key]!;
if (rel.definition.isImplicit) {
const rel = rels[key];
if (!rel || rel.definition.isImplicit) {
return;
}
relationships[key] = (rel as ManyRelationship | BelongsToRelationship).getData();
Expand Down Expand Up @@ -927,6 +927,13 @@ export default class JSONAPICache implements Cache {
*/
getAttr(identifier: StableRecordIdentifier, attr: string): unknown {
const cached = this.__peek(identifier, true);
assert(`Cannot retrieve attributes for identifier ${identifier} as it is not present in the cache`, cached);

// in Prod we try to recover when accessing something that
// doesn't exist
if (!cached) {
return undefined;
}
if (cached.localAttrs && attr in cached.localAttrs) {
return cached.localAttrs[attr];
} else if (cached.inflightAttrs && attr in cached.inflightAttrs) {
Expand Down Expand Up @@ -981,8 +988,17 @@ export default class JSONAPICache implements Cache {
* @returns { <field>: [<old>, <new>] }
*/
changedAttrs(identifier: StableRecordIdentifier): ChangedAttributesHash {
const cached = this.__peek(identifier, false);
assert(`Cannot retrieve changed attributes for identifier ${identifier} as it is not present in the cache`, cached);

// in Prod we try to recover when accessing something that
// doesn't exist
if (!cached) {
return Object.create(null);
}

// TODO freeze in dev
return this.__peek(identifier, false).changes || Object.create(null);
return cached.changes || Object.create(null);
}

/**
Expand All @@ -995,6 +1011,13 @@ export default class JSONAPICache implements Cache {
*/
hasChangedAttrs(identifier: StableRecordIdentifier): boolean {
const cached = this.__peek(identifier, true);
assert(`Cannot retrieve changed attributes for identifier ${identifier} as it is not present in the cache`, cached);

// in Prod we try to recover when accessing something that
// doesn't exist
if (!cached) {
return false;
}

return (
(cached.inflightAttrs !== null && Object.keys(cached.inflightAttrs).length > 0) ||
Expand Down
5 changes: 2 additions & 3 deletions packages/model/src/-private/legacy-relationships-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,13 @@ export class LegacySupport {
if (relatedIdentifier === null) {
return null;
} else {
let toReturn = store._instanceCache.getRecord(relatedIdentifier);
assert(
`You looked up the '${key}' relationship on a '${identifier.type}' with id ${
identifier.id || 'null'
} but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (\`belongsTo(<type>, { async: true, inverse: <inverse> })\`)`,
toReturn === null || store._instanceCache.recordIsLoaded(relatedIdentifier, true)
store._instanceCache.recordIsLoaded(relatedIdentifier, true)
);
return toReturn;
return store._instanceCache.getRecord(relatedIdentifier);
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions packages/store/src/-private/caches/instance-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,13 @@ export class InstanceCache {
}
);
}
assert(`Cannot create a record for ${identifier.type + identifier.id} (${identifier.lid}) as no resource data exists`, cache.peek(identifier));
assert(
`Cannot create a record for ${identifier.type + ':' + String(identifier.id)} (${
identifier.lid
}) as no resource data exists`,
// @ts-expect-error managedVersion is private and debug only
Boolean(cache.managedVersion === '1' || cache.peek(identifier))
);
record = this.store.instantiateRecord(
identifier,
properties || {},
Expand All @@ -273,7 +279,12 @@ export class InstanceCache {
this.store.notifications
);
} else {
assert(`Cannot create a record for ${identifier.type + identifier.id} (${identifier.lid}) as no resource data exists`, cache.peek(identifier));
assert(
`Cannot create a record for ${identifier.type + ':' + String(identifier.id)} (${
identifier.lid
}) as no resource data exists`,
cache.peek(identifier)
);
record = this.store.instantiateRecord(identifier, properties || {});
}

Expand Down
Loading