RestClient icon indicating copy to clipboard operation
RestClient copied to clipboard

Promise abort enhancement

Open cetonek opened this issue 5 years ago • 4 comments

Iv been using the library for last few weeks and it has worked for me very well, however there is one thing that I find abit inconvenient - aborting requests.

I set up a repository class for my rest api:

public class LadderRepository : MonoBehaviour {

    private static string ladderFinalUrl = "ladder";

    private readonly IList<RequestHelper> requests = new List<RequestHelper>();


    private void Awake() {
        InitUrls();
    }

    private void InitUrls() {
     .....
    }

    public IPromise<LadderResponse> FetchLadder() {
        var requestHelper = ComposeRequest(ladderFinalUrl);
        return RestClient.Get<LadderResponse>(requestHelper);
    }

}

Basically I expose IPromises for other game objects that want to communicate with the api, now my problem is aborting individual IPromises.

Ideally Id like to just call a method Abort on given IPromise to cancel given api call. I am used to abort things like this from rxjava - disposing an observable whenever I feel like.

Maybe I am missing something but right now Id have to expose given RequestHelpers to achieve what I want, which doesnt seem to be so clean. What do you think about it?

cetonek avatar Dec 05 '19 11:12 cetonek

You can have a loop to iterate your requests list in order to call the Abort method of every request of the RequestHelper object, what you think?

jdnichollsc avatar Dec 06 '19 14:12 jdnichollsc

Yes thats true, actually that is what I am doing at the moment. But still that has its own limitations - maybe I want to abort only some of the requests or only one particular request.

That could be also solved, yes - for example by having a Dictionary<int, RequestHelper> - where key would be promise id and value its respective request helper - then Id abort individual requests with given id.

Still not as convenient as simply calling IPromise.Abort() without anything else.

cetonek avatar Dec 06 '19 16:12 cetonek

ohh it would be awesome, any pull request is welcome! 💯

jdnichollsc avatar Jan 13 '20 22:01 jdnichollsc

If #226 gets merged, ProtoPromise comes with CancelationToken that can have aborts registered to it.

timcassell avatar Sep 25 '22 17:09 timcassell