0.0.11 ā€¢ Published 3 years ago

@jonasrottmann/livedata v0.0.11

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

LiveData

LiveData is a very small observable value container targeted at small apps. The goal is to make observing app state as easy as possible.

šŸ§° Install

Install via npm/yarn:

npm install @jonasrottmann/livedata

yarn add @jonasrottmann/livedata

šŸ‘©ā€šŸ’» Usage

For a simple counter example check out counter.html or explore on CodePen.

šŸš§ This documentation is work in progress...

import {LiveData} from '@jonasrottmann/livedata'

// Create a new observable container with the initial value `true`
const livedata = new LiveData(true);

// Register an observer
const unsubscribe = livedata.subscribe((newValue, oldValue) => {
    console.log(`Value changed from ${oldValue} to ${newValue}!`)
})

// Change the value depending on the old value
livedata.transition(value => !value)

// Set the value
livedata.set(true)

// End the subscription
unsubscribe()

LiveData additionally can receive two callbacks onActive and onInactive, which will be called when the first observer is added or the last one removed. This can be useful for adding/removing event listeners to modify the LiveDatas value.

const listener = e => keyboardLiveData.set(e)
const keyboardLiveData = new LiveData(
    undefined,
    // onActive
    () => window.addEventListener('keyup', listener),
    // onInactive
    () => window.removeEventListener('keyup', listener)
)

keyboardLiveData will start listening to keyboard presses as soon as the first observer calls subscribe and will stop when the last observer has been removed.

šŸ‘Øā€āš–ļø License

MIT

0.0.10

3 years ago

0.0.11

3 years ago

0.0.9

3 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago