# function-tracker

> Tracks the value returned by a function across multiple calls so events can be attached to changes

Latest version **1.1.0** (published 2018-04-14) · CC-BY-SA-4.0 license · 0 weekly downloads

## Install

```sh
npm install function-tracker
pnpm add function-tracker
yarn add function-tracker
bun add function-tracker
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2018-04-14 |
| First published | 2018-03-29 |
| Weekly downloads | 0 |
| License | CC-BY-SA-4.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 10.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Tom Klinovsky |
| Maintainers | lutraman |
| Keywords | tracker, events |

## Links

- npm: https://www.npmjs.com/package/function-tracker
- Repository: https://github.com/tomklino/function-tracker
- Homepage: https://github.com/tomklino/function-tracker#readme
- Issues: https://github.com/tomklino/function-tracker/issues
- npm.io page: https://npm.io/package/function-tracker

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2018-04-14
- 1.0.2 — 2018-03-29
- 1.0.1 — 2018-03-29
- 1.0.0 — 2018-03-29

## README

# function-tracker
Tracks the value returned by a function across multiple calls so events can be attached to changes

function-tracker tracks functions that return an array or a Promise that resolves to an array, and fires events such as:

* `element_join` when an element that did not exist in the previous return value appears in the current one
* `element_left` when an element that existed in the previous return value does not appear in the current one

## Installation

```js
npm i --save function-tracker
```

## Example usage

```js
const trackerFactory = require('function-tracker')

let usersTracker = trackerFactory({
  watchFunction: () => {
    //some function that fetches something and returns an array (or a Promise that resolves to an array)
  }
})
.on('element_join', (new_users) => { console.log("there are new users:", new_users) })
.on('element_left', (departed_users) => { console.log("these people are no longer with us:", departed_users) })

usersTracker.call() //returns [{username: "tom"}, {username: "ben"}]
//and after a while...
usersTracker.call() //returns [{username: "tom"}, {username: "maya"}]

//event element_join will be called with [{username: "maya}]
//event element_left will be called with [{username: "ben"}]
```

## Advanced usage

Let's assume we have a function `getActiveUsers` that returns the following format:

```js
[{username: "tom", active_since: 15234523711}, {username: "ben", active_since:  15234522612}]
```

Obviously the next time we call that function, all of the elements will be different becuase of the `active_since` field, even though the users tom and ben had not left or joined.

We can set the way `function-tracker` compares item from the last call to the current one by passing the logic we want to use to the `setComparator` method, like so:

```js
let usersTracker = trackerFactory({
  watchFunction: getActiveUsers
})
.setComparator((a,b) => { return a.username === b.username })
.on('element_join', (new_users) => { console.log("someone really joined!", new_users })
```

---
_Source: https://npm.io/package/function-tracker · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
