@shockw4ver/class

Some useful class tools, maybe.

Usage no npm install needed!

<script type="module">
  import shockw4verClass from 'https://cdn.skypack.dev/@shockw4ver/class';
</script>

README

Shockw4ver/class

There are something make you happy...😎

autobind-lite

A tiny autobind decorator, mainly be useful with function declaration in ReactJS class components.

  • Usage

    import React from 'react'
    import { autobind } from '@shockw4ver/class'
    
    export default class extends React.Component {
      state = {
        msg: 'Hellow world!'
      }
    
      @autobind
      handleClick () {
        // 👇 Got TypeError if you didn't use `autobind` or other ways to bind `handleClick` on your class.
        const { msg } = this.state
        alert(msg)
      }
    
      render () {
        return <button onClick={this.handleClick}></button>
      }
    }
    
    

only

To limit times could a delay function be called at same time.

  • Usage

    Pretend to has a case with when method in mobx.

    import { observable, when } from 'mobx'
    
    class UserStore {
      @observable isLogin = false
      @observable data = {}
    
      @only(1)
      async fetchData () {
        when(() => isLogin === true)
    
        const res = await fetch('some_url')
    
        this.data = res.json()
      }
    }
    

    If there're two components without any relationshiop but call the fetchData at the same time, with only(1), there will just one fetchData works, and all of them will react to the store.