unstated-suspense-autosuspend
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

Unstated Suspense Autosuspend

Automatically use unstated-suspense on all your container's API methods.

It supports methods returning promises, it re-throws any thrown exceptions, and it supports bubbling up the suspension to parent containers (in case you're using unstated-compose).

Only methods defined in your container, and not somewhere further down in its prototype chain, will be autosuspended.

Install

npm install --save unstated-suspense-autosuspend

Usage

It supports a second optional options object which by default looks like this:

{
  bubbles: Infinity, // How many levels to bubble up the suspension
  methods: /^(?!_|middleware|(?:(?:get|is|has)(?![a-z0-9])))/, // Methods matching this regex will be autosuspended
  middlewares: true // Suspend middlewares as well
}

Alternatively you can assign your options for unstated-suspense-autosuspend to your container's autosuspend property.

import autosuspend from 'unstated-suspense-autosuspend';
import {Container} from 'unstated-suspense';

class App extends Container {
  // autosuspend = false; // Disables `unstated-suspense-autosuspend` for this container
  // autosuspend = { bubbles: false } // Disables bubbling
  // autosuspend = { methods: /^api/ } // Only methods whose names start with "api" will be autosuspended
  constructor () {
    super ();
    autosuspend ( this );
    // autosuspend ( this, {...} ) // Passing custom options via the API
  }
  middlewareFoo () {} // Not autosuspended, it's name doesn't match `options.methods`
  _foo () {} // Not autosuspended, it's name doesn't match `options.methods`
  update () { // Autosuspended
    this.setFoo ( 11 );
    this.setBar ( 12 );
  }
  setFoo ( foo ) { // Autosuspended
    this.setState ({ foo });
  }
  setBar ( bar ) { // Autosuspended
    this.setState ({ bar });
  }
}

Related

License

MIT © Fabio Spampinato

Package Sidebar

Install

npm i unstated-suspense-autosuspend

Weekly Downloads

1

Version

1.2.0

License

MIT

Unpacked Size

14.9 kB

Total Files

8

Last publish

Collaborators

  • fabiospampinato