All Projects → slorber → Awesome Debounce Promise

slorber / Awesome Debounce Promise

Debounce your API calls easily and stay in promised land.

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Awesome Debounce Promise

synapse
Non-intrusive C++ signal programming library
Stars: ✭ 48 (-83.45%)
Mutual labels:  callback
Flask-Shell2HTTP
Execute shell commands via HTTP server (via flask's endpoints).
Stars: ✭ 93 (-67.93%)
Mutual labels:  callback
call thunk
Take the member function of the C++ class as the callback function of the C function
Stars: ✭ 19 (-93.45%)
Mutual labels:  callback
async-chainable
An extension to Async adding better handling of mixed Series / Parallel tasks via object chaining
Stars: ✭ 25 (-91.38%)
Mutual labels:  callback
movie-trailer
🎥 Fetch movie trailers: "Crash" ➔ http://path/to/trailer
Stars: ✭ 19 (-93.45%)
Mutual labels:  callback
simplevk
SimpleVK это PHP библиотека для быстрой разработки ботов для VK | vk api php class bot sdk library client framework longpoll callback streaming auth
Stars: ✭ 84 (-71.03%)
Mutual labels:  callback
image-cache
NodeJS Image cache with Base64 format
Stars: ✭ 18 (-93.79%)
Mutual labels:  callback
Watchman
A simple message queue callback center
Stars: ✭ 271 (-6.55%)
Mutual labels:  callback
epump
ePump是一个基于I/O事件通知、非阻塞通信、多路复用、多线程等机制开发的事件驱动模型的 C 语言应用开发框架,利用该框架可以很容易地开发出高性能、大并发连接的服务器程序。
Stars: ✭ 26 (-91.03%)
Mutual labels:  callback
run exclusive
⚡🔒 Wait queue for function execution 🔒 ⚡
Stars: ✭ 22 (-92.41%)
Mutual labels:  callback
ant-arduino
An implementation of a ANT driver for Arduino, Mbed and ESP-IDF
Stars: ✭ 69 (-76.21%)
Mutual labels:  callback
datamosh
✨💾 Edit images via buffers. 💯✨
Stars: ✭ 23 (-92.07%)
Mutual labels:  callback
react-tabllist
React-based customizable style table or list components that support event and callback functions.
Stars: ✭ 20 (-93.1%)
Mutual labels:  callback
proposal-function-helpers
A withdrawn proposal for standardizing some useful, popular helper functions into JavaScript’s Function object.
Stars: ✭ 41 (-85.86%)
Mutual labels:  callback
KASocialLogins
This is Social login library in which you can login through Facebook , LinkedIn and Google
Stars: ✭ 15 (-94.83%)
Mutual labels:  callback
SwiftObserver
Elegant Reactive Primitives for Clean Swift Architecture #NoRx
Stars: ✭ 14 (-95.17%)
Mutual labels:  callback
cpsfy
🚀 Tiny goodies for Continuation-Passing-Style functions, fully tested
Stars: ✭ 58 (-80%)
Mutual labels:  callback
Mongoose Paginate V2
A cursor based custom pagination library for Mongoose with customizable labels.
Stars: ✭ 283 (-2.41%)
Mutual labels:  callback
Inlineactivityresult
Receive the activity result directly after the startActivityForResult with InlineActivityResult
Stars: ✭ 264 (-8.97%)
Mutual labels:  callback
eBay-node-client
Ebay NodeJS Wrapper
Stars: ✭ 50 (-82.76%)
Mutual labels:  callback

Awesome Debounce Promise

NPM Build Status

Debounce your async calls with React in mind.

  • No callback hell of lodash/underscore
  • Handle concurrent requests nicely (only use last request's response)
  • Typescript support (native and well typed)
  • React in mind, but can be used in other contexts (no dependency)

Read also this famous SO question about debouncing with React.

Install

yarn add awesome-debounce-promise

npm install awesome-debounce-promise --save

import AwesomeDebouncePromise from 'awesome-debounce-promise';

const asyncFunction = () => fetch('/api');

const asyncFunctionDebounced = AwesomeDebouncePromise(
  asyncFunction,
  500,
  options,
);

asyncFunctionDebounced().then(console.log); // no request fired, promise will never resolve
asyncFunctionDebounced().then(console.log); // no request fired, promise will never resolve
asyncFunctionDebounced().then(console.log); // request fired, promise will resolve with response

For Typescript, you need the compiler option "esModuleInterop": true

Usecases

Debouncing a search input (with React class)

const searchAPI = text => fetch('/search?text=' + encodeURIComponent(text));

const searchAPIDebounced = AwesomeDebouncePromise(searchAPI, 500);

class SearchInputAndResults extends React.Component {
  state = {
    text: '',
    results: null,
  };

  handleTextChange = async text => {
    this.setState({ text, results: null });
    const result = await searchAPIDebounced(text);
    this.setState({ result });
  };

  componentWillUnmount() {
    this.setState = () => {};
  }
}

When calling debouncedSearchAPI:

  • it will debounce the api calls. The API will only be called when user stops typing
  • each call will return a promise
  • only the promise returned by the last call will resolve, which will prevent the concurrency issues
  • there will be at most a single this.setState({ result }); call per api call

Debouncing a search input (with React hooks)

I recommend solving this problem with react-async-hook, which plays well with awesome-debounce-promise. You'll find more informations on react-async-hook readme and runnable examples.

const useSearchStarwarsHero = () => {
  // Handle the input text state
  const [inputText, setInputText] = useState('');

  // Debounce the original search async function
  const debouncedSearchStarwarsHero = useConstant(() =>
    AwesomeDebouncePromise(searchStarwarsHero, 300)
  );

  const search = useAsync(debouncedSearchStarwarsHero,[inputText]);

  // Return everything needed for the hook consumer
  return {
    inputText,
    setInputText,
    search,
  };
};

Debouncing the background saving of some form inputs

const saveFieldValue = (fieldId, fieldValue) =>
  fetch('/saveField', {
    method: 'PUT',
    body: JSON.stringify({ fieldId, fieldValue }),
  });

const saveFieldValueDebounced = AwesomeDebouncePromise(
  saveFieldValue,
  500,
  // Use a key to create distinct debouncing functions per field
  { key: (fieldId, text) => fieldId },
);

class SearchInputAndResults extends React.Component {
  state = {
    value1: '',
    value2: '',
  };

  onFieldTextChange = async (fieldId, fieldValue) => {
    this.setState({ [fieldId]: fieldValue });
    await saveFieldValueDebounced(fieldId, fieldValue);
  };

  render() {
    const { value1, value2 } = this.state;
    return (
      <form>
        <input
          value={value1}
          onChange={e => onFieldTextChange(1, e.target.value)}
        />
        <input
          value={value2}
          onChange={e => onFieldTextChange(2, e.target.value)}
        />
      </form>
    );
  }
}

Thanks to the key feature, the 2 fields will be debounced independently from each others. In practice, one debounced function is created for each key.

Options

const DefaultOptions = {
  // One distinct debounced function is created per key and added to an internal cache
  // By default, the key is null, which means that all the calls
  // will share the same debounced function
  key: (...args) => null,

  // By default, a debounced function will only resolve
  // the last promise it returned
  // Former calls will stay unresolved, so that you don't have
  // to handle concurrency issues in your code
  // Setting this to false means all returned promises will resolve to the last result
  onlyResolvesLast: true,
};

Other debouncing options are available and provided by an external low-level library: debounce-promise

FAQ

How can I cancel the debouncing?

You can easily add promise cancellation support to this lib with awesome-imperative-promise, lib that is already used internally.

Why is my debouncing function always firing and is not debounced?

The debouncing function returned by the lib is stateful. If you want deboucing to work fine, make sure to avoid recreating this function everytime. This is the same behavior as regular callback-based debouncing functions.

Instead of this:

handleTextChange = async text => {
  const searchAPI = text => fetch('/search?text=' + encodeURIComponent(text));
  const searchAPIDebounced = AwesomeDebouncePromise(searchAPI, 500);
  this.setState({ text, results: null });
  const result = await searchAPIDebounced(text);
  this.setState({ result });
};

Do this:

const searchAPI = text => fetch('/search?text=' + encodeURIComponent(text));
const searchAPIDebounced = AwesomeDebouncePromise(searchAPI, 500);

handleTextChange = async text => {
  this.setState({ text, results: null });
  const result = await searchAPIDebounced(text);
  this.setState({ result });
};

Dependencies

This library is a combination of multiple low-level tiny microlibs:

Hire a freelance expert

Looking for a React/ReactNative freelance expert with more than 5 years production experience? Contact me from my website or with Twitter.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].