Last active
August 3, 2017 19:20
-
-
Save maxidr/fadfea7503397fb545451d2a5b536ffd to your computer and use it in GitHub Desktop.
Make xhr request that will be aborted if you call it again. Using mithril request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const merge = require('ramda/src/merge') | |
const m = require('mithril') | |
/* | |
@param {string} key - The unique key of the request | |
@param {Object} params - The same params used in m.request of mithril | |
@returns Promise | |
@link https://mithril.js.org/request.html | |
Example of use: | |
function searchUser(email){ | |
return uniqueReq('searchUser', { | |
method: 'GET', | |
url: '/users?email=' + email | |
}) | |
} | |
searchUser('[email protected]').then(user => console.log(user)) | |
Every time that you call the function searchUser, the previous request will be aborted. | |
*/ | |
function uniqueReq(key, params){ | |
if( ! uniqueReq.xhrs ){ uniqueReq.xhrs = {} } | |
if( uniqueReq.xhrs[key] ){ uniqueReq.xhrs[key].abort() } | |
return m.request( | |
merge( | |
params, | |
{ config: xhr => uniqueReq.xhrs[key] = xhr } | |
) | |
) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment