3.1.21 • Published 9 years ago

vigour-observable v3.1.21

Weekly downloads
6
License
ISC
Repository
github
Last release
9 years ago

vigour-observable

Build Status js-standard-style npm version Coverage Status

Simple, extendable observables - does not require knowledge of flatmaps, reduce or event streams, inspired by rx-js and modelled as observ-struct.

Adds unqiue change stamps see vigour-stamp for more information

Inherits from vigour-base

####operators and set

var Observable = require('vigour-observable')
var name = new Observable({
  val: 'a name',
  $transform (val) {
    return val.toUpperCase()
  }
})

var person = new Observable({ name: 'a name' })

person.name.on(() => {
  // logs "A NAME" when set to the reference
  console.log('hey a name!', person.name.compute())
})

// create an observing reference from person.name to name
person.name.set(name)

// fires the listener on person.name, logs "JAMES"
name.set('james')

####on and off

var Observable = require('vigour-observable')
var fruits = new Observable()

fruits.on((data, stamp) => {
  // stamp is a unique stamp for each change see vigour-stamp
  // data is the current set obj { banana: 1, kiwi: 1 } in this case
  console.log('fruits!', data, stamp)
})

// this will fire the listener on fruits
fruits.set({ banana: 1, kiwi: 1 })

fruits.banana.once(() => {
  console.log('banana!')
})

// fires banana, does not fire for fruits (by default listeners do not fire for nested fields)
fruits.banana.set(2)

fruits.set({
  // this is the object notation for on listeners - usefull for inheritance
  on: {
    data () {
      // on.data is the default emitter of on
      console.log('any fruit?')
    }
  }
})

// remove the kiwi and fires "any fruit?" and "fruits"
fruits.kiwi.set(null)

// you can make as many emitter types as you want
// will add a listener to the emitter "purchase" with an identifier "history"
fruits.on('purchase', (fruit) => {
  console.log('a purchase!', data)
  this.set({ purchaseHistory: { [fruit]: 1 } })
}, 'history')

fruits.emit('pruchase', 'kiwi')

// remove the listener on purchase with id history
fruits.off('purchase', 'history')

// removes all listeners on purchase
fruits.off('purchase')

// adds the same listener using the object notation
fruits.set({
  on: {
    purchase: {
      history (fruit) {
        this.set({ purchaseHistory: { [fruit]: 1 } })
      }
    }
  }
})
3.1.21

9 years ago

3.1.20

9 years ago

3.1.19

9 years ago

3.1.18

9 years ago

3.1.17

9 years ago

3.1.16

9 years ago

3.1.15

9 years ago

3.1.14

9 years ago

3.1.13

9 years ago

3.1.12

9 years ago

3.1.11

9 years ago

3.1.10

9 years ago

3.1.9

9 years ago

3.1.8

9 years ago

3.1.7

9 years ago

3.1.6

9 years ago

3.1.5

9 years ago

3.1.4

9 years ago

3.1.3

9 years ago

3.1.2

9 years ago

3.1.1

9 years ago

3.1.0

9 years ago

3.0.0

9 years ago

2.1.12

9 years ago

2.1.11

9 years ago

2.1.10

9 years ago

2.1.9

9 years ago

2.1.8

9 years ago

2.1.7

9 years ago

2.1.6

9 years ago

2.1.5

9 years ago

2.1.4

9 years ago

2.1.3

9 years ago

2.1.2

9 years ago

2.1.1

9 years ago

2.1.0

9 years ago

2.0.5

9 years ago

2.0.4

9 years ago

2.0.3

9 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.2.1

9 years ago

1.2.0

9 years ago

1.1.2

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.15

9 years ago

1.0.14

9 years ago

1.0.13

10 years ago

1.0.12

10 years ago

1.0.11

10 years ago

1.0.10

10 years ago

1.0.9

10 years ago

1.0.8

10 years ago

1.0.7

10 years ago

1.0.6

10 years ago

1.0.5

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago