hello there! π
I have been working on the new version of ts-belt with support for Async*
modules for a quite long time, and now I feel it's a great time to publish (at least) a release candidate
version. It's also an excellent opportunity to gather feedback from you :) The bad news is, the docs for these modules are missing at the moment (I'm working on it!), but let me describe the essentials of each module:
Installation
yarn add @mobily/ts-belt@next
AsyncData
AsyncData
contains a variant type for representing the different states in which a value can be during an asynchronous data load.
There are four possible states: Init
, Loading
, Reloading
, and Complete
.
type AsyncData<T> = Init | Loading | Reloading<T> | Complete<T>
import { AD } from '@mobily/ts-belt'
Variant constructors:
AD.Init
AD.Loading
AD.Reloading(value)
AD.Complete(value)
AD.makeInit()
AD.makeLoading()
AD.makeReloading(value)
AD.makeComplete(value)
Functions:
AD.isInit
AD.isLoading
AD.isReloading
AD.isComplete
AD.isBusy
AD.isIdle
AD.isEmpty
AD.isNotEmpty
AD.toBusy
AD.toIdle
AD.getValue
AD.getWithDefault
AD.getReloading
AD.getComplete
AD.map
AD.mapWithDefault
AD.flatMap
AD.tapInit
AD.tapLoading
AD.tapReloading
AD.tapComplete
AD.tapEmpty
AD.tapNotEmpty
AD.all
AD.fold
Example: https://codesandbox.io/s/cool-star-6m87kk?file=/src/App.tsx
AsyncDataResult
AsyncDataResult
is basically an alias of AsyncData<Result<Ok, Error>>
. This variant type can be used to represent the different states in which a data value can exist while being loaded asynchronously, with the possibility of either success or failure.
type AsyncDataResult<A, B> = AsyncData<Result<A, B>>
import { ADR } from '@mobily/ts-belt'
Variant constructors:
ADR.Init
ADR.Loading
ADR.ReloadingOk(value)
ADR.ReloadingError(error)
ADR.CompleteOk(value)
ADR.CompleteError(error)
ADR.makeInit()
ADR.makeLoading()
ADR.makeReloadingOk(value)
ADR.makeReloadinError(error)
ADR.makeCompleteOk(value)
ADR.makeCompleteError(error)
Functions:
ADR.isOk
ADR.isError
ADR.isReloadingOk
ADR.isReloadingError
ADR.isCompleteOk
ADR.isCompleteError
ADR.getOk
ADR.getReloadingOk
ADR.getCompleteOk
ADR.getError
ADR.getReloadingError
ADR.getCompleteError
ADR.map
ADR.mapError
ADR.flatMap
ADR.tap
ADR.fold
ADR.foldOk
ADR.toAsyncData
Example: https://codesandbox.io/s/brave-cloud-ov30h7?file=/src/App.tsx
AsyncOption
Same as Option
but for handling asynchronous operations.
type AsyncOption<T> = Promise<Option<T>>
import { AO } from '@mobily/ts-belt'
Variant constructors:
AO.make(promise)
AO.resolve(value)
AO.reject()
Functions:
AO.filter
AO.map
AO.flatMap
AO.fold
AO.mapWithDefault
AO.match
AO.toNullable
AO.toUndefined
AO.toResult
AO.getWithDefault
AO.isNone
AO.isSome
AO.tap
AO.contains
AO.flatten
AsyncResult
Same as Result
but for handling asynchronous operations.
type AsyncResult<A, B> = Promise<Result<A, B>>
import { AR } from '@mobily/ts-belt'
Variant constructors:
AR.make(promise)
AR.resolve(value)
AR.reject(error)
Functions:
AR.flatMap
AR.fold
AR.map
AR.mapWithDefault
AR.getWithDefault
AR.filter
AR.match
AR.toNullable
AR.toOption
AR.toUndefined
AR.isOk
AR.isError
AR.tap
AR.tapError
AR.handleError
AR.mapError
AR.catchError
AR.recover
AR.flip
AR.flatten
Minor changes
- β¨ added
A.sample
(gets a random element from provided array)
- β¨ added
O.all
(transforms an array of Option(s)
into a single Option
data type)
- β¨ added
R.all
(transforms an array of Result(s)
into a single Result
data type)
- β¨ added
R.filter
(returns Ok(value)
if result
is Ok(value)
and the result of predicateFn
is truthy, otherwise, returns Error
)
- π fixed up the
groupBy
signature
Breaking changes
ts-belt@v4 does not support Flow
, due to a lack of proper features in flowgen
, sorry about that!
Feel free to post your thoughts, any kind of feedback would be greatly appreciated! πͺ