Giter VIP home page Giter VIP logo

react-use-task's Introduction

Deprecated: you probably don't want this.

Simply put: Components are not the right place to define these tasks. Currently working towards bringing ember-concurrency-like modifiers and derived state to redux-saga. This was a fun exercise.

react-use-task

☎️ React hooks for writing async tasks/workers (with cancellation and concurrency)

NPM Build Status Bundle Size

Why ?

This library was inspired by ember-concurrency and uses posterus to run generators as cancellable async-tasks.

Redux-Saga is a fantastic tool for managing these types of tasks. I use it with redux-saga-thunk to get "derived state". It's extremely powerful. But, let's be honest - sagas are a lot of boilerplate. Sometimes, a less-powerful tool can be useful for implementing common patterns.

This library is not intended to replace redux-saga. It is component-lifecycle centric, and hopes to provide some ergonomic tools for consise async tasks:

  • Derived state: Never manage boolean flags. Declaratively handle loading, error, and success states.
  • Task Modes: By default, tasks can be performed multiple-times in parallel. "Modes" can be selected to modify task behavior. Using a telephone analogy:
    • Restartable: "Hang up on current caller when a new call comes in"
    • Drop: "If already on the phone, ignore incoming calls"
    • Enqueue: "Place callers on hold and handle them in order"
    • Keep Latest: ?analogy? Like drop, but the last ignored gets handled when free
  • Concurrency: Easily specify concurrency limits
  • Cancellation: Cancel individual task instances, or all running instances at once. Component unmounted? Everything cleans up automatically.

Install

$ npm install --save react-use-task

Usage

Basic Tasks

Example use-case: Save button

import { useTask } from 'react-use-task';

const BasicTask = () => {
  const [{ isRunning, last }, perform] = useTask(function*() {
    // Hit some remote api
    yield delay(1000);
  });

  return (
    <div>
      {/* When task isRunning, button disabled & inner text changed */}
      <button disabled={isRunning} onClick={perform}>
        {isRunning ? 'Saving...' : 'Perform Task'}
      </button>

      {last && last.isSuccessful && <div>Saved!</div>}
    </div>
  );
};

const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));

One-at-max Tasks

Example use-case: ignore button-spam (drop mode) - TODO

Example use-case: typeahead seach (debounced via restartable) - TODO


Workers (background tasks)

Example use-case: Stock ticker (polling remote API)

import { useWorker } from "react-use-task";

const StockTicker = () => {
  const [price, setPrice] = useState();
  useWorker(function*() {
    // Infinite loop worker
    while (true) {
      yield delay(1000);
      setPrice(Math.random() * 50 + 50);
    }
  });

  if (typeof price === "undefined") {
    return <div>Loading...</div>;
  }
  return <div>Latest stock price: ${price.toFixed(2)}</div>;
};

// By mounting / unmounting the <StockTicker>, the worker loop is
//  automatically started & cancelled with the component
const App = () => {
  const [showTicker, setShow] = useState(false);
  return (
    <div>
      <button onClick={() => setShow(!showTicker)}>Toggle stock ticker</button>
      {showTicker && <StockTicker />}
    </div>
  );
};

Plans

  • Non-immediate running of task (e.g. on button press)
  • .drop, .restartable, .maxConcurrency task modifiers (like ember-concurrency)
  • Exposing manual cancellation
  • Historical state - last, lastSuccessful (like e-c)
  • Code sandbox examples
  • Finalize TaskInstance API - go all-in on futures?
  • Improve error handling
  • More docs / motivaion / examples
  • Fancy visualizations (like ember-concurrency)
  • Possibly split out some Posterus.Future utilities

react-use-task's People

Contributors

davidgovea avatar

Stargazers

 avatar  avatar

Forkers

tmbtech rauhryan

react-use-task's Issues

Join forces with `use-task`

  • I'm submitting a ...
    [ ] bug report
    [ ] feature request
    [ ] question about the decisions made in the repository
    [ ] question about how to use this project
    [x] none of the above

Hello!

I created use-task which, I think, is intending to solve the same use-case as this library. Generators, based on ember-concurrency, React hooks, etc.

Unfortunately a lot has some up since I started the project and I can't give it the kind of attention that I believe the idea deserves.

Would you be interested at all on collaborating on a single library? If anything, I would be pretty happy to give the use-task NPM package name to this project instead, and maybe we can talk a bit about some of the stuff I did supporting AbortController for cancellation.

If you're not interested, no big deal, but if you are I'd love to chat!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.