@ars9/decay
TypeScript icon, indicating that this package has built-in type declarations

0.9.2 • Public • Published

@ars9/decay

A simple class to store a numeric value that decays with time

Example:

const { Decay } = require('@ars9/decay');

// Default configuration
const decay = new Decay({
  initialValue: 1,
  decayValue: 1,
  minValue: 0,
  maxValue: Infinity,
  interval: 1000,
  type: DecayType.Linear,
});

// Start decay
decay.start();
console.log(decay.value); // 1

// You can subscribe for an event when the decay hits the bottom
decay.on('min', (value) => console.log(value));

// You can subscribe for an event when the value maxes out (not in this case)
decay.on('max', (value) => console.log(value));

// You can subscribe for an event when the decay has been stopped manually
decay.on('stop', (value) => console.log(value));

// Wait for the value to decay to minumum value (or to be maxed out / stopped)
await decay.wait(); 
console.log(decay.value); // 0

// You can manually increment the value
decay.increment(10);
console.log(decay.value); // 10

// ...or decrement
decay.decrement(1);
console.log(decay.value); // 9

// Don't forget to stop decay to cleanup timers
decay.stop();

Readme

Keywords

Package Sidebar

Install

npm i @ars9/decay

Weekly Downloads

1

Version

0.9.2

License

MIT

Unpacked Size

367 kB

Total Files

8

Last publish

Collaborators

  • ars9