/utils/xhr

Export the xhr method to mapp.utils{}.

Methods

(inner) debounce(xhr, params, reject) → {boolean}

A request will not be debounced if the params.debounce property is falsy [undefined].

The debounce property will be assigned as delay [ms] value if numeric.

The debounce property will be assigned as key for the debounce.

The delay will be provided to timeout sending the xhr request.

The timeout will be stored in the debounceMap with the associated params.key property. The timeout will be cleared should a request with the same key property be debounced before the timeout function is executed.

Parameters:
NameTypeDescription
xhrobject

XMLHttpRequest.

paramsobject

Request parameters.

rejectfunction

The request promise will be rejected if debounced.

Properties
NameTypeAttributesDefaultDescription
params.debounceboolean<optional>

Request should be debounced.

params.keystring<optional>
'global'

Key for request in debounceMap.

params.delayboolean<optional>
300

Delay in ms for debounce timeout.

Returns:

true if request will be debounced.

Type: 
boolean

(inner) xhr(params) → {Promise}

The params object/string for the xhr utility method is required.

The params are assumed to the request URL if provided as a string argument.

The request params and response are stored in a Map() if the cache flag is set in the params object argument.

If the debounce flag is set, requests will be delayed until after the specified time has elapsed since the last call in the same group. Unlike throttle which aborts previous requests, debounce delays execution of the latest request. The method is assumed to be 'POST' if a params.body is provided.

Parameters:
NameTypeDescription
paramsObject

The object containing the parameters.

Properties
NameTypeAttributesDefaultDescription
params.urlstring

The request URL.

params.methodstring<optional>
GET

The request method.

params.responseTypestring<optional>
json

The XHR responseType.

params.requestHeaderObject<optional>
{'Content-Type': 'application/json'}

The XHR requestHeader.

params.bodystring<optional>

A stringified request body for a 'POST' request.

params.resolveTargetboolean<optional>

Whether the target instead of target.response should be resolved.

params.cacheboolean<optional>

Whether the response should be cached in a Map().

params.debounceboolean | number | object<optional>

Whether the request should be debounced. Can be true (300ms default), a number (milliseconds), or an object (group name with 300ms default). Format: "{key: "name", delay: 300}.

Returns:

A promise that resolves with the XHR.

Type: 
Promise