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

[WIP] issue 9232 #9380

Draft
wants to merge 2 commits into
base: release-4-6
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ export class LegacySupport {
}

let promise = this._findBelongsTo(key, resource, relationshipMeta, options);
const isLoaded = relatedIdentifier && store._instanceCache.recordIsLoaded(relatedIdentifier);

return this._updatePromiseProxyFor('belongsTo', key, {
promise,
content: relatedIdentifier ? store._instanceCache.getRecord(relatedIdentifier) : null,
content: isLoaded ? store._instanceCache.getRecord(relatedIdentifier!) : null,
_belongsToState,
});
} else {
Expand Down
3 changes: 3 additions & 0 deletions packages/model/addon/-private/record-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ export default class RecordState {
notifications.subscribe(identity, (identifier: StableRecordIdentifier, type: NotificationType, key?: string) => {
switch (type) {
case 'state':
this.notify('isSaved');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@runspired what do you think is the best way to test this? Can try to write ones based of an existing one if you have an example

this.notify('isLoaded');
this.notify('isEmpty');
this.notify('isNew');
this.notify('isDeleted');
this.notify('isDirty');
Expand Down
36 changes: 36 additions & 0 deletions packages/store/addon/-private/instance-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,42 @@ export class InstanceCache {
return this._internalModelForResource(identifier);
}

recordIsLoaded(identifier: StableRecordIdentifier, filterDeleted: boolean = false) {
const recordData = this.#instances.recordData.get(identifier);
if (!recordData) {
return false;
}
const isNew = recordData.isNew?.();
const isEmpty = recordData.isEmpty?.();

// if we are new we must consider ourselves loaded
if (isNew) {
return !recordData.isDeleted?.();
}
// even if we have a past request, if we are now empty we are not loaded
// typically this is true after an unloadRecord call

// if we are not empty, not new && we have a fulfilled request then we are loaded
// we should consider allowing for something to be loaded that is simply "not empty".
// which is how RecordState currently handles this case; however, RecordState is buggy
// in that it does not account for unloading.
return filterDeleted && recordData.isDeletionCommitted?.() ? false : !isEmpty;

/*
const req = this.store.getRequestStateService();
const fulfilled = req.getLastRequestForRecord(identifier);
const isLocallyLoaded = !isEmpty;
const isLoading =
!isLocallyLoaded &&
fulfilled === null &&
req.getPendingRequestsForRecord(identifier).some((req) => req.type === 'query');
if (isEmpty || (filterDeleted && recordData.isDeletionCommitted(identifier)) || isLoading) {
return false;
}
return true;
*/
}

createSnapshot(identifier: StableRecordIdentifier, options: FindOptions = {}): Snapshot {
return new Snapshot(options, identifier, this.store);
}
Expand Down
Loading