1.0.38 • Published 1 month ago

vast v1.0.38

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

Vast - Simple State Utility for Libraries

Vast is a simple state utility created to use in Vest. It allows using a similar pattern to React's useState hook to store data.

Note that this is mostly intended to be used within libraries, and not as a consumer facing interface. When paired with a context propagation libraries such as context, it can have pretty powerful capabilities. See Vest for a real life example.

Installation

npm i vast

Usage

import { createState } from 'vast';

const state = createState(); // Creates a state reference.

const useColor = state.registerStateKey('blue'); // Creates a new key in the state, and gives it an initial value
// You can also pass in a function to use as the initial state

const [color, setColor] = useColor(); // ["blue", Function]
setColor('red'); // set the color to "red"

The next time you will call useColor the value of color will be "red".

You can also set a computed value by passing in a function:

const [color, setColor] = useColor(); // ["blue", Function]
setColor(currentColor => (color === 'red' ? 'blue' : 'red'));

Subscribing to changes

Getting notified for every change in the state

NOTE This will not let you know what change was made, but only that a key in the state was updated or added:

Simply add an onChange callback to your createState:

const state = createState(() => console.log('the state was updated!'));

Now on every state change, this callback will run.

Alternatively, if you need more granularity, you can subscribe to specific state keys.

Subscribing to specific state key updates

If you need to react to specific changes in your state, you can add an onUpdate callback to those state key registrations:

const useColor = state.registerStateKey(
  'red',
  (currentState, previousState) => {
    console.log(`the color changed from ${previousState} to ${currentState}!`);
  }
);

Now, whenever a state update happens in that key, your callback will run, providing the previous and changed value as well.

1.0.38

1 month ago

1.0.37

1 month ago

1.0.36

2 months ago

1.0.35

3 months ago

1.0.34

3 months ago

1.0.33

4 months ago

1.0.32

4 months ago

1.0.31

4 months ago

1.0.30

4 months ago

1.0.29

5 months ago

1.0.26

5 months ago

1.0.25

6 months ago

1.0.24

6 months ago

1.0.23

6 months ago

1.0.28

5 months ago

1.0.27

5 months ago

1.0.19

8 months ago

1.0.18

8 months ago

1.0.18-next-04eea9

11 months ago

1.0.18-next-cc6009

11 months ago

1.0.22

8 months ago

1.0.21

8 months ago

1.0.20

8 months ago

1.0.18-dev-ec989a

12 months ago

1.0.18-next-a084e9

11 months ago

1.0.18-next-979ab9

11 months ago

1.0.18-next-2cf993

11 months ago

1.0.18-next-25c20e

11 months ago

1.0.18-next-1b0637

12 months ago

1.0.18-dev-ae6b14

12 months ago

1.0.18-next-83f17b

11 months ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.18-next-8222e6

12 months ago

1.0.18-next-ac1cf9

12 months ago

1.0.18-next-ed535b

12 months ago

1.0.18-next-70dac5

12 months ago

1.0.18-next-1de937

12 months ago

1.0.18-next-470211

12 months ago

1.0.15

1 year ago

1.1.0-dev-c4ba9b

2 years ago

1.1.0-dev-f30a30

2 years ago

1.0.11

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

2.0.0-dev-1aae50

2 years ago

2.0.0-dev-cc2a13

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

2.0.0-dev-031d95

2 years ago

2.0.0-dev-e266d9

2 years ago

2.0.0-dev-fc2efe

2 years ago

2.0.0

2 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

9 years ago

0.0.0

11 years ago