Skip to content

Commit

Permalink
replace usage of internalModel.isHiddenFromRecordArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Jul 31, 2022
1 parent 506000a commit ee62785
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 51 deletions.
41 changes: 41 additions & 0 deletions packages/store/addon/-private/caches/instance-cache.ts
Expand Up @@ -348,6 +348,47 @@ export class InstanceCache {
}
}

export function isHiddenFromRecordArrays(cache: InstanceCache, identifier: StableRecordIdentifier): boolean {
// During dematerialization we don't want to rematerialize the record.
// recordWasDeleted can cause other records to rematerialize because it
// removes the internal model from the array and Ember arrays will always
// `objectAt(0)` and `objectAt(len -1)` to check whether `firstObject` or
// `lastObject` have changed. When this happens we don't want those
// models to rematerialize their records.

// eager checks to avoid instantiating record data if we are empty or loading
let recordData = cache.peek({ identifier, bucket: 'recordData' });
if (!recordData) {
return true;
}

// if isLoading return false
// if isDematerializing, destroyed, or has scheduled destroy return true
// TODO eliminate this internalModel need
const internalModel = cache.getInternalModel(identifier);
if (!internalModel.isEmpty || internalModel.isLoading) {
return false;
}
if (recordData.isDeletionCommitted?.() || (recordData.isNew?.() && recordData.isDeleted?.())) {
return true;
} else {
return false;
}
}

function _recordDataIsFullDeleted(recordData: RecordData): boolean {
if (recordData.isDeletionCommitted?.() || (recordData.isNew?.() && recordData.isDeleted?.())) {
return true;
} else {
return false;
}
}

export function recordDataIsFullyDeleted(cache: InstanceCache, identifier: StableRecordIdentifier): boolean {
let recordData = cache.peek({ identifier, bucket: 'recordData' });
return !recordData || _recordDataIsFullDeleted(recordData);
}

function assertRecordsPassedToHasMany(records: RecordInstance[]) {
assert(`You must pass an array of records to set a hasMany relationship`, Array.isArray(records));
assert(
Expand Down
Expand Up @@ -114,42 +114,6 @@ export default class InternalModel {
return this.store._instanceCache.getRecordData(this.identifier);
}

isHiddenFromRecordArrays() {
// During dematerialization we don't want to rematerialize the record.
// recordWasDeleted can cause other records to rematerialize because it
// removes the internal model from the array and Ember arrays will always
// `objectAt(0)` and `objectAt(len -1)` to check whether `firstObject` or
// `lastObject` have changed. When this happens we don't want those
// models to rematerialize their records.

// eager checks to avoid instantiating record data if we are empty or loading
if (this.isEmpty) {
return true;
}

if (this.isLoading) {
return false;
}

let isRecordFullyDeleted = this._isRecordFullyDeleted();
return this._isDematerializing || this.hasScheduledDestroy() || this.isDestroyed || isRecordFullyDeleted;
}

_isRecordFullyDeleted(): boolean {
if (this._recordData.isDeletionCommitted && this._recordData.isDeletionCommitted()) {
return true;
} else if (
this._recordData.isNew &&
this._recordData.isDeleted &&
this._recordData.isNew() &&
this._recordData.isDeleted()
) {
return true;
} else {
return false;
}
}

isDeleted(): boolean {
if (this._recordData.isDeleted) {
return this._recordData.isDeleted();
Expand Down
17 changes: 4 additions & 13 deletions packages/store/addon/-private/managers/record-array-manager.ts
Expand Up @@ -12,6 +12,7 @@ import type { CollectionResourceDocument, Meta } from '@ember-data/types/q/ember
import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
import type { Dict } from '@ember-data/types/q/utils';

import { isHiddenFromRecordArrays } from '../caches/instance-cache';
import { internalModelFactoryFor } from '../caches/internal-model-factory';
import AdapterPopulatedRecordArray from '../record-arrays/adapter-populated-record-array';
import RecordArray from '../record-arrays/record-array';
Expand All @@ -37,16 +38,6 @@ function getIdentifier(identifier: StableRecordIdentifier): StableRecordIdentifi
return identifier;
}

function shouldIncludeInRecordArrays(store: Store, identifier: StableRecordIdentifier): boolean {
const cache = internalModelFactoryFor(store);
const internalModel = cache.peek(identifier);

if (internalModel === null) {
return false;
}
return !internalModel.isHiddenFromRecordArrays();
}

/**
@class RecordArrayManager
@internal
Expand Down Expand Up @@ -90,7 +81,7 @@ class RecordArrayManager {
// recordArrayManager
pendingForIdentifier.delete(i);
// build up a set of models to ensure we have purged correctly;
let isIncluded = shouldIncludeInRecordArrays(this.store, i);
let isIncluded = !isHiddenFromRecordArrays(this.store._instanceCache, i);
if (!isIncluded) {
identifiersToRemove.push(i);
}
Expand Down Expand Up @@ -208,7 +199,7 @@ class RecordArrayManager {
let visible: StableRecordIdentifier[] = [];
for (let i = 0; i < all.length; i++) {
let identifier = all[i];
let shouldInclude = shouldIncludeInRecordArrays(this.store, identifier);
let shouldInclude = !isHiddenFromRecordArrays(this.store._instanceCache, identifier);

if (shouldInclude) {
visible.push(identifier);
Expand Down Expand Up @@ -400,7 +391,7 @@ function updateLiveRecordArray(store: Store, recordArray: RecordArray, identifie

for (let i = 0; i < identifiers.length; i++) {
let identifier = identifiers[i];
let shouldInclude = shouldIncludeInRecordArrays(store, identifier);
let shouldInclude = !isHiddenFromRecordArrays(store._instanceCache, identifier);
let recordArrays = recordArraysForIdentifier(identifier);

if (shouldInclude) {
Expand Down
4 changes: 2 additions & 2 deletions packages/store/addon/-private/store-service.ts
Expand Up @@ -41,7 +41,7 @@ import type { Dict } from '@ember-data/types/q/utils';

import edBackburner from './backburner';
import { IdentifierCache } from './caches/identifier-cache';
import { InstanceCache, storeFor, StoreMap } from './caches/instance-cache';
import { InstanceCache, recordDataIsFullyDeleted, storeFor, StoreMap } from './caches/instance-cache';
import {
internalModelFactoryFor,
peekRecordIdentifier,
Expand Down Expand Up @@ -2127,7 +2127,7 @@ class Store extends Service {
`Cannot initiate a save request for an unloaded record: ${identifier}`,
!internalModel.isEmpty && !internalModel.isDestroyed
);
if (internalModel._isRecordFullyDeleted()) {
if (recordDataIsFullyDeleted(this._instanceCache, identifier)) {
return resolve(record);
}

Expand Down

0 comments on commit ee62785

Please sign in to comment.