Skip to content

Commit

Permalink
perf: optimize factory.create (#8140)
Browse files Browse the repository at this point in the history
* perf: optimize factory.create

* fix toString test

* fix

* remove guidFor
  • Loading branch information
runspired authored Aug 22, 2022
1 parent 274d59e commit ba1f566
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
7 changes: 1 addition & 6 deletions packages/-ember-data/tests/unit/model-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { computed, get, observer, set } from '@ember/object';
import { guidFor } from '@ember/object/internals';

import { module, test } from 'qunit';
import { reject, resolve } from 'rsvp';
Expand Down Expand Up @@ -196,11 +195,7 @@ module('unit/model - Model', function (hooks) {
},
});

assert.strictEqual(
person.toString(),
`<dummy@model:${person.constructor.modelName}::${guidFor(person)}:1>`,
'reports id in toString'
);
assert.strictEqual(person.toString(), `<model::${person.constructor.modelName}:1>`, 'reports id in toString');
});

testInDebug('trying to use `id` as an attribute should raise', async function (assert) {
Expand Down
13 changes: 6 additions & 7 deletions packages/model/addon/-private/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,18 @@ class Model extends EmberObject {
}
const createProps = options._createProps;
const _secretInit = options._secretInit;
delete options._createProps;
delete options._secretInit;
options._createProps = null;
options._secretInit = null;
super.init(options);

_secretInit(this);
let identity = _secretInit.identifier;
_secretInit.cb(this, _secretInit.recordData, identity, _secretInit.store);
this.___recordState = DEBUG ? new RecordState(this) : null;

this.setProperties(createProps);

let store = storeFor(this);
let notifications = store._notificationManager;
let identity = recordIdentifierFor(this);

this.___private_notifications = notifications.subscribe(identity, (identifier, type, key) => {
notifyChanges(identifier, type, key, this, store);
Expand Down Expand Up @@ -493,9 +493,8 @@ class Model extends EmberObject {
}
}

// TODO just write a nice toString
toStringExtension() {
return this.id;
toString() {
return `<model::${this.constructor.modelName}:${this.id}>`;
}

/**
Expand Down
25 changes: 17 additions & 8 deletions packages/store/addon/-private/store-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,25 +322,23 @@ class Store extends Service {
): DSModel | RecordInstance {
if (HAS_MODEL_PACKAGE) {
let modelName = identifier.type;
let store = this;

let recordData = this._instanceCache.getRecordData(identifier);
let createOptions: any = {
// TODO deprecate allowing unknown args setting
_createProps: createRecordArgs,
// TODO @deprecate consider deprecating accessing record properties during init which the below is necessary for
_secretInit: (record: RecordInstance): void => {
setRecordIdentifier(record, identifier);
StoreMap.set(record, store);
setRecordDataFor(record, recordData);
_secretInit: {
identifier,
recordData,
store: this,
cb: secretInit,
},
container: null, // necessary hack for setOwner?
};

// ensure that `getOwner(this)` works inside a model instance
setOwner(createOptions, getOwner(this));
delete createOptions.container;
return getModelFactory(this, this._modelFactoryCache, modelName).create(createOptions);
return getModelFactory(this, this._modelFactoryCache, modelName).class.create(createOptions);
}
assert(`You must implement the store's instantiateRecord hook for your custom model class.`);
}
Expand Down Expand Up @@ -2780,3 +2778,14 @@ function extractIdentifierFromRecord(
function isPromiseRecord(record: PromiseProxyRecord | RecordInstance): record is PromiseProxyRecord {
return !!record.then;
}

function secretInit(
record: RecordInstance,
recordData: RecordData,
identifier: StableRecordIdentifier,
store: Store
): void {
setRecordIdentifier(record, identifier);
StoreMap.set(record, store);
setRecordDataFor(record, recordData);
}

0 comments on commit ba1f566

Please sign in to comment.