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

unloadRecord does not remove observers #4277

Conversation

ryanpatrickcook
Copy link
Contributor

Issue: #4126

This is a work in progress to get conversation going on what the expectation should be. The test shows the issue where when you unload a record from the store, the observers on that object are not torn down.

  • Should unloadRecord set the object as destroyed?
  • Should unloadRecord clear observers?

@bmac
Copy link
Member

bmac commented Mar 28, 2016

@ryanpatrickcook I believe unloadRecord should destroy the object and clear observers.

@ryanpatrickcook
Copy link
Contributor Author

That is what I would think as well, but it doesn't appear to be clearing observers. There is a twiddle in the issue linked that shows an example. In the test written here, it also appears that record.isDestroyed is false after unloading a record. Maybe the test is not accurate.

@pangratz
Copy link
Member

pangratz commented Apr 9, 2016

Hey @ryanpatrickcook, this has been discussed in this weeks' team meeting and we decided that the record should indeed be destroyed when it's unloaded.

Sorry for the delay, but can you update this PR to do the following:

  • Destroy the record within store#unloadRecord()
  • Change this code so it re-uses the stores' unloadRecord so the record is destroyed too
  • Update the test so a) the setup (push to store, add observer), b) the destroying and c) the assertions are in a separate run-loop

After those changes the test should be green and this should be good to go! 🚀

@pangratz
Copy link
Member

pangratz commented May 4, 2016

@ryanpatrickcook do you think you have time to update this PR? Happy to help or take over if you won't have time in the near future...

@ryanpatrickcook ryanpatrickcook force-pushed the Unload-Record-Does-Not-Remove-Observers branch from c9bab2e to 9788587 Compare May 4, 2016 21:06
@ryanpatrickcook
Copy link
Contributor Author

@pangratz I've updated with your input. Let me know what you think. Thanks!

@ryanpatrickcook ryanpatrickcook force-pushed the Unload-Record-Does-Not-Remove-Observers branch from 9788587 to c7b827f Compare May 4, 2016 22:07
@@ -86,7 +86,7 @@ test("unload a record", function(assert) {
store.unloadRecord(record);
});

assert.equal(get(record, 'hasDirtyAttributes'), false, "record is not dirty");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using Ember 1.13.13, after a record was unloaded/destroyed, get(record, 'hasDirtyAttributes') returned undefined. Since the record was now destroyed, I felt as though checking hasDirtyAttributes was no longer valid. Replaced with an isDestroyed check. Would it be preferred that hasDirtyAttributes responds with false?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, not sure what the expected behavior of hasDirtyAttributes is here. I will bring this up in the next meeting. Good point @ryanpatrickcook 👍

Copy link
Member

@pangratz pangratz May 9, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked and it seems that hasDirtyAttributes is false after a record is destroyed (both for 1.13 and 2.5). See this ember-twiddle. Am I missing something?

I think in this case it would be ok to add back the assertion for hasDirtyAttributes...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 1.13, it looks like if the assertion for hasDirtyAttributes is undefined when outside of the run-loop with unloadRecord. When inside the run-loop with unloadRecord, it returns false.

@ryanpatrickcook ryanpatrickcook force-pushed the Unload-Record-Does-Not-Remove-Observers branch from c7b827f to 51fdcd6 Compare May 13, 2016 14:59
@pangratz pangratz self-assigned this May 13, 2016
});

assert.equal(get(record, 'hasDirtyAttributes'), false, "record is not dirty");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason this assertion is failing is due to the implementation of hasDirtyAttributes in DS.Model. It looks like a change to the currentState.isDirty dependency of the computed property is triggered when the record is unloaded.

@emberjs/ember-data-contributors are there any objections in changing the implementation so it re-uses the retrieveFromCurrentState function, like we already do for the other flags like isDirty?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ryanpatrickcook so, we decided to change the implementation of the hasDirtyAttributes CP on the model, by aligning it to the implementation of the other flags. Can you change the dependent key here to be just currentState and add back the assertion. After that, this PR is good to go!

@ryanpatrickcook ryanpatrickcook force-pushed the Unload-Record-Does-Not-Remove-Observers branch from 51fdcd6 to 9ab9c07 Compare May 24, 2016 01:31
@ryanpatrickcook ryanpatrickcook changed the title [wip] unloadRecord does not remove observers unloadRecord does not remove observers May 24, 2016
hasDirtyAttributes: Ember.computed('currentState.isDirty', function() {
return this.get('currentState.isDirty');
hasDirtyAttributes: Ember.computed('currentState', function() {
return this.get('_internalModel.currentState.isDirty');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pangratz I updated the dependent key in the CP here to currentState, but the hasDirtyAttributes assertion still failed in 1.13 since this.get('currentState') is undefined. I updated the returned value to _internalModel.currentState.isDirty similar to what retrieveFromCurrentState is doing. Will this work?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you test this? Because we have an ember-try scenario for ember.js 1.13 and it looks like the test passes on Travis and AppVeyor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pangratz I switched the return back to this.get('currentState.isDirty'). Note the current failure on the branch. I think this should be returning this.get('_internalModel.currentState.isDirty'). In my previous question I was just wanting to verify with you that it was appropriate to refer to _internalModel.currentState.isDirty. That may have been confusing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, now I see. Sorry for being slow here. Re-reading your comment makes it clear what you meant. Yes, please change it back to this.get('_internalModel.currentState.isDirty'). Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! Thanks!

@tchak
Copy link
Member

tchak commented May 24, 2016

There is a PR #3301 to fix this in a more general way. Is it this old PR still relevant? cc @igorT

@ryanpatrickcook ryanpatrickcook force-pushed the Unload-Record-Does-Not-Remove-Observers branch 2 times, most recently from 056a72c to b38609f Compare May 24, 2016 13:48
@fivetanley
Copy link
Member

I think this is still relevant. @ryanpatrickcook can you rebase onto the latest master, and @pangratz can you take another look?

@ryanpatrickcook ryanpatrickcook force-pushed the Unload-Record-Does-Not-Remove-Observers branch from b38609f to 00e954c Compare September 26, 2016 12:51
@pangratz pangratz merged commit 26f9a57 into emberjs:master Oct 19, 2016
@pangratz
Copy link
Member

Sorry this took so long @ryanpatrickcook. Thanks for your patience and this fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants