jaz303 / promise-debounce

Debounce any promise-returning function so that at any instant only one call is pending
MIT License
5 stars 1 forks source link

debounce only if function arguments are the same #3

Open thom-nic opened 8 years ago

thom-nic commented 8 years ago

I've got something similar to the README example, an XHR request that I want to debounce. However different components may want to use the same request with different query parameters, in which case both request should be allowed.

example:

const getUsers = debounce( params => axios.get('/users', params) )

getUsers();
getUsers({site_id: 1}); // not debounced
getUsers({organization_id: 2}); // not debounced
getUsers({site_id: 1}); // debounced

I realize this complicates the design a bit but think it's a useful feature.

jaz303 commented 8 years ago

How about this for an interface:

const foo = debounce(fn, {hash: true})

Wherein {hash:true} indicates that the arguments to fn should be hashed to a string and a single promise returned for each unique hash value. Alternatively, you could provide your own hashing function:

const foo = debounce(fn, {hash: params => params.op + "=" + params.value})

If the hash option is omitted, default functionality is retained.