9.0.0-alpha.13 • Published 8 months ago

flamingo-carotene-state-manager v9.0.0-alpha.13

Weekly downloads
130
License
OSL-3.0
Repository
github
Last release
8 months ago

Module flamingo-carotene-state-manager

The state-manager gives you the possibility to set and get states, and watch changes. Under the hood redux is used.

How to initialize

npm i -D flamingo-carotene-state-manager

Import state manager

import state from 'flamingo-carotene-state-manager'

Note: The state-manager is a singleton and already returns an instance but a class.

Creating initial state

const initialState = {
 {
  foo: true,
  bar: {
    foo: 'oof',
    baz: 'zab'
  }
  baz: {
    data: null
  }
}

Initialize class with initial state

state.init(initialState)

How to use

Getting a state

You can get a value of a state by using get(). The first parameter is a .-notated path of the value inside the state object.

state.get('bar.baz')

Setting a state

By calling set() you'll set a new value for a key. You can even create new state values with set.

state.set('baz.data', 'myNewValue')

Watching a state

You can watch for a state change. Just call watch() with a key you're like to watch, and the method will be called instantly, when a state change happens. The callback will get the new state as the first parameter.

state.watch(`baz.data`, (newValue) => {
  console.log('State "baz.data" has changed to new value!', newValue) 
})

Note that the newValue is an immutable. So you can't change the state by changing newValue inside the watcher. To change states, always call set().

Options

state.watch(`baz.data`, myCallback, { noCompare: true })
noCompare

You can enforce triggering listener functions even if the state has not changed by adding noCompare: true to the options object.

Default is false

9.0.0-alpha.13

8 months ago

9.0.0-alpha.4

12 months ago

9.0.0-alpha.12

11 months ago

9.0.0-alpha.0

1 year ago

7.1.1

1 year ago

8.1.0

1 year ago

8.0.0

1 year ago

8.1.0-alpha.0

1 year ago

7.0.0

2 years ago

7.0.4

2 years ago

7.0.7

2 years ago

6.7.29

2 years ago

6.7.28

3 years ago

6.7.25

3 years ago

6.7.24

3 years ago

6.7.23

3 years ago

6.7.22

3 years ago

6.7.21

3 years ago

6.7.11

3 years ago

6.7.10

3 years ago

6.7.4

3 years ago

6.7.2

4 years ago

6.7.0

4 years ago

6.5.21

4 years ago

6.5.20

4 years ago

6.5.15

4 years ago

6.5.14

4 years ago

6.5.13

4 years ago

6.5.0

4 years ago

6.4.0

4 years ago

6.3.6

4 years ago

6.3.3

4 years ago

6.3.1

4 years ago

6.3.0

4 years ago

6.2.18

4 years ago

6.2.17

4 years ago

6.2.16

5 years ago

6.2.15

5 years ago

6.2.13

5 years ago

6.2.1

5 years ago

6.2.0

5 years ago