Giter VIP home page Giter VIP logo

decko's Introduction

decko NPM Version Build Status

A concise implementation of the three most useful decorators:

  • @bind: make the value of this constant within a method
  • @debounce: throttle calls to a method
  • @memoize: cache return values based on arguments

Decorators help simplify code by replacing the noise of common patterns with declarative annotations. Conversely, decorators can also be overused and create obscurity. Decko establishes 3 standard decorators that are immediately recognizable, so you can avoid creating decorators in your own codebase.

๐Ÿ’ก Tip: decko is particularly well-suited to Preact Classful Components.

๐Ÿ’ซย Note:

Sponsor

Installation

Available on npm:

npm i -S decko

Usage

Each decorator method is available as a named import.

import { bind, memoize, debounce } from 'decko';

@bind

class Example {
	@bind
	foo() {
		// the value of `this` is always the object from which foo() was referenced.
		return this;
	}
}

let e = new Example();
assert.equal(e.foo.call(null), e);

@memoize

Cache values returned from the decorated function. Uses the first argument as a cache key. Cache keys are always converted to strings.

Options:

caseSensitive: false - Makes cache keys case-insensitive

cache: {} - Presupply cache storage, for seeding or sharing entries

class Example {
	@memoize
	expensive(key) {
		let start = Date.now();
		while (Date.now()-start < 500) key++;
		return key;
	}
}

let e = new Example();

// this takes 500ms
let one = e.expensive(1);

// this takes 0ms
let two = e.expensive(1);

// this takes 500ms
let three = e.expensive(2);

@debounce

Throttle calls to the decorated function. To debounce means "call this at most once per N ms". All outward function calls get collated into a single inward call, and only the latest (most recent) arguments as passed on to the debounced function.

Options:

delay: 0 - The number of milliseconds to buffer calls for.

class Example {
	@debounce
	foo() {
		return this;
	}
}

let e = new Example();

// this will only call foo() once:
for (let i=1000; i--) e.foo();

License

MIT

decko's People

Contributors

developit avatar hotell avatar bryanjenningz avatar dalaidunc avatar

Watchers

ScriptMaster avatar James Cloos avatar  avatar

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.