@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 (or reinitialize was invoked)
pending promise is loading or paused
fulfilled promise was fulfilled with data
rejected promise was rejected with error
Config options:
fn async function that will be invoked on run
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 data
onError when promise has rejected with error
onAbort when promise has aborted
onDestroy when destroying the instance (i.e. unmount)
Actions:
run invoke fn
abort cancel a pending promise and invoke AbortController::abort
reinitialize abort and reset state to initial
Metadata:
runCount number of times run was invoked (automatically or manually)
startedAt when the last run was invoked
finishedAt when the last promise was fulfilled or rejected
This API deviates quite a bit from React Async. Notably:
promiseFn and deferFn are combined into fn
promise is dropped as an option. You can just pass () => promise instead.
@async-library/corewill 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-hookfor example. It should have feature parity withreact-asyncandreact-async-hook(outside of React specifics).Features
To discuss:
Core API
States:
initialnothing happened yet (orreinitializewas invoked)pendingpromise is loading or pausedfulfilledpromise was fulfilled with datarejectedpromise was rejected with errorConfig options:
fnasync function that will be invoked onrunrunOnInitflag to enable running on init (mount) [do we really need this?]initialValueinitialize value to a predefined value or error [should this be separate values for data and error?]Callback options:
onInitwhen first initialized (i.e. mount)onDatawhen promise has fulfilled with dataonErrorwhen promise has rejected with erroronAbortwhen promise has abortedonDestroywhen destroying the instance (i.e. unmount)Actions:
runinvokefnabortcancel a pending promise and invokeAbortController::abortreinitializeabort and reset state toinitialMetadata:
runCountnumber of timesrunwas invoked (automatically or manually)startedAtwhen the lastrunwas invokedfinishedAtwhen the last promise was fulfilled or rejectedThis API deviates quite a bit from React Async. Notably:
promiseFnanddeferFnare combined intofnpromiseis dropped as an option. You can just pass() => promiseinstead.