Open
Description
@async-library/core
will contain all the core functionality that Async Library provides. It will generally not be used directly, but be a dependency of @async-library/react-async-hook
for example. It should have feature parity with react-async
and react-async-hook
(outside of React specifics).
This description is updated as the discussion unfolds.
Features
- Standardized API (both in and out)
- Full state management for a single instance in local state
- No global store (otherwise you might as well use Redux Saga)
- Cancellation (with AbortController)
- DevTools integration (e.g. allow replaying a promiseFn)
To discuss:
- Should we use generators instead of promises?
- Should we use an internal state machine?
- Should we allow users to create their own async state machine?
Core API
States:
initial
nothing happened yet (orreinitialize
was invoked)pending
promise is loading or pausedfulfilled
promise was fulfilled with datarejected
promise was rejected with error
Config options:
fn
async function that will be invoked onrun
runOnInit
flag to enable running on init (mount) [do we really need this?]initialValue
initialize value to a predefined value or error [should this be separate values for data and error?]
Callback options:
onInit
when first initialized (i.e. mount)onData
when promise has fulfilled with dataonError
when promise has rejected with erroronAbort
when promise has abortedonDestroy
when destroying the instance (i.e. unmount)
Actions:
run
invokefn
abort
cancel a pending promise and invokeAbortController::abort
reinitialize
abort and reset state toinitial
Metadata:
runCount
number of timesrun
was invoked (automatically or manually)startedAt
when the lastrun
was invokedfinishedAt
when the last promise was fulfilled or rejected
This API deviates quite a bit from React Async. Notably:
promiseFn
anddeferFn
are combined intofn
promise
is dropped as an option. You can just pass() => promise
instead.
Metadata
Metadata
Assignees
Labels
No labels