项目作者: simonschwartz

项目描述 :
A small wrapper for Asynchronous JavaScript requests that allows you to detect stalled, fetching and finished states.
高级语言: JavaScript
项目地址: git://github.com/simonschwartz/request-state-wrapper.git
创建时间: 2019-03-03T00:48:58Z
项目社区:https://github.com/simonschwartz/request-state-wrapper

开源协议:

下载


Request State Wrapper

A small, less than 1kb gzipped, utility wrapper for Asynchronous JavaScript events.

It allows you to easily handle fetching, stalled and finished states.

Philosophy

The way we handle loading states in front end applications can be incredibly reptetive and imperative.

We also design loading states that aren’t always the optimal user experience for all users. This means users with fast AND slow connections.

Request State Wrapper aims to solve the following problems.

  • Make it easy to detect stalled loading states so we can communicate to users on slower connections that the app is still working / not broken.
  • Make it easy to batch multiple requests together to avoid staggered UI updates.
  • Take the reptetive code required to handle loading states, and compose it into a single, declarative utility function. This helps keep our codebases DRY(Dont Repeat Yourself).

Usage

  1. npm install --save request-state-wrapper
  1. import { createRequest } from 'request-state-wrapper';
  2. const yourAsyncFunction = () =>
  3. fetch('https://api.github.com/repos/nodejs/node').then(response => response.json());
  4. // Create your request with request-state-wrapper
  5. const request = createRequest({
  6. request: yourAsyncFunction,
  7. stalledDelay: 1000,
  8. onStalled: () => { /* handle stalled state */ },
  9. });
  10. // Run it!
  11. request()
  12. .then(response => { /* handle request response */ })
  13. .catch(error => { /* handle request error */ });

Usage recipies

For more detailed implementation recipies see:

API

createRequest takes a single object argument.

  1. const demoRequest = createRequest({
  2. request,
  3. stalledDelay,
  4. onStateChange,
  5. onFetching,
  6. onStalled,
  7. onFinished,
  8. })

Required

  • request - Function that returns a promise or Array of functions that return promises

Optional

  • stalledDelay - Time, in milliseconds, it takes for the request to be considered stalled (defaults to 1000)
  • onStateChange - Handler function that is called whenever the request state changes
  • onFetching - Handler function that is called whenever the request starts fetching
  • onStalled - Handler function that is called whenever the request state becomes stalled
  • onFinished - Handler function that is called whenever the request state finishes

createRequest returns a function that invokes the request. It takes a single argument, an object of options all of which are optional.

  1. demoRequest({
  2. onStateChange,
  3. onFetching,
  4. onStalled,
  5. onFinished,
  6. })
  • onStateChange - Handler function that is called whenever the request state changes
  • onFetching - Handler function that is called whenever the request starts fetching
  • onStalled - Handler function that is called whenever the request state becomes stalled
  • onFinished - Handler function that is called whenever the request state finishes

Important:

  • Any options declared at request time will override options declared at creation time
  • onFetching, onStalled, onFinished take precedence over onStateChange