0.3.2 • Published 5 years ago

stately-async v0.3.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

stately-async

[api] [github]
npm

This module contains types and functions for representing and managing the state of asynchronous operations. It is the underlying data API behind the <Async> component in stately-react.

All asynchronous tasks have a similar lifecycle: they are called, data is emitted once or multiple times, and either the task is completed or an error is encountered. Typically, the code to manage this lifecycle is duplicated for every asynchronous action, whether it is done with epics, sagas, or component lifecycle.

This module provides functions and types that allow a consumer to use self-managing stateful "lifecycles" for arbitrary asynchronous tasks. A lifecycle can be created for any asynchronous function, regardless of its parameter arity, output type, or underlying implementation.

It is implemented following the action/reducer pattern, and is intended to work with Redux or any other state managment system that uses actions and reducers (such as <Controllable> components.)

Usage

Most asynchronous operations can be expressed declaratively using the <Async> component, which is preferable to using this library directly. However, unusual or custom use cases may arise where it is ideal to create and use an AsyncLifecycle directly:

import store from './myStore'
import getTweets from './getTweets'
import { asyncLifecycle } from 'stately-async'

class TweetsComponent extends React.Component {
  constructor(props) {
    super(props)
    this.tweetsLifecycle = asyncLifecycle(getTweets)
  }
  
  render() {
    const { state } = this.props
    return state.data ? <TweetsView tweets={state.data} />
      : state.error ? <ErrorMessage error={state.error}>
      : state.status === 'active' ? <LoadingSpinner />
      : null
  }

  componentDidMount() {
    store.dispatch(this.tweetsLifecycle.call('@myTwitterHandle'))
  }

  componentDidUnmount() {
    store.dispatch(this.tweetsLifecylce.destroy())
  }
}
0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.12

5 years ago

0.2.11

6 years ago

0.2.10

6 years ago

0.2.9

6 years ago

0.2.8

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.1

6 years ago