Skip to content

Commit

Permalink
adds an explicit IdentityMap construct
Browse files Browse the repository at this point in the history
  • Loading branch information
runspired committed Dec 9, 2016
1 parent b557fba commit df21a7b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
28 changes: 28 additions & 0 deletions addon/-private/system/identity-map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import RecordMap from './record-map';
import EmptyObject from './empty-object';

export default class IdentityMap {
constructor() {
this._map = new EmptyObject();
}

retrieve(modelName) {
let recordMap = this._map[modelName];

if (!recordMap) {
recordMap = this._map[modelName] = new RecordMap(modelName);
}

return recordMap;
}

clear() {
let recordMaps = this._map;
let keys = Object.keys(recordMaps);

for (let i = 0; i < keys.length; i++) {
let key = keys[i];
recordMaps[key].clear();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import EmptyObject from 'ember-data/-private/system/empty-object';

const NULL_RESOLUTION = new EmptyObject();

export default class ClassMeta {
constructor(store, modelName) {
constructor(modelName) {
this.modelName = modelName;
this._store = store;
this._modelClass = null;
this._idToRecord = null;
this._records = null;
this._metadata = null;
Expand All @@ -24,17 +20,9 @@ export default class ClassMeta {
return this._metadata || (this._metadata = new EmptyObject());
}

get modelClass() {
if (!this._modelClass) {
this._modelClass = this._store.modelFor(this.modelName) || NULL_RESOLUTION;
}

return this._modelClass === NULL_RESOLUTION ? null : this._modelClass;
}

/* deprecated way of accessing modelClass */
get type() {
return this.modelClass;
throw new Error('RecordMap.type is no longer available');
}

clear() {
Expand Down
22 changes: 4 additions & 18 deletions addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Model from 'ember-data/model';
import { instrument, assert, deprecate, warn, runInDebug } from "ember-data/-private/debug";
import normalizeModelName from "ember-data/-private/system/normalize-model-name";
import { InvalidError } from 'ember-data/adapters/errors';
import ClassMeta from 'ember-data/-private/system/class-meta';
import IdentityMap from 'ember-data/-private/system/identity-map';
import {
promiseArray,
promiseObject
Expand Down Expand Up @@ -213,7 +213,7 @@ Store = Service.extend({
store: this
});

this._recordMaps = new EmptyObject();
this._identityMap = new IdentityMap();
this._pendingSave = [];
this._instanceCache = new ContainerInstanceCache(getOwner(this), this);

Expand Down Expand Up @@ -1665,13 +1665,7 @@ Store = Service.extend({
assert('Passing classes to store methods has been removed. Please pass a dasherized string instead of '+ inspect(modelName), !modelName || typeof modelName === 'string');

if (arguments.length === 0) {
let recordMaps = this._recordMaps;
let keys = Object.keys(recordMaps);

for (let i = 0; i < keys.length; i++) {
let key = keys[i];
recordMaps[key].clear();
}
this._identityMap.clear();
} else {
let trueModelName = this._classKeyFor(modelName);
this._recordMapFor(trueModelName).clear();
Expand Down Expand Up @@ -1975,15 +1969,7 @@ Store = Service.extend({
*/
_recordMapFor(modelName) {
heimdall.increment(_recordMapFor);
let recordMaps = this._recordMaps;
let recordMap = recordMaps[modelName];

if (recordMap) { return recordMap; }

recordMap = new ClassMeta(this, modelName);
recordMaps[modelName] = recordMap;

return recordMap;
return this._identityMap.retrieve(modelName);
},

// ................
Expand Down

0 comments on commit df21a7b

Please sign in to comment.