Skip to content

@async-library/core #2

Open
Open
@ghengeveld

Description

@ghengeveld

@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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions