From a0afcc3224f290a4b9db29e952603c3845f6d778 Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Wed, 27 Jul 2022 18:41:45 -0700 Subject: [PATCH] replace usage of internalModel.isHiddenFromRecordArrays --- .../addon/-private/caches/instance-cache.ts | 41 +++++++++++++++++++ .../legacy-model-support/internal-model.ts | 36 ---------------- .../-private/managers/record-array-manager.ts | 17 ++------ .../store/addon/-private/store-service.ts | 4 +- 4 files changed, 47 insertions(+), 51 deletions(-) diff --git a/packages/store/addon/-private/caches/instance-cache.ts b/packages/store/addon/-private/caches/instance-cache.ts index 8aa1e87fc4f..18b830e5065 100644 --- a/packages/store/addon/-private/caches/instance-cache.ts +++ b/packages/store/addon/-private/caches/instance-cache.ts @@ -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( diff --git a/packages/store/addon/-private/legacy-model-support/internal-model.ts b/packages/store/addon/-private/legacy-model-support/internal-model.ts index e10e8fb2b2a..fbf7e4bd1f9 100644 --- a/packages/store/addon/-private/legacy-model-support/internal-model.ts +++ b/packages/store/addon/-private/legacy-model-support/internal-model.ts @@ -105,42 +105,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(); diff --git a/packages/store/addon/-private/managers/record-array-manager.ts b/packages/store/addon/-private/managers/record-array-manager.ts index ec673b0ef28..b6463de6b21 100644 --- a/packages/store/addon/-private/managers/record-array-manager.ts +++ b/packages/store/addon/-private/managers/record-array-manager.ts @@ -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'; @@ -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 @@ -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); } @@ -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); @@ -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) { diff --git a/packages/store/addon/-private/store-service.ts b/packages/store/addon/-private/store-service.ts index 7f32e92706e..7b7d8a9e414 100644 --- a/packages/store/addon/-private/store-service.ts +++ b/packages/store/addon/-private/store-service.ts @@ -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, @@ -2124,7 +2124,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); }