/utils/xhr
Export the xhr method to mapp.utils{}.
- Source
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.
| Name | Type | Description |
|---|---|---|
xhr | object | XMLHttpRequest. |
params | object | Request parameters. |
reject | function | The request promise will be rejected if debounced. |
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
params.debounce | boolean | <optional> | Request should be debounced. | |
params.key | string | <optional> | 'global' | Key for request in debounceMap. |
params.delay | boolean | <optional> | 300 | Delay in ms for debounce timeout. |
- Source
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.
| Name | Type | Description |
|---|---|---|
params | Object | The object containing the parameters. |
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
params.url | string | The request URL. | ||
params.method | string | <optional> | GET | The request method. |
params.responseType | string | <optional> | json | The XHR responseType. |
params.requestHeader | Object | <optional> | {'Content-Type': 'application/json'} | The XHR requestHeader. |
params.body | string | <optional> | A stringified request body for a 'POST' request. | |
params.resolveTarget | boolean | <optional> | Whether the target instead of target.response should be resolved. | |
params.cache | boolean | <optional> | Whether the response should be cached in a Map(). | |
params.debounce | boolean | | <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}. |
- Source
A promise that resolves with the XHR.
- Type:
- Promise