New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

segal-decorators

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

segal-decorators

Bunch of useful decorators

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
51
increased by50%
Maintainers
1
Weekly downloads
 
Created
Source

Segal Decorators

Bunch of useful decorators

Table of contents

  • Installation
  • Contributing
  • Changelog
  • Decorators

Installation

Use npm to install the package

$ npm install segal-decorators --save 

Decorators

Memoize

Memoize the function by using the cached result. If hashFn is passed it is used to compute the hash key. default hash function is using JSON.stringify on the original function arguments.

Declaration

function Memoize(hashFn?: (...args: any[]) => string)

Usage
import { Memoize } from 'segal-decorators';

class Foo {
  counter = 0;
  
  @Memoize()
  count(...args: any[]) {
    return ++this.counter;
  }
}

console.log(test.count(1)); // Outputs: 1
console.log(test.count(2)); // Outputs: 2
console.log(test.count(1)); // Outputs: 1
console.log(test.count(1, 2)); // Outputs: 3
console.log(test.count(2)); // Outputs: 2

Time Memoize

Time memoization, similar to Memoize, requires an additional parameter timeoutMs to determine the amount of time in milliseconds to cache the result.

Declaration

function TimeMemoize(timeoutMs: number, hashFn?: (...args: any[]) => string)

Usage
import { TimeMemoize } from 'segal-decorators';

class Foo {
  counter = 0;
  
  @TimeMemoize(2000)
  count(...args: any[]) {
    return ++this.counter;
  }
}

console.log(test.count(1)); // Outputs: 1
console.log(test.count(2)); // Outputs: 2
console.log(test.count(1)); // Outputs: 1
console.log(test.count(2)); // Outputs: 2

setTimeout(() => {
  console.log(test.count(1)); // Outputs: 3
  console.log(test.count(2)); // Outputs: 4
}, 3000);

Lru Memoize

Least-recently-used memoization, similar to Memoize, requires an additional parameter maxRecords of the number of records to keep in cache.

Declaration

function LruMemoize(maxRecords: number, hashFn?: (...args: any[]) => string)

Usage
import { LruMemoize } from 'segal-decorators';

class Foo {
  counter = 0;
  
  @LruMemoize(2)
  count(...args: any[]) {
    return ++this.counter;
  }
}

console.log(test.count(1)); // State of LRU Cache=[1], Outputs: 1 
console.log(test.count(2)); // State of LRU Cache=[2, 1], Outputs: 2
console.log(test.count(3)); // State of LRU Cache=[3, 2], Outputs: 3
console.log(test.count(2)); // State of LRU Cache=[2, 3], Outputs: 2
console.log(test.count(1)); // State of LRU Cache=[1, 2], Outputs: 4

Debounce

Postpone its execution until after timeMs have elapsed since the last time it was invoked.

Declaration

function Debounce(timeMs: number)

Usage
import { Debounce } from 'segal-decorators';

class Foo {
  counter = 0;
  
  @Debounce(2000)
  call() {
    return ++this.counter;
  }
}

console.log(test.call()); // Does not output anything since it's called again with-in the 2 second time-frame.
console.log(test.call()); // Outputs: 1 (AFTER 2 SECONDS)

setTimeout(() => {
  console.log(test.call()); // Outputs: 1 (AFTER ADDITIONAL 2 SECONDS)
}, 3000)

Throttle

When invoked repeatedly, will only call the original function at most once per every timeMs.

Declaration

function Throttle(timeMs: number)

Usage
import { Throttle } from 'segal-decorators';

class Foo {
  counter = 0;
  
  @Throttle(2000)
  call() {
    return ++this.counter;
  }
}

console.log(test.call()); // Outputs: 1
console.log(test.call()); // Does not output anything since it's called again with-in the 2 second time-frame.

setTimeout(() => {
  console.log(test.call());
  // Outputs: 1 
}, 3000)

Async Retry

In-case an exception has been thrown, it will retry the action up to retries amount of times with a restMs delay between attempts.

Declaration

function AsyncRetry(retries: number, restMs: number = 100)

Usage
import { AsyncRetry } from 'segal-decorators';

class Foo {
  counter = 0;
  
  @AsyncRetry(2)
  doSomething() {
    return requestServerSomething()
  }
}

// in-case of a failure will try twice before throwing an exception
test.doSomething().then((response) => {
  // Reach here if succeed in one of the 2 attempts
}).catch(() => {
  // Reach here if failed after 2 attempts
})

Once

Called exactly once. Repeated calls will have no effect, returning the value from the first call.

Declaration

function Once()

Usage
import { Once } from 'segal-decorators';

class Foo {
  counter = 0;
  
  @Once()
  count(...args: string[]) {
    return ++this.counter;
  }
}

console.log(test.count()); // Outputs: 1
console.log(test.count()); // Outputs: 1
console.log(test.count(1)); // Outputs: 1
console.log(test.count(1, 2)); // Outputs: 1

Keywords

FAQs

Package last updated on 31 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc