Skip to content

Commit

Permalink
docs: Updating README
Browse files Browse the repository at this point in the history
  • Loading branch information
aholstenson committed Jul 27, 2019
1 parent 1db0e37 commit ec31822
Showing 1 changed file with 61 additions and 9 deletions.
70 changes: 61 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@
[![Coverage Status](https://coveralls.io/repos/aholstenson/timer-wheel/badge.svg)](https://coveralls.io/github/aholstenson/timer-wheel)
[![Dependencies](https://david-dm.org/aholstenson/timer-wheel.svg)](https://david-dm.org/aholstenson/timer-wheel)

`timer-wheel` is a JavaScript library for efficiently managing a large amount
of timed actions. It allows you to schedule actions after a certain delay and
then control when to advance and run actions where the delay has passed.
`timer-wheel` is a library for JavaScript and TypeScript for efficiently
managing the expiration of a large amount of items. It allows you to schedule
items to be expired after a certain delay and then control when to advance and
handle the expired items.

This implementation is designed for larger delays and has a minimum delay of
1000 ms.

```javascript
```typescript
import { TimerWheel } from 'timer-wheel';

const wheel = new TimerWheel();
wheel.schedule(() => {
console.log('Action invoked');
}, 1500 /* ms */)
const wheel = new TimerWheel<any>();
wheel.schedule('data', 1500 /* ms */)

// Call `advance()` to advance the wheel and run actions
setInterval(() => {
wheel.advance();
const expired = wheel.advance();
for(const data of expired) {
// Do something with the expired data
}
}, 1000);
```

Expand All @@ -31,3 +33,53 @@ instead of checking if every cache item should be expired use a wheel to queue
removal actions and call `advance` before every get/set. This is how
[Transitory](https://github.com/aholstenson/transitory) implements expiring
caches.

## Rescheduling

By default `TimerWheel` will schedule the same object to expired more than once.
This will schedule `obj` to be expired both after 1 and 5 seconds:

```javascript
const wheel = new TimerWheel();
const obj = {};

// Schedule to be expired after 1 seconds
wheel.schedule(obj, 1000);

// Schedule to also be expired after 5 seconds
wheel.schedule(obj, 5000);
```

If you want to be able to reschedule when an item expires use
`ReschedulingTimerWheel`:

```javascript
import { ReschedulingTimerWheel } from 'timer-wheel';

const wheel = new ReschedulingTimerWheel();

const obj = {};

// First schedule at 1 second
wheel.schedule(obj, 1000);

// Replace first scheduling with a new one after 5 seconds
wheel.schedule(obj, 5000);
```

## Running actions

There's a wheel designed to run actions available:

```javascript
import { ActionTimerWheel } from 'timer-wheel';

const wheel = new ActionTimerWheel();

wheel.schedule(() => {
/* do something here */
}, 8000);

// Advance the wheel to run expired actions
wheel.advance();
```

0 comments on commit ec31822

Please sign in to comment.