项目作者: ArusSoft

项目描述 :
Async property
高级语言: TypeScript
项目地址: git://github.com/ArusSoft/async-property.git
创建时间: 2019-08-28T10:44:15Z
项目社区:https://github.com/ArusSoft/async-property

开源协议:MIT License

下载


async-property

Simple typesafe helper to store async operations state and data.

Example

To work with async processes in react and redux we use async types and actions, so why not to use async properties.

  1. import React, { useState, useEffect } from 'react'
  2. import {
  3. AsyncProperty,
  4. emptyProperty, requestProperty,
  5. setSuccessProperty, setFailureProperty,
  6. isEmpty, isRequest, isSuccess, isFailure
  7. } from 'async-property';
  8. const requestStringProperty = async () => new Promise<string>((resolve) => resolve('async result'))
  9. const Example: React.FC = () => {
  10. const [stringProperty, setStringProperty] = useState<AsyncProperty<string>>(emptyProperty)
  11. useEffect(() => {
  12. setStringProperty(requestProperty)
  13. const fetchData = async () => {
  14. try {
  15. const stringResult = await requestStringProperty()
  16. setStringProperty(setSuccessProperty(stringResult))
  17. } catch (error) {
  18. setStringProperty(setFailureProperty(error))
  19. }
  20. }
  21. fetchData()
  22. }, [])
  23. if (isEmpty(stringProperty) || isRequest(stringProperty)) {
  24. return (
  25. <p>Processing</p>
  26. )
  27. }
  28. if (isFailure(stringProperty)) {
  29. return (
  30. <p>Failure: {stringProperty.error}</p>
  31. )
  32. }
  33. if (isSuccess(stringProperty)) {
  34. return (
  35. <p>Success: {stringProperty.value}</p>
  36. )
  37. }
  38. return <p>Initialize</p>
  39. }

Documentation

Types

  1. type AsyncProperty<T, F = Error> = EmptyProperty | RequestProperty | SuccessProperty<T> | FailureProperty<F> | CancelProperty;
  2. type EmptyProperty = {
  3. state: PropertyState.EMPTY
  4. }
  5. type RequestProperty = {
  6. state: PropertyState.REQUEST
  7. }
  8. type SuccessProperty<T> = {
  9. state: PropertyState.SUCCESS,
  10. value: T,
  11. }
  12. type FailureProperty<T = Error> = {
  13. state: PropertyState.FAILURE,
  14. error: T,
  15. }
  16. type CancelProperty = {
  17. state: PropertyState.CANCEL,
  18. reason?: string,
  19. }

Enum

  1. const enum PropertyState {
  2. EMPTY = 'Empty',
  3. REQUEST = 'Request',
  4. SUCCESS = 'Success',
  5. FAILURE = 'Failure',
  6. CANCEL = 'Cancel',
  7. }

Constants

  1. const emptyProperty: EmptyProperty = {
  2. state: PropertyState.EMPTY
  3. }
  4. const requestProperty: RequestProperty = {
  5. state: PropertyState.REQUEST
  6. }
  7. const cancelProperty: CancelProperty = {
  8. state: PropertyState.CANCEL
  9. }

Methods

Setters

  1. function setSuccessProperty<T>(value: T): SuccessProperty<T>
  2. function setFailureProperty<T>(error: T): FailureProperty<T>
  3. function setCancelProperty(reason: string): CancelProperty

Type check

  1. function isAsyncProperty<T, F>(property: any): property is AsyncProperty<T, F>
  2. function isEmpty(property: AsyncProperty<any>): property is EmptyProperty
  3. function isRequest(property: AsyncProperty<any>): property is RequestProperty
  4. function isSuccess<T>(property: AsyncProperty<T>): property is SuccessProperty<T>
  5. function isFailure<T>(property: AsyncProperty<any, T>): property is FailureProperty<T>
  6. function isCancel(property: AsyncProperty<any>): property is CancelProperty

Contribute

Build

  1. npm run build

Test

  1. npm test