Skip to content

Ember Data: Return Promise from DS.Model.save()#362

Closed
fivetanley wants to merge 2 commits intoemberjs:masterfrom
fivetanley:deprecate-promise-object-save
Closed

Ember Data: Return Promise from DS.Model.save()#362
fivetanley wants to merge 2 commits intoemberjs:masterfrom
fivetanley:deprecate-promise-object-save

Conversation

@fivetanley
Copy link
Member

@fivetanley fivetanley commented Aug 22, 2018

@fivetanley fivetanley added the T-ember-data RFCs that impact the ember-data library label Aug 22, 2018
@btecu
Copy link

btecu commented Aug 23, 2018

This is great!

@luxzeitlos
Copy link

In my opinion.save() is not really the problem. The primary problem are relations, followed by findAll/findRecord/query/queryRecord. And to solve them we need a way to work seamlessly with computed properties, templates and promises. There are different ideas and solutions like await helpers. I even have my own little helper for computed properties. However I think we need something official.

And I'm not sure if I like the idea to change the result of save at a different time then the result of relationships and findAll/findRecord/query/queryRecord. This just creates two different APIs, but does not really solve the problem. Its easier if save just returns the same as findRecord.

@runspired
Copy link
Contributor

runspired commented Aug 27, 2018

@luxferresum

We should not prevent progress in one area because of a preference for fixing issues other areas.

In my opinion.save() is not really the problem.

Implementation problems with save (in addition to the usage issues mentioned in the RFC)

  • We return a new unique PromiseProxy for every call to save, regardless of whether the save call creates a new request (or even any request at all).
  • These proxies are leaks (untracked, and never destroyed), which cause mysterious app and test failures when the backing record is destroyed or unloaded.
  • Older versions of Ember (pre 3.1) cause these leaked proxies to blow up with errors if we properly destroy the record instance.

The primary problem are relations, followed by findAll/findRecord/query/queryRecord. And to solve them we need a way to work seamlessly with computed properties, templates and promises. There are different ideas and solutions like await helpers. I even have my own little helper for computed properties. However I think we need something official.

Solutions for issues with finders are en-route, but doing it all at once would be far more disruptive.

And I'm not sure if I like the idea to change the result of save at a different time then the result of relationships and findAll/findRecord/query/queryRecord. This just creates two different APIs, but does not really solve the problem. Its easier if save just returns the same as findRecord.

Many other record APIs return promises or the record instead of a PromiseProxy. With the exception of save, we only return a PromiseProxy today when we may not have the record available already. We do have it available during save.

@igorT
Copy link
Member

igorT commented Nov 2, 2018

This looks great to me as well. @runspired we should discuss this at the next meeting if we haven't already

@runspired runspired self-assigned this Apr 10, 2019
@rwjblue
Copy link
Member

rwjblue commented Apr 10, 2019

Chatted about this in todays ember-data meeting, the general consensus is that we'd like to move forward.

@hjdivad
Copy link
Member

hjdivad commented Aug 21, 2019

Discussed during today's meeting; we've already agreed this looks like a good path forward; putting it in FCP.

@pete-the-pete
Copy link

pete-the-pete commented Aug 21, 2019 via email

@HeroicEric
Copy link
Member

HeroicEric commented Aug 29, 2019

I think this might be a good opportunity to do the same refactoring for Model.reload(), which also currently returns a PromiseObject for all of the same reasons mentioned in this RFC.

https://github.com/emberjs/data/blob/5e495394cbf67025be2622f604e67004d79bc21c/packages/store/addon/-private/system/model/model.js#L984-L996

@Gaurav0
Copy link
Contributor

Gaurav0 commented Oct 27, 2019

This has been in FCP for months?

@runspired
Copy link
Contributor

After discussion today we decided that the return value here needs to be a custom thenable, basically a Promise with three additional properties for knowing the state

isPending
isResolved
isRejected

We are otherwise 👍 on merging this

@knownasilya
Copy link
Contributor

Ping

@runspired
Copy link
Contributor

I'll mention at the next meeting that we forgot to merge, we should go ahead and merge :)


## Unresolved questions

* It's quite possible Ember Data users use the isSettled, isRejected, isFulfilled, and isPending properties of the PromiseProxyMixin to display state in their UI. For example, you may have a component that calls record.save(), and uses isPending to show a loading spinner or isRejected to show UI for error states. Should we point them to community alternatives like [ember-promise-helpers](https://github.com/fivetanley/ember-promise-helpers) or [ember-concurrency](http://ember-concurrency.com/docs/introduction/) in the deprecation message for these properties?
Copy link
Contributor

Choose a reason for hiding this comment

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

We could also implement something on top of Promise that exposes these flags.

https://github.com/snewcomer/ember-stateful-promise/blob/main/addon/decorators/stateful-function.js

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess their method could be wrapped with something similar to this or ember-concurrency as well. Almost every save we deal with uses derived state to help the UI along and prevent user initiated bugs.

So I take that back. Returning a simple Promise is better.

Copy link
Contributor

@snewcomer snewcomer left a comment

Choose a reason for hiding this comment

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

Will start working on this one! Will have to figure out how to update to get CI to pass. Or we just dup it to a new RFC.

@snewcomer
Copy link
Contributor

supplanted by #795!

@snewcomer snewcomer closed this Feb 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Final Comment Period T-ember-data RFCs that impact the ember-data library

Projects

None yet

Development

Successfully merging this pull request may close these issues.