Skip to content

Commit

Permalink
the shape of things
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Aug 5, 2022
1 parent b728340 commit c3f3068
Show file tree
Hide file tree
Showing 26 changed files with 264 additions and 336 deletions.
2 changes: 0 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ module.exports = {
'ember-data-types/q/ember-data-json-api.ts',
'ember-data-types/q/ds-model.ts',
'packages/store/addon/-private/managers/record-data-store-wrapper.ts',
'packages/store/addon/-private/caches/internal-model-factory.ts',
'packages/store/addon/-private/network/snapshot.ts',
'packages/store/addon/-private/network/snapshot-record-array.ts',
'packages/store/addon/-private/legacy-model-support/schema-definition-service.ts',
Expand All @@ -205,7 +204,6 @@ module.exports = {
'packages/store/addon/-private/caches/record-data-for.ts',
'packages/store/addon/-private/utils/normalize-model-name.ts',
'packages/store/addon/-private/legacy-model-support/shim-model-class.ts',
'packages/store/addon/-private/legacy-model-support/internal-model.ts',
'packages/store/addon/-private/network/fetch-manager.ts',
'packages/store/addon/-private/store-service.ts',
'packages/store/addon/-private/utils/coerce-id.ts',
Expand Down
2 changes: 0 additions & 2 deletions ember-data-types/q/ds-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type EmberObject from '@ember/object';

import type { Errors } from '@ember-data/model/-private';
import type Store from '@ember-data/store';
import type InternalModel from '@ember-data/store/-private/legacy-model-support/internal-model';

import type { JsonApiValidationError } from './record-data-json-api';
import type { AttributeSchema, RelationshipSchema, RelationshipsSchema } from './record-data-schemas';
Expand All @@ -12,7 +11,6 @@ export interface DSModel extends EmberObject {
constructor: DSModelSchema;
store: Store;
errors: Errors;
_internalModel: InternalModel;
toString(): string;
save(): Promise<DSModel>;
eachRelationship<T>(callback: (this: T, key: string, meta: RelationshipSchema) => void, binding?: T): void;
Expand Down
2 changes: 0 additions & 2 deletions packages/-ember-data/addon/-private/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ export { default as DS } from './core';
export { Errors } from '@ember-data/model/-private';
export { Snapshot } from '@ember-data/store/-private';

// `ember-data-model-fragments` relies on `InternalModel`
// `ember-data-model-fragments' and `ember-data-change-tracker` rely on `normalizeModelName`
export {
AdapterPopulatedRecordArray,
InternalModel,
PromiseArray,
PromiseObject,
RecordArray,
Expand Down
2 changes: 0 additions & 2 deletions packages/-ember-data/addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
AdapterPopulatedRecordArray,
DS,
Errors,
InternalModel,
ManyArray,
PromiseArray,
PromiseManyArray,
Expand All @@ -52,7 +51,6 @@ DS.Model = Model;
DS.attr = attr;
DS.Errors = Errors;

DS.InternalModel = InternalModel;
DS.Snapshot = Snapshot;

DS.Adapter = Adapter;
Expand Down
3 changes: 0 additions & 3 deletions packages/-ember-data/node-tests/fixtures/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ module.exports = {
'(private) @ember-data/store IdentifierCache#_getRecordIdentifier',
'(private) @ember-data/store IdentifierCache#_mergeRecordIdentifiers',
'(private) @ember-data/store IdentifierCache#peekRecordIdentifier',
'(private) @ember-data/store InternalModelFactory#lookup',
'(private) @ember-data/store InternalModelFactory#peek',
'(private) @ember-data/store ManyArray#isPolymorphic',
'(private) @ember-data/store ManyArray#relationship',
'(private) @ember-data/store RecordArray#_createSnapshot',
Expand All @@ -89,7 +87,6 @@ module.exports = {
'(private) @ember-data/store Store#_push',
'(private) @ember-data/store Store#find',
'(private) @ember-data/store Store#init',
'(private) @ember-data/store Store#recordWasInvalid',
'(public) @ember-data/adapter Adapter#coalesceFindRequests',
'(public) @ember-data/adapter Adapter#createRecord',
'(public) @ember-data/adapter Adapter#deleteRecord',
Expand Down
10 changes: 3 additions & 7 deletions packages/model/addon/-private/legacy-relationships-support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {
import type { UpgradedMeta } from '@ember-data/record-data/-private/graph/-edge-definition';
import type { RelationshipState } from '@ember-data/record-data/-private/graph/-state';
import type Store from '@ember-data/store';
import type { InternalModel } from '@ember-data/store/-private';
import { recordDataFor, recordIdentifierFor, storeFor } from '@ember-data/store/-private';
import type { IdentifierCache } from '@ember-data/store/-private/caches/identifier-cache';
import type { DSModel } from '@ember-data/types/q/ds-model';
Expand Down Expand Up @@ -141,7 +140,8 @@ export class LegacySupport {
`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({ async: true })\`)`,
toReturn === null || !store._instanceCache.peek({ identifier: relatedIdentifier, bucket: 'recordData' })?.isEmpty?.()
toReturn === null ||
!store._instanceCache.peek({ identifier: relatedIdentifier, bucket: 'recordData' })?.isEmpty?.()
);
return toReturn;
}
Expand Down Expand Up @@ -705,11 +705,7 @@ function areAllInverseRecordsLoaded(store: Store, resource: JsonApiRelationship)
}
}

function isEmpty(
store: Store,
cache: IdentifierCache,
resource: ResourceIdentifierObject
): boolean {
function isEmpty(store: Store, cache: IdentifierCache, resource: ResourceIdentifierObject): boolean {
const identifier = cache.getOrCreateRecordIdentifier(resource);
const recordData = store._instanceCache.peek({ identifier, bucket: 'recordData' });
return !recordData || !!recordData.isEmpty?.();
Expand Down
20 changes: 12 additions & 8 deletions packages/model/addon/-private/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import {
DEPRECATE_SAVE_PROMISE_ACCESS,
} from '@ember-data/private-build-infra/deprecations';
import { recordIdentifierFor, storeFor } from '@ember-data/store';
import { coerceId, deprecatedPromiseObject, InternalModel, WeakCache } from '@ember-data/store/-private';
import { recordDataFor } from '@ember-data/store/-private';
import { coerceId, deprecatedPromiseObject, recordDataFor, WeakCache } from '@ember-data/store/-private';

import Errors from './errors';
import { LegacySupport } from './legacy-relationships-support';
Expand Down Expand Up @@ -138,8 +137,6 @@ class Model extends EmberObject {

this.setProperties(createProps);

// TODO pass something in such that we don't need internalModel
// to get this info
let store = storeFor(this);
let notifications = store._notificationManager;
let identity = recordIdentifierFor(this);
Expand Down Expand Up @@ -458,16 +455,23 @@ class Model extends EmberObject {
// this guard exists, because some dev-only deprecation code
// (addListener via validatePropertyInjections) invokes toString before the
// object is real.
if (DEBUG && !this._internalModel) {
return void 0;
if (DEBUG) {
try {
return recordIdentifierFor(this).id;
} catch {
return void 0;
}
}
return recordIdentifierFor(this).id;
}
set id(id) {
const normalizedId = coerceId(id);
const identifier = recordIdentifierFor(this);
let didChange = normalizedId !== identifier.id;
assert(`Cannot set ${identifier.type} record's id to ${id}, because id is already ${identifier.id}`, !didChange || identifier.id === null);
assert(
`Cannot set ${identifier.type} record's id to ${id}, because id is already ${identifier.id}`,
!didChange || identifier.id === null
);

if (normalizedId !== null && didChange) {
this.store._instanceCache.setRecordId(identifier.type, normalizedId, identifier.lid);
Expand Down Expand Up @@ -815,7 +819,7 @@ class Model extends EmberObject {
rollbackAttributes() {
const { currentState } = this;
recordDataFor(this).rollbackAttributes();
record.errors.clear();
this.errors.clear();
currentState.cleanErrorRequests();
}

Expand Down
4 changes: 2 additions & 2 deletions packages/model/addon/-private/promise-many-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { resolve } from 'rsvp';

import type { ManyArray } from 'ember-data/-private';

import type { InternalModel } from '@ember-data/store/-private';
import { StableRecordIdentifier } from '@ember-data/types/q/identifier';
import type { RecordInstance } from '@ember-data/types/q/record-instance';

export interface HasManyProxyCreateArgs {
Expand Down Expand Up @@ -42,7 +42,7 @@ export interface HasManyProxyCreateArgs {
@class PromiseManyArray
@public
*/
export default interface PromiseManyArray extends Omit<ArrayProxy<InternalModel, RecordInstance>, 'destroy'> {}
export default interface PromiseManyArray extends Omit<ArrayProxy<StableRecordIdentifier, RecordInstance>, 'destroy'> {}
export default class PromiseManyArray {
declare promise: Promise<ManyArray> | null;
declare isDestroyed: boolean;
Expand Down
6 changes: 3 additions & 3 deletions packages/model/addon/-private/record-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ export function tagged(_target, key, desc) {
}

/**
Historically InternalModel managed a state machine
the currentState for which was reflected onto Model.
Historically EmberData managed a state machine
for each record, the currentState for which
was reflected onto Model.
This implements the flags and stateName for backwards compat
with the state tree that used to be possible (listed below).
Expand Down Expand Up @@ -304,7 +305,6 @@ export default class RecordState {
return !this.isLoaded && this.pendingCount > 0 && this.fulfilledCount === 0;
}

// TODO @runspired handle "unloadRecord" see note in InternalModel
@tagged
get isLoaded() {
if (this.isNew) {
Expand Down
1 change: 0 additions & 1 deletion packages/private-build-infra/addon/current-deprecations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export default {
DEPRECATE_SNAPSHOT_MODEL_CLASS_ACCESS: '4.5',
DEPRECATE_STORE_FIND: '4.5',
DEPRECATE_HAS_RECORD: '4.5',
DEPRECATE_RECORD_WAS_INVALID: '4.5',
DEPRECATE_STRING_ARG_SCHEMAS: '4.5',
DEPRECATE_JSON_API_FALLBACK: '4.5',
DEPRECATE_MODEL_REOPEN: '4.8',
Expand Down
1 change: 0 additions & 1 deletion packages/private-build-infra/addon/deprecations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const DEPRECATE_RSVP_PROMISE = deprecationState('DEPRECATE_RSVP_PROMISE')
export const DEPRECATE_SNAPSHOT_MODEL_CLASS_ACCESS = deprecationState('DEPRECATE_SNAPSHOT_MODEL_CLASS_ACCESS');
export const DEPRECATE_STORE_FIND = deprecationState('DEPRECATE_STORE_FIND');
export const DEPRECATE_HAS_RECORD = deprecationState('DEPRECATE_HAS_RECORD');
export const DEPRECATE_RECORD_WAS_INVALID = deprecationState('DEPRECATE_RECORD_WAS_INVALID');
export const DEPRECATE_STRING_ARG_SCHEMAS = deprecationState('DEPRECATE_STRING_ARG_SCHEMAS');
export const DEPRECATE_JSON_API_FALLBACK = deprecationState('DEPRECATE_JSON_API_FALLBACK');
export const DEPRECATE_MODEL_REOPEN = deprecationState('DEPRECATE_MODEL_REOPEN');
Expand Down
4 changes: 1 addition & 3 deletions packages/record-data/addon/-private/coerce-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { DEBUG } from '@glimmer/env';
// corresponding record, we will not know if it is a string or a number.
type Coercable = string | number | boolean | null | undefined | symbol;

function coerceId(id: Coercable): string | null {
export function coerceId(id: Coercable): string | null {
if (id === null || id === undefined || id === '') {
return null;
}
Expand All @@ -35,5 +35,3 @@ export function ensureStringId(id: Coercable): string {

return normalized!;
}

export default coerceId;
3 changes: 2 additions & 1 deletion packages/record-data/addon/-private/graph/-utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { assert, inspect, warn } from '@ember/debug';

import { coerceId, recordDataFor as peekRecordData } from '@ember-data/store/-private';
import { recordDataFor as peekRecordData } from '@ember-data/store/-private';
import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
import type { RecordData } from '@ember-data/types/q/record-data';
import type { RelationshipRecordData } from '@ember-data/types/q/relationship-record-data';
import type { Dict } from '@ember-data/types/q/utils';

import { coerceId } from '../coerce-id';
import type BelongsToRelationship from '../relationships/state/belongs-to';
import type ManyRelationship from '../relationships/state/has-many';
import type ImplicitRelationship from '../relationships/state/implicit';
Expand Down
6 changes: 3 additions & 3 deletions packages/record-data/addon/-private/graph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function graphFor(store: RecordDataStoreWrapper | Store): Graph {
* Graph acts as the cache for relationship data. It allows for
* us to ask about and update relationships for a given Identifier
* without requiring other objects for that Identifier to be
* instantiated (such as `InternalModel`, `RecordData` or a `Record`)
* instantiated (such as `RecordData` or a `Record`)
*
* This also allows for us to make more substantive changes to relationships
* with increasingly minor alterations to other portions of the internals
Expand Down Expand Up @@ -398,9 +398,9 @@ function destroyRelationship(rel) {
rel.clear();

// necessary to clear relationships in the ui from dematerialized records
// hasMany is managed by InternalModel which calls `retreiveLatest` after
// hasMany is managed by Model which calls `retreiveLatest` after
// dematerializing the recordData instance.
// but sync belongsTo require this since they don't have a proxy to update.
// but sync belongsTo requires this since they don't have a proxy to update.
// so we have to notify so it will "update" to null.
// we should discuss whether we still care about this, probably fine to just
// leave the ui relationship populated since the record is destroyed and
Expand Down
3 changes: 1 addition & 2 deletions packages/record-data/addon/-private/record-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type {
RelationshipRecordData,
} from '@ember-data/types/q/relationship-record-data';

import coerceId from './coerce-id';
import { isImplicit } from './graph/-utils';
import { graphFor } from './graph/index';
import type BelongsToRelationship from './relationships/state/belongs-to';
Expand Down Expand Up @@ -689,7 +688,7 @@ export default class RecordDataDefault implements RelationshipRecordData {
}

/*
Ember Data has 3 buckets for storing the value of an attribute on an internalModel.
Ember Data has 3 buckets for storing the value of an attribute.
`_data` holds all of the attributes that have been acknowledged by
a backend via the adapter. When rollbackAttributes is called on a model all
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,20 @@ export default class ManyRelationship {
let seen = Object.create(null);

for (let i = 0; i < this.currentState.length; i++) {
const inverseInternalModel = this.currentState[i];
const id = inverseInternalModel.lid;
const inverseIdentifier = this.currentState[i];
const id = inverseIdentifier.lid;
if (!seen[id]) {
seen[id] = true;
callback(inverseInternalModel);
callback(inverseIdentifier);
}
}

for (let i = 0; i < this.canonicalState.length; i++) {
const inverseInternalModel = this.canonicalState[i];
const id = inverseInternalModel.lid;
const inverseIdentifier = this.canonicalState[i];
const id = inverseIdentifier.lid;
if (!seen[id]) {
seen[id] = true;
callback(inverseInternalModel);
callback(inverseIdentifier);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/store/addon/-debug/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DEBUG } from '@glimmer/env';
relationship (specified via `relationshipMeta`) of the `record`.
This utility should only be used internally, as both record parameters must
be an InternalModel and the `relationshipMeta` needs to be the meta
be stable record identifiers and the `relationshipMeta` needs to be the meta
information about the relationship, retrieved via
`record.relationshipFor(key)`.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/store/addon/-private/caches/identifier-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ export class IdentifierCache {
/**
* Internal hook to allow management of merge conflicts with identifiers.
*
* we allow late binding of this private internal merge so that `internalModelFactory`
* can insert itself here to handle elimination of duplicates
* we allow late binding of this private internal merge so that
* the cache can insert itself here to handle elimination of duplicates
*
* @method __configureMerge
* @private
Expand Down
Loading

0 comments on commit c3f3068

Please sign in to comment.