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

{beta} Beta unload and create #5098

Merged
merged 2 commits into from
Jul 28, 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
21 changes: 21 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@

### Master

### Release 2.15.0-beta.3 (July 27, 2017)

- [#5033](https://github.com/emberjs/data/pull/5033) loosen header parsing slightly
- [#4965](https://github.com/emberjs/data/pull/4965) [BUGFIX beta] Skip test which doesn't play nicely with the latest em…
- [84041a5f](https://github.com/emberjs/data/commit/84041a5f73b5b2a8f6d6d6475f7aa71d5b83b939) Update RELEASE.md
- [#4959](https://github.com/emberjs/data/pull/4959) [BUGFIX] remove forgotten broccoli-stew import
- [#4947](https://github.com/emberjs/data/pull/4947) remove needless change events when creating a recordArrays
- [#5071](https://github.com/emberjs/data/pull/5071) [BACKPORT] Update doc for deprecated function.
- [#5002](https://github.com/emberjs/data/pull/5002) [BUGFIX BETA] Added `system/store/container-instance-cache` to the -private export
- [#4992](https://github.com/emberjs/data/pull/4992) Use https in references to emberjs website (#4992)
- [#4969](https://github.com/emberjs/data/pull/4969) [BUGFIX beta] Ensure Engines can boot without error.
- [#4971](https://github.com/emberjs/data/pull/4971) Fix typo in function call
- [#5008](https://github.com/emberjs/data/pull/5008) [BUGFIX BETA]: Fixed export regression. `ember-data/transform` to be default.
- [#5022](https://github.com/emberjs/data/pull/5022) Ensure `ember-data/-private` module is emitted properly for ember-cli < 2.12. (#5022)
- [#5031](https://github.com/emberjs/data/pull/5031) Ensure `ember-data/-private` module is emitted properly for ember-cli < 2.12. (#5031)
- [#5068](https://github.com/emberjs/data/pull/5068) [BUGFIX release] Preserve local relationship changes after persisting a delet…
- [#5079](https://github.com/emberjs/data/pull/5079) [BACKPORT 2 release] Release fix unload after destroy
- [#5086](https://github.com/emberjs/data/pull/5086) {release} [BUGFIX release] handle dupe relationship entries
- [#5091](https://github.com/emberjs/data/pull/5091) {release} Bump `amd-name-resolver` version to enable parallel babel transpile
- [#5092](https://github.com/emberjs/data/pull/5092) [BUGFIX release] relationship [x, y] should not break on x.unloadRecord()

### Release 2.15.0-beta.2 (July 11, 2017)
- [#5032](https://github.com/emberjs/data/pull/5032) loosen header parsing slightly
- [#5048](https://github.com/emberjs/data/pull/5048) [BUGFIX release] createRecord should only setup relationships it has … (#5048)
Expand Down
22 changes: 22 additions & 0 deletions addon/-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ export default class InternalModel {
}
}

hasScheduledDestroy() {
return !!this._scheduledDestroy;
}

cancelDestroy() {
assert(`You cannot cancel the destruction of an InternalModel once it has already been destroyed`, !this.isDestroyed);

Expand All @@ -502,6 +506,24 @@ export default class InternalModel {
this._scheduledDestroy = null;
}

// typically, we prefer to async destroy this lets us batch cleanup work.
// Unfortunately, some scenarios where that is not possible. Such as:
//
// ```js
// const record = store.find(‘record’, 1);
// record.unloadRecord();
// store.createRecord(‘record’, 1);
// ```
//
// In those scenarios, we make that model's cleanup work, sync.
//
destroySync() {
if (this._isDematerializing) {
this.cancelDestroy();
}
this._checkForOrphanedInternalModels();
}

_checkForOrphanedInternalModels() {
this._isDematerializing = false;
this._scheduledDestroy = null;
Expand Down
14 changes: 11 additions & 3 deletions addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -2557,15 +2557,23 @@ Store = Service.extend({

assert(`You can no longer pass a modelClass as the first argument to store._buildInternalModel. Pass modelName instead.`, typeof modelName === 'string');

let recordMap = this._internalModelsFor(modelName);
let internalModels = this._internalModelsFor(modelName);
let existingInternalModel = internalModels.get(id);

assert(`The id ${id} has already been used with another record for modelClass '${modelName}'.`, !id || !recordMap.get(id));
if (existingInternalModel && existingInternalModel.hasScheduledDestroy()) {
// unloadRecord is async, if one attempts to unload + then sync create,
// we must ensure the unload is complete before starting the create
existingInternalModel.destroySync();
existingInternalModel = null;
}

assert(`The id ${id} has already been used with another record for modelClass '${modelName}'.`, !existingInternalModel);

// lookupFactory should really return an object that creates
// instances with the injections applied
let internalModel = new InternalModel(modelName, id, this, data);

recordMap.add(internalModel, id);
internalModels.add(internalModel, id);

return internalModel;
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ember-data",
"version": "2.15.0-beta.2",
"version": "2.15.0-beta.3",
"description": "A data layer for your Ember applications.",
"repository": "git://github.com/emberjs/data.git",
"directories": {
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/store/unload-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ test('unload a record', function(assert) {
});
});

test('unload followed by create of the same type + id', function(assert) {
let record = run(() => store.createRecord('record', { id: 1 }));

assert.ok(store.recordForId('record', 1) === record, 'record should exactly equal');

return run(() => {
record.unloadRecord();
let createdRecord = store.createRecord('record', { id: 1 });
assert.ok(record !== createdRecord, 'newly created record is fresh (and was created)');
});
});

module("DS.Store - unload record with relationships");

test('can commit store after unload record with relationships', function(assert) {
Expand Down