mattkrick / cashay

:moneybag: Relay for the rest of us :moneybag:
MIT License
453 stars 28 forks source link

need to wrap query/mutation code #151

Closed dustinfarris closed 7 years ago

dustinfarris commented 7 years ago

I am hitting a scenario where the async nature of Cashay is causing test errors.

This is because Ember implements a run-loop to manage tasks and optimize scheduling where possible.

While run-loops are auto-created as needed in production, the run-loop is disabled in testing—to help discover patches of code not covered so you can fix them.

A common sore spot is with async third-party libs that do not cater to this Ember-specific detail—such as Cashay.

tl;dr my assertions run before cashay.query is done handling results from a server query.

So this is a selfish (Ember-specific) request: I need a way to wrap the code that happens after await transport.handleQuery here and here. If these sections of code could be moved to separate callback functions that I can import, wrap, and give back to cashay.create my problems should go away.

For me this would look something like:

import Ember from 'ember';

import { cashay, handleServerQueryResponse, handleServerMutationResponse } from 'cashay';

const wrappedHandleServerQueryResponse = (cashay, serverResponse) => {
  Ember.run(() => {
    handleServerQueryResponse(cashay, serverResponse);
  });
});

const wrappedHandleServerMutationResponse = (cashay, serverResponse) => {
  Ember.run(() => {
    handleServerMutationResponse(cashay, serverResponse);
  });
});

cashay.create({
  store,
  httpTransport, 
  handleServerQueryResponse: wrappedHandleServerQueryResponse,
  handleServerMutationResponse: wrappedHandleServerMutationResponse
});

I don't think this would be a breaking change if the arg to .create is just tacked on the end with the proper default.

I am willing to do the work here, but @mattkrick is this ok with you?

dustinfarris commented 7 years ago

so my test was failing because i was failing to clear cashay between tests.

looks like the Ember.run i have in the redux subscribe callback is good enough.

Possibly will need to hook into callbacks in the future, but that day is not today.