简体   繁体   中英

Meteor - Cancelling a Server Method from the Client

I'm performing a database count through a server method. Users can select how they want the count to be performed and then invoke the method.

My problem is that the count can take some time and a user might change their mind while the method is running and request a different count. Is there any way for me to cancel the invoked method and run a new count?

I've thought this.unblock() might work; it will allow a new method to be run, but it won't cancel the old method. I've also considered pre-counting and then just using a lookup, but there are too many selector combinations.

Here's my code, it's fairly simple:

//Server
Meteor.methods({
    getFilterCount: function(oFilterSelector) {
        return clMaBldgs.find(oFilterSelector, {}).count();
    }
});

//Client
Meteor.call('getFilterCount', oFilterSelector, function (error, result) {
    //do some stuff
});

Cancelling an already-made method call to server from client is not possible.

But if your method are called multiple times in a short interval, and you only care about the last call's result. You could use debounce to delay execution for some times. This will help reduce unnecessary calls to server.

More of a work around, but if you had a reactive variable (could be an entry in the DB) that is a status of the method, and in the method itself, you check for that flag every chance you get for the status.

{
    _id: "some id for the method", running: true
}

Pseudocode for method

Meteor.methods({
    method: (_id) => {
        status = getStatusForId(_id);
        while(status) {
            status = getStatusForId(_id);
        }
    }
});

Then to stop it, you just update that flag to be false and the method will stop as soon as it can.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM