warg
A basic stream/reactive library designed to make dirty reads impossible
Last updated 10 months ago by tehshrike .
WTFPL · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install warg 
SYNC missed versions from official npm registry.

A basic reactive/stream library

This library is designed to make dirty reads impossible - i.e. your computed properties will only be called when there is a consistent state for them to run on.

This library runs calculations synchronously and eagerly. If you have a ton of upstream data changing and want lazy calculations, use shiz.

Install

npm i warg

CommonJS:

const { value, computed } = require('warg')

ESM:

import { value, computed } from 'warg'

Examples

const numbers = value([ 1, 2, 3, 42, 69])
const factor = value(2)
const multipliedArray = computed({
	numbers,
	factor,
}, ({ numbers, factor }) => numbers.map(number => number * factor))

multipliedArray.get() // => [ 2, 4, 6, 84, 138 ]

API

wargObservable = value(initialValue)

Returns a warg observable with a set method.

const wat = value('sup')

wat.get() // => 'sup'

wat.set('dawg')

wat.get() // => 'dawg'

wargObservable = computed(dependencies, computeFunction)

Returns a warg observable that takes in an object of dependencies and returns a new warg observable that combines them together with your given function.

const small = value(1)

const bigger = computed({
	number: small
}, ({ number }) => number + 1)

const multiplied = computed({
	a: small,
	b: bigger,
}, ({ a, b }) => a * b)

multiplied.get() // => 2

Warg observable

Besides any methods above, warg observables have these methods:

wargObservable.get()

Returns the current value.

newWargObservable = wargObservable.map(computeFunction)

Sugar for newWargObservable = wargObservable.compute({ observable }, ({ observable }) => computeFunction(observable)).

const a = value(6)
const special = a.map(value => value * 7)
special.get() // 42

unsubscribe = wargObservable.subscribe(callback)

Calls the callback function whenever the observable value changes.

Also calls the callback function with the current value right away when subscribe is called.

value('whu?').subscribe(str => {
	str // => 'whu?'
})

Events

Warg observables are also event emitters, though the events are more for internal library use. You probably won't need them.

These events are fired:

  • dirty - fired whenever a warg observable knows that it, or one of its dependencies, is changed. You may not read from the observable after this point, until it finishes resolving.
  • value - fired whenever a warg observable is resolved and becomes readable again - essentially, whenever its value has changed.

Because warg resolves all changes synchronously in the same tick, you should never need to worry about values becoming dirty and resolving themselves. That all happens behind the scenes before your set call completes.

License

WTFPL

Current Tags

  • 2.1.0                                ...           latest (10 months ago)

10 Versions

  • 2.1.0                                ...           10 months ago
  • 2.0.2                                ...           10 months ago
  • 2.0.1                                ...           10 months ago
  • 2.0.0                                ...           10 months ago
  • 1.2.2                                ...           a year ago
  • 1.2.1                                ...           a year ago
  • 1.2.0                                ...           a year ago
  • 1.1.2                                ...           2 years ago
  • 1.1.1                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 3
Dependencies (0)
None
Dev Dependencies (9)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |