observ-listen-if

1.0.1 • Public • Published

observ-listen-if Build Status

Manage an observable listener based on another boolean observable

Install

$ npm install --save observ-listen-if

Usage

var listenIf = require('observ-listen-if')
var Observ = require('observ')

var condition = Observ(false)
var value = Observ(0)

listenIf(condition, value, function (value) {
  //=> 2
})

value.set(1) // noop, condition === false

conditions.set(true)
value.set(2) // listener runs

To watch instead (triggering listeners immediately and upon change instead of just upon change):

var watchIf = require('observ-listen-if/watch')
watchIf(condition, value, listener)

API

listenIf(condition, value, fn) -> function

Returns an unlisten function. The listener will be called for changes to value when the condition is true.

condition

Required
Type: function

An observable. The value can be anything but the value will be cast to a boolean before change detection occurs. The value listener will be removed when the condition becomes false.

value

Required
Type: function

An observable value.

fn

Required
Type: function
Arguments: value

A listener function to call when the value is set after the condition has become true.

License

MIT © Ben Drucker

Package Sidebar

Install

npm i observ-listen-if

Weekly Downloads

0

Version

1.0.1

License

MIT

Last publish

Collaborators

  • bendrucker