@colisweb/rescript-toolkit 5.32.0 → 5.33.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/request/Request.res +36 -0
package/package.json
CHANGED
package/src/request/Request.res
CHANGED
|
@@ -231,6 +231,42 @@ let useManualRequest = (
|
|
|
231
231
|
(state, trigger)
|
|
232
232
|
}
|
|
233
233
|
|
|
234
|
+
let useDebouncedManuelRequest = (
|
|
235
|
+
type argument response err,
|
|
236
|
+
~onSuccess: option<response => unit>=?,
|
|
237
|
+
~onError: option<error<err> => unit>=?,
|
|
238
|
+
~debounceDelay: int=300,
|
|
239
|
+
config: module(Config with
|
|
240
|
+
type argument = argument
|
|
241
|
+
and type response = response
|
|
242
|
+
and type error = err
|
|
243
|
+
),
|
|
244
|
+
argument: 'value => argument,
|
|
245
|
+
initialValue: 'value,
|
|
246
|
+
) => {
|
|
247
|
+
let (value, setValue) = React.useState(() => initialValue)
|
|
248
|
+
|
|
249
|
+
let debouncedFunction = {
|
|
250
|
+
let update = (newValue: 'value) => {
|
|
251
|
+
if newValue !== initialValue {
|
|
252
|
+
setValue(_ => newValue)
|
|
253
|
+
fetchAPI(config, argument(newValue))
|
|
254
|
+
->Promise.tapOk(res => {
|
|
255
|
+
setValue(_ => newValue)
|
|
256
|
+
onSuccess->Option.forEach(fn => fn(res))
|
|
257
|
+
})
|
|
258
|
+
->Promise.tapError(e => {
|
|
259
|
+
setValue(_ => initialValue)
|
|
260
|
+
onError->Option.forEach(fn => fn(e))
|
|
261
|
+
})
|
|
262
|
+
->ignore
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
Lodash.debounce(update, debounceDelay)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
(value, debouncedFunction)
|
|
269
|
+
}
|
|
234
270
|
external exnToError: Js.Exn.t => error<'a> = "%identity"
|
|
235
271
|
|
|
236
272
|
let decodeResponseError = (responseError, decoder) => {
|