Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Backburner Usage #4723

Merged
merged 4 commits into from
Jan 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions addon/-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const {
inspect,
isEmpty,
isEqual,
run: emberRun,
setOwner,
RSVP,
RSVP: { Promise }
Expand Down Expand Up @@ -122,6 +121,7 @@ export default class InternalModel {
this._isDestroyed = false;
this.isError = false;
this.error = null;
this._isUpdatingRecordArrays = false;

// caches for lazy getters
this._modelClass = null;
Expand Down Expand Up @@ -376,7 +376,7 @@ export default class InternalModel {
}

becameReady() {
emberRun.schedule('actions', this.store.recordArrayManager, this.store.recordArrayManager.recordWasLoaded, this);
this.store.recordArrayManager.recordWasLoaded(this);
}

didInitializeData() {
Expand Down Expand Up @@ -521,7 +521,7 @@ export default class InternalModel {
*/
adapterDidDirty() {
this.send('becomeDirty');
this.updateRecordArraysLater();
this.updateRecordArrays();
}

/*
Expand Down Expand Up @@ -654,7 +654,7 @@ export default class InternalModel {
setups[i].setup(this);
}

this.updateRecordArraysLater();
this.updateRecordArrays();
}

_unhandledEvent(state, name, context) {
Expand All @@ -673,7 +673,8 @@ export default class InternalModel {
if (this._deferredTriggers.push(args) !== 1) {
return;
}
emberRun.schedule('actions', this, this._triggerDeferredTriggers);

this.store._updateInternalModel(this);
}

_triggerDeferredTriggers() {
Expand Down Expand Up @@ -784,8 +785,9 @@ export default class InternalModel {
@private
*/
updateRecordArrays() {
this._updatingRecordArraysLater = false;
this.store._dataWasUpdated(this);
if (this._isUpdatingRecordArrays) { return; }
this._isUpdatingRecordArrays = true;
this.store.recordArrayManager.recordDidChange(this);
}

setId(id) {
Expand Down Expand Up @@ -843,24 +845,13 @@ export default class InternalModel {
this._inFlightAttributes = new EmptyObject();

this.send('didCommit');
this.updateRecordArraysLater();
this.updateRecordArrays();

if (!data) { return; }

this.record._notifyProperties(changedKeys);
}

/*
@method updateRecordArraysLater
@private
*/
updateRecordArraysLater() {
// quick hack (something like this could be pushed into run.once
if (this._updatingRecordArraysLater) { return; }
this._updatingRecordArraysLater = true;
emberRun.schedule('actions', this, this.updateRecordArrays);
}

addErrorMessageToAttribute(attribute, message) {
get(this.getRecord(), 'errors')._add(attribute, message);
}
Expand Down
2 changes: 1 addition & 1 deletion addon/-private/system/model/states.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function didSetProperty(internalModel, context) {
internalModel.send('becomeDirty');
}

internalModel.updateRecordArraysLater();
internalModel.updateRecordArrays();
}

// Implementation notes:
Expand Down
56 changes: 40 additions & 16 deletions addon/-private/system/record-array-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
_addRecordToRecordArray,
_recordWasChanged,
_recordWasDeleted,
_flushLoadedRecords,
array_flatten,
array_remove,
create,
Expand All @@ -41,6 +42,7 @@ const {
'_addInternalModelToRecordArray',
'_recordWasChanged',
'_recordWasDeleted',
'_flushLoadedRecords',
'array_fatten',
'array_remove',
'create',
Expand Down Expand Up @@ -80,12 +82,13 @@ export default Ember.Object.extend({
});

this.changedRecords = [];
this.loadedRecords = [];
this._adapterPopulatedRecordArrays = [];
},

recordDidChange(record) {
recordDidChange(internalModel) {
heimdall.increment(recordDidChange);
if (this.changedRecords.push(record) !== 1) { return; }
if (this.changedRecords.push(internalModel) !== 1) { return; }

emberRun.schedule('actions', this, this.updateRecordArrays);
},
Expand All @@ -108,17 +111,22 @@ export default Ember.Object.extend({
*/
updateRecordArrays() {
heimdall.increment(updateRecordArrays);
this.changedRecords.forEach(internalModel => {
let updated = this.changedRecords;

for (let i = 0, l = updated.length; i < l; i++) {
let internalModel = updated[i];

if (internalModel.isDestroyed ||
internalModel.currentState.stateName === 'root.deleted.saved') {
internalModel.currentState.stateName === 'root.deleted.saved') {
this._recordWasDeleted(internalModel);
} else {
this._recordWasChanged(internalModel);
}
});

this.changedRecords.length = 0;
internalModel._isUpdatingRecordArrays = false;
}

updated.length = 0;
},

_recordWasDeleted(internalModel) {
Expand Down Expand Up @@ -146,19 +154,35 @@ export default Ember.Object.extend({
//Need to update live arrays on loading
recordWasLoaded(internalModel) {
heimdall.increment(recordWasLoaded);
let modelName = internalModel.modelName;
let recordArrays = this.filteredRecordArrays.get(modelName);
let filter;
if (this.loadedRecords.push(internalModel) !== 1) { return; }

recordArrays.forEach(array => {
filter = get(array, 'filterFunction');
this.updateFilterRecordArray(array, filter, modelName, internalModel);
});
emberRun.schedule('actions', this, this._flushLoadedRecords);
},

_flushLoadedRecords() {
heimdall.increment(_flushLoadedRecords);
let internalModels = this.loadedRecords;

for (let i = 0, l = internalModels.length; i < l; i++) {
let internalModel = internalModels[i];
let modelName = internalModel.modelName;

let recordArrays = this.filteredRecordArrays.get(modelName);
let filter;

if (this.liveRecordArrays.has(modelName)) {
let liveRecordArray = this.liveRecordArrays.get(modelName);
this._addInternalModelToRecordArray(liveRecordArray, internalModel);
for (let j = 0, rL = recordArrays.length; j < rL; j++) {
let array = recordArrays[j];
filter = get(array, 'filterFunction');
this.updateFilterRecordArray(array, filter, modelName, internalModel);
}

if (this.liveRecordArrays.has(modelName)) {
let liveRecordArray = this.liveRecordArrays.get(modelName);
this._addInternalModelToRecordArray(liveRecordArray, internalModel);
}
}

this.loadedRecords.length = 0;
},

/**
Expand Down
15 changes: 10 additions & 5 deletions addon/-private/system/relationships/state/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,18 @@ export default class Relationships {

get(key) {
let relationships = this.initializedRelationships;
let internalModel = this.internalModel;
let relationshipsByName = get(internalModel.type, 'relationshipsByName');
let relationship = relationships[key];

if (!relationships[key] && relationshipsByName.get(key)) {
relationships[key] = createRelationshipFor(internalModel, relationshipsByName.get(key), internalModel.store);
if (!relationship) {
let internalModel = this.internalModel;
let relationshipsByName = get(internalModel.type, 'relationshipsByName');
let rel = relationshipsByName.get(key);

if (rel) {
relationship = relationships[key] = createRelationshipFor(internalModel, rel, internalModel.store);
}
}

return relationships[key];
return relationship;
}
}
22 changes: 6 additions & 16 deletions addon/-private/system/relationships/state/has-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,30 +127,20 @@ export default class ManyRelationship extends Relationship {
}

computeChanges(records) {
var members = this.canonicalMembers;
var recordsToRemove = [];
var length;
var record;
var i;

records = setForArray(records);
let members = this.canonicalMembers;
let recordsToRemove = [];
let recordSet = setForArray(records);

members.forEach(function(member) {
if (records.has(member)) { return; }
if (recordSet.has(member)) { return; }

recordsToRemove.push(member);
});

this.removeCanonicalRecords(recordsToRemove);

// Using records.toArray() since currently using
// removeRecord can modify length, messing stuff up
// forEach since it directly looks at "length" each
// iteration
records = records.toArray();
length = records.length;
for (i = 0; i < length; i++) {
record = records[i];
for (let i = 0, l = records.length; i < l; i++) {
let record = records[i];
this.removeCanonicalRecord(record);
this.addCanonicalRecord(record, i);
}
Expand Down
14 changes: 8 additions & 6 deletions addon/-private/system/relationships/state/relationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export default class Relationship {
}
record._implicitRelationships[this.inverseKeyForImplicit].addRecord(this.record);
}
this.record.updateRecordArraysLater();
this.record.updateRecordArrays();
}
this.setHasData(true);
}
Expand Down Expand Up @@ -235,18 +235,20 @@ export default class Relationship {

flushCanonical() {
heimdall.increment(flushCanonical);
let list = this.members.list;
this.willSync = false;
//a hack for not removing new records
//TODO remove once we have proper diffing
var newRecords = [];
for (var i = 0; i < this.members.list.length; i++) {
if (this.members.list[i].isNew()) {
newRecords.push(this.members.list[i]);
for (let i = 0; i < list.length; i++) {
if (list[i].isNew()) {
newRecords.push(list[i]);
}
}

//TODO(Igor) make this less abysmally slow
this.members = this.canonicalMembers.copy();
for (i = 0; i < newRecords.length; i++) {
for (let i = 0; i < newRecords.length; i++) {
this.members.add(newRecords[i]);
}
}
Expand All @@ -257,7 +259,7 @@ export default class Relationship {
return;
}
this.willSync = true;
this.store._backburner.join(() => this.store._backburner.schedule('syncRelationships', this, this.flushCanonical));
this.store._updateRelationshipState(this);
}

updateLink(link) {
Expand Down
Loading