Skip to content

Commit

Permalink
rewrite with feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaurav0 committed Apr 11, 2020
1 parent db10e73 commit 157eaff
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
6 changes: 3 additions & 3 deletions packages/store/addon/-private/system/references/belongs-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ export default class BelongsToReference extends Reference {
id() {
let id = null;
let resource = this._resource();
if (resource && resource.data && resource.data.id) {
if (resource && resource.data) {
id = resource.data.id;
}
return id;
}

_resource() {
return this.recordData.getBelongsTo(this.key);
return INTERNAL_MODELS.get(this)?._recordData.getBelongsTo(this.key);
}

/**
Expand Down Expand Up @@ -144,7 +144,7 @@ export default class BelongsToReference extends Reference {
}

assertPolymorphicType(
INTERNAL_MODELS.get(this.recordData),
INTERNAL_MODELS.get(this),
this.belongsToRelationship.relationshipMeta,
record._internalModel,
this.store
Expand Down
10 changes: 5 additions & 5 deletions packages/store/addon/-private/system/references/has-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class HasManyReference extends Reference {
}

_resource() {
return this.recordData.getHasMany(this.key);
return INTERNAL_MODELS.get(this)?._recordData.getHasMany(this.key);
}

/**
Expand Down Expand Up @@ -178,7 +178,7 @@ export default class HasManyReference extends Reference {
array = payload.data;
}

let internalModel = INTERNAL_MODELS.get(this.recordData);
let internalModel = INTERNAL_MODELS.get(this);

let internalModels = array.map(obj => {
let record = this.store.push(obj);
Expand Down Expand Up @@ -254,7 +254,7 @@ export default class HasManyReference extends Reference {
@return {ManyArray}
*/
value() {
let internalModel = INTERNAL_MODELS.get(this.recordData);
let internalModel = INTERNAL_MODELS.get(this);
if (this._isLoaded()) {
return internalModel.getManyArray(this.key);
}
Expand Down Expand Up @@ -325,7 +325,7 @@ export default class HasManyReference extends Reference {
this has-many relationship.
*/
load(options) {
let internalModel = INTERNAL_MODELS.get(this.recordData);
let internalModel = INTERNAL_MODELS.get(this);
return internalModel.getHasMany(this.key, options);
}

Expand Down Expand Up @@ -378,7 +378,7 @@ export default class HasManyReference extends Reference {
@return {Promise} a promise that resolves with the ManyArray in this has-many relationship.
*/
reload(options) {
let internalModel = INTERNAL_MODELS.get(this.recordData);
let internalModel = INTERNAL_MODELS.get(this);
return internalModel.reloadHasMany(this.key, options);
}
}
14 changes: 10 additions & 4 deletions packages/store/addon/-private/system/references/record.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import RSVP, { resolve } from 'rsvp';

import Reference from './reference';
import Reference, { INTERNAL_MODELS } from './reference';

type SingleResourceDocument = import('../../ts-interfaces/ember-data-json-api').SingleResourceDocument;
type RecordInstance = import('../../ts-interfaces/record-instance').RecordInstance;
Expand All @@ -17,9 +17,12 @@ type RecordInstance = import('../../ts-interfaces/record-instance').RecordInstan
@extends Reference
*/
export default class RecordReference extends Reference {
public type = this.recordData.modelName;
public get type() {
return INTERNAL_MODELS.get(this)!.modelName;
}

private get _id() {
return this.recordData.id;
return INTERNAL_MODELS.get(this)!.id;
}

/**
Expand Down Expand Up @@ -125,7 +128,10 @@ export default class RecordReference extends Reference {
*/
value(): RecordInstance | null {
if (this._id !== null) {
return this.store.peekRecord(this.type, this._id);
let internalModel = INTERNAL_MODELS.get(this);
if (internalModel && internalModel.isLoaded()) {
return internalModel.getRecord();
}
}
return null;
}
Expand Down
10 changes: 3 additions & 7 deletions packages/store/addon/-private/system/references/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { deprecate } from '@ember/debug';
import { FULL_LINKS_ON_RELATIONSHIPS } from '@ember-data/canary-features';
import { DEPRECATE_REFERENCE_INTERNAL_MODEL } from '@ember-data/private-build-infra/deprecations';

import recordDataFor from '../record-data-for';

type Dict<T> = import('../../ts-interfaces/utils').Dict<T>;
type JsonApiRelationship = import('../../ts-interfaces/record-data-json-api').JsonApiRelationship;
type PaginationLinks = import('../../ts-interfaces/ember-data-json-api').PaginationLinks;
Expand All @@ -13,7 +11,6 @@ type CoreStore = import('../core-store').default;
type JSONObject = import('json-typescript').Object;
type JSONValue = import('json-typescript').Value;
type InternalModel = import('../model/internal-model').default;
type RecordData = InternalModel['_recordData'];

/**
@module @ember-data/store
Expand All @@ -32,7 +29,7 @@ function isResourceIdentiferWithRelatedLinks(
return value && value.links && value.links.related;
}

export const INTERNAL_MODELS = new WeakMap<RecordData, InternalModel>();
export const INTERNAL_MODELS = new WeakMap<Reference, InternalModel>();

/**
This is the baseClass for the different References
Expand All @@ -46,8 +43,7 @@ interface Reference {
abstract class Reference {
public recordData: InternalModel['_recordData'];
constructor(public store: CoreStore, internalModel: InternalModel) {
this.recordData = recordDataFor(internalModel);
INTERNAL_MODELS.set(this.recordData, internalModel);
INTERNAL_MODELS.set(this, internalModel);
}

public _resource(): ResourceIdentifier | JsonApiRelationship | void {}
Expand Down Expand Up @@ -216,7 +212,7 @@ if (DEPRECATE_REFERENCE_INTERNAL_MODEL) {
until: '3.21',
});

return INTERNAL_MODELS.get(this.recordData);
return INTERNAL_MODELS.get(this);
},
});
}
Expand Down
4 changes: 0 additions & 4 deletions packages/store/addon/-private/ts-interfaces/record-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ export interface ChangedAttributesHash {
}

export interface RecordData {
id: string | null;
clientId: string | null;
modelName: string;

getResourceIdentifier(): RecordIdentifier | undefined;
pushData(data: JsonApiResource, calculateChange?: boolean): void;
clientDidCreate(): void;
Expand Down

0 comments on commit 157eaff

Please sign in to comment.