# referential

> Mutable state container for sharing data safely in a functional or reactive programming style.

Latest version **0.5.25** (published 2019-01-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install referential
pnpm add referential
yarn add referential
bun add referential
```

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.5.25 |
| Published | 2019-01-19 |
| First published | 2015-12-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=4.0.0 |
| Dependencies | 3 |
| Unpacked size | 73.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Zach Kelling |
| Maintainers | davidtai, zeekay |
| Keywords | datastructure, immutable, pointers, react, reactive, reference, referential, state, store, tree |

## Links

- npm: https://www.npmjs.com/package/referential
- Repository: https://github.com/zeekay/referential
- Homepage: http://github.com/zeekay/referential
- Issues: http://github.com/zeekay/referential/issues
- npm.io page: https://npm.io/package/referential

## Dependencies (3)

- [es-is](https://npm.io/package/es-is.md) ^3.3.10
- [riot-observable](https://npm.io/package/riot-observable.md) 3.0.0
- [es-object-assign](https://npm.io/package/es-object-assign.md) ^4.1.4

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.5.25 (latest) — 2019-01-19
- 0.5.23 — 2017-05-26
- 0.5.22 — 2017-05-26
- 0.5.21 — 2017-05-26
- 0.5.18 — 2017-04-13
- 0.5.17 — 2017-04-10
- 0.5.15 — 2017-03-21
- 0.5.14 — 2017-03-12
- 0.5.13 — 2017-03-11
- 0.5.12 — 2017-03-10
- 0.5.11 — 2017-03-10
- 0.5.10 — 2017-03-09
- 0.5.9 — 2017-03-08
- 0.5.8 — 2017-03-08
- 0.5.7 — 2017-03-07
- … 33 more at https://npm.io/package/referential/versions

## README

# referential

[![npm][npm-img]][npm-url]
[![build][build-img]][build-url]
[![dependencies][dependencies-img]][dependencies-url]
[![downloads][downloads-img]][downloads-url]
[![license][license-img]][license-url]
[![chat][chat-img]][chat-url]

> Refer to mutable state safely.

Referential makes it easy to share mutable state safely. A `Ref` to an object
or subtree always refers to the same underlying data regardless how it's
mutated.

Heavily optimized for reads.

### Motivating example
```javascript
var state = {a: {b: {c: "world"}}}

var render = (function() {
    var template = state.a.b.c;
    return function() {
        return "Hello " + template
    }
}())

state.a.b.c = "This will be lost on you."

console.log(render()) // Hello world
```

## Install
```bash
$ npm install referential --save
```

## Usage
```javascript
refer = require('referential')

// Create a reference
ref = refer({a: 1})

// Get underlying value of reference
ref()    // {a: 1}
ref('a') // 1

// Mutate state
ref.set('b', 2) // {a: 1, b: 2}
ref.set({c: 3}) // {a: 1, b: 2, c: 3}

// Extend state
ref.extend({c: {d: 1, e: 2}}) // {a: 1, b: 2, c: {d: 1, e: 2}}
ref.extend({c: {d: 3}})       // {a: 1, b: 2, c: {d: 3, e: 2}}

// Create entire tree as needed
ref.set('d.e.f', 4) // {a: 1, b: 2, c: 3, d: {e: {f: 4}}}

// Get reference to subtree
ref2 = ref.refer('d.e')
ref2() // {f: 4}

// Mutate subtree (and update parent)
ref2.set('g', 5)
ref2() // {f: 4, g: 5}

// Mutate parent (and update subtree)
ref.set('d.e.f', 6)
ref()  // {a: 1, b: 2, c: 3, d: {e: {f: 6, g: 5}}}
ref2() // {f: 6, g: 5}

// Clone ref, create new tree
ref3 = ref2.clone()
ref3.set('g', 6)
ref3() // {f: 6, g: 6}
ref2() // {f: 6, g: 5}

// Bind to update event, should print `x was set to 2 from 1`
ref4 = refer({x: 1})
ref4.on('set', function(name, newValue, oldValue) {
    console.log(name + ' was set to ' + newValue + ' from ' + oldValue)
})
ref4.set('x', 2)
```

Check the tests for [more examples][examples].

## License
[MIT][license-url]

[examples]:         https://github.com/zeekay/referential/blob/master/test/test.coffee

[build-img]:        https://img.shields.io/travis/zeekay/referential.svg
[build-url]:        https://travis-ci.org/zeekay/referential
[chat-img]:         https://badges.gitter.im/join-chat.svg
[chat-url]:         https://gitter.im/zeekay/hi
[coverage-img]:     https://coveralls.io/repos/zeekay/referential/badge.svg?branch=master&service=github
[coverage-url]:     https://coveralls.io/github/zeekay/referential?branch=master
[dependencies-img]: https://david-dm.org/zeekay/referential.svg
[dependencies-url]: https://david-dm.org/zeekay/referential
[downloads-img]:    https://img.shields.io/npm/dm/referential.svg
[downloads-url]:    http://badge.fury.io/js/referential
[license-img]:      https://img.shields.io/npm/l/referential.svg
[license-url]:      https://github.com/zeekay/referential/blob/master/LICENSE
[npm-img]:          https://img.shields.io/npm/v/referential.svg
[npm-url]:          https://www.npmjs.com/package/referential

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