Skip to content

Commit

Permalink
Ensure generating a new RecordReference doesnt require stamping out a…
Browse files Browse the repository at this point in the history
…n internalModel
  • Loading branch information
snewcomer committed Sep 27, 2020
1 parent 31b0de9 commit 140b1d8
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ module('integration/references/belongs-to', function(hooks) {
var personReference = store.getReference('person', 1);
var familyReference = person.belongsTo('family');

assert.ok(personReference);
assert.equal(familyReference.parent, personReference);
assert.ok(personReference, 'person reference is present');
assert.deepEqual(familyReference.parent, personReference, 'parent reference on BelongsToReference');
});

test('BelongsToReference#meta() returns the most recent meta for the relationship', async function(assert) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ module('integration/references/has-many', function(hooks) {
var familyReference = store.getReference('family', 1);
var personsReference = family.hasMany('persons');

assert.ok(familyReference);
assert.equal(personsReference.parent, familyReference);
assert.ok(familyReference, 'person reference is present');
assert.deepEqual(personsReference.parent, familyReference, 'parent reference on HasManyReferencee');
});

test('HasManyReference#meta() returns the most recent meta for the relationship', function(assert) {
Expand Down
16 changes: 15 additions & 1 deletion packages/store/addon/-private/system/core-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import { promiseArray, promiseObject } from './promise-proxies';
import RecordArrayManager from './record-array-manager';
import recordDataFor from './record-data-for';
import NotificationManager from './record-notification-manager';
import { RecordReference } from './references';
import { RequestPromise } from './request-cache';
import { _bind, _guard, _objectIsAlive, guardDestroyedStore } from './store/common';
import { _find, _findAll, _findBelongsTo, _findHasMany, _findMany, _query, _queryRecord } from './store/finders';
Expand Down Expand Up @@ -113,6 +114,8 @@ type PendingSaveItem = {

let _Model;

const RECORD_REFERENCES = new WeakMap<StableRecordIdentifier, RecordReference>();

function getModel() {
if (HAS_MODEL_PACKAGE) {
_Model = _Model || require('@ember-data/model').default;
Expand Down Expand Up @@ -1438,7 +1441,18 @@ abstract class CoreStore extends Service {
const normalizedId = ensureStringId(id);
const resource: ResourceIdentifierObject = constructResource(type, normalizedId);

return internalModelFactoryFor(this).lookup(resource).recordReference;
if (RECORD_ARRAY_MANAGER_IDENTIFIERS) {
const identifier: StableRecordIdentifier = identifierCacheFor(this).getOrCreateRecordIdentifier(resource);
if (RECORD_REFERENCES.has(identifier)) {
return RECORD_REFERENCES.get(identifier);
}

const reference = new RecordReference(this, identifier);
RECORD_REFERENCES.set(identifier, reference);
return reference;
} else {
return internalModelFactoryFor(this).lookup(resource).recordReference;
}
}

/**
Expand Down
27 changes: 22 additions & 5 deletions packages/store/addon/-private/system/references/record.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import RSVP, { resolve } from 'rsvp';

import Reference, { internalModelForReference } from './reference';
import { RECORD_ARRAY_MANAGER_IDENTIFIERS } from '@ember-data/canary-features';

import Reference, { internalModelForReference, REFERENCE_CACHE } from './reference';

type SingleResourceDocument = import('../../ts-interfaces/ember-data-json-api').SingleResourceDocument;
type RecordInstance = import('../../ts-interfaces/record-instance').RecordInstance;
type StableRecordIdentifier = import('../../ts-interfaces/identifier').StableRecordIdentifier;

/**
@module @ember-data/store
Expand All @@ -17,12 +20,26 @@ type RecordInstance = import('../../ts-interfaces/record-instance').RecordInstan
@extends Reference
*/
export default class RecordReference extends Reference {
public get type() {
return internalModelForReference(this)!.modelName;
public get type(): string {
if (RECORD_ARRAY_MANAGER_IDENTIFIERS) {
let identifier = REFERENCE_CACHE.get(this) as StableRecordIdentifier;
return identifier.type;
} else {
return internalModelForReference(this)!.modelName;
}
}

private get _id() {
return internalModelForReference(this)!.id;
private get _id(): string | null {
if (RECORD_ARRAY_MANAGER_IDENTIFIERS) {
let identifier = REFERENCE_CACHE.get(this) as StableRecordIdentifier;
if (identifier) {
return identifier.id;
}

return null;
} else {
return internalModelForReference(this)!.id;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function isResourceIdentiferWithRelatedLinks(
return value && value.links && value.links.related;
}

const REFERENCE_CACHE = new WeakMap<Reference, InternalModel | StableRecordIdentifier>();
export const REFERENCE_CACHE = new WeakMap<Reference, InternalModel | StableRecordIdentifier>();

export function internalModelForReference(reference: Reference): InternalModel | null | undefined {
if (RECORD_ARRAY_MANAGER_IDENTIFIERS) {
Expand Down

0 comments on commit 140b1d8

Please sign in to comment.