Skip to content

caiogondim/with-subscribe.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

with-subscribe

 Travis CI

Minimal Observable interface to watch for object modifications.

Installation

npm install --save with-subscribe

Usage

On a constructor:

class Counter {
  constructor() {
    this.count = 0
  }

  increment() {
    this.count += 1
  }
}
const CounterWithSubscribe = withSubscribe(Counter)
const counter = new CounterWithSubscribe()

counter.subscribe(() => console.log('State:', counter.count)) // -> State: 0

counter.increment() // -> State: 1
counter.increment() // -> State: 2

As a decorator:

@withSubscribe
class Counter {
  constructor() {
    this.count = 0
  }

  increment() {
    this.count += 1
  }
}
const counter = new Counter()

counter.subscribe(() => console.log('State:', counter.count)) // -> State: 0

counter.increment() // -> State: 1
counter.increment() // -> State: 2

On an object:

const counter = withSubscribe({
  count: 0,
  increment() {
    this.count += 1
  }
})

counter.subscribe(() => console.log('State:', counter.count)) // -> State: 0

counter.increment() // -> State: 1
counter.increment() // -> State: 2

caiogondim.com  ·  GitHub @caiogondim  ·  Twitter @caio_gondim

About

Minimal Observable interface to watch for object modifications.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published