Gathering detailed insights and metrics for @plancky/state
Gathering detailed insights and metrics for @plancky/state
Gathering detailed insights and metrics for @plancky/state
Gathering detailed insights and metrics for @plancky/state
npm install @plancky/state
Typescript
Module System
Min. Node Version
Node Version
NPM Version
70.4
Supply Chain
95.4
Quality
75.1
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
378
Last Day
1
Last Week
1
Last Month
2
Last Year
53
5 Commits
3 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
0.1.1
Package Id
@plancky/state@0.1.1
Unpacked Size
31.72 kB
Size
7.37 kB
File Count
20
NPM Version
6.14.11
Node Version
12.21.0
Cumulative downloads
Total Downloads
Last day
0%
1
Compared to previous day
Last week
0%
1
Compared to previous week
Last month
0%
2
Compared to previous month
Last year
-59.5%
53
Compared to previous year
Handling asyncronosities has always been a pain in state management libraries. Even with redux middleware like thunk it's still hard to work with async event listeners and clean them up in a safe way. I decided to create a new state management library that can handle effects inherently. You can find my thought process of creating the package in this article Building a React state management library with built in effects support.
1import createStore from '@plancky/state'; 2 3const store = createStore( 4 { username: 'Erik' }, 5 ({ setState }) => ({ 6 setUsername: (username) => { 7 setState(s => ({ username })); 8 } 9 }) 10); 11 12store.subscribe(() => { 13 console.log(store.getState()); 14}); 15 16store.setUseranem('George'); // Logs { username: "George" }
1import createStore from '@plancky/state'; 2 3const store = createStore( 4 { messages: [] }, 5 ({ setState, addEffect, removeEffect }) => ({ 6 startChat: () => { 7 addEffect('my_chat', () => { 8 const intRef = setInterval(() => { 9 setState(s => ({ messages: [...s.messages, 'new_msg']})); 10 }, 1000); 11 return () => clearInterval(intRef); 12 }); 13 }, 14 stopChat: () => { 15 removeEffect('my_chat'); 16 } 17 }) 18); 19 20store.actions.startChat(); // The state will be update with an additional message every second 21 22// ... some time later ... 23 24store.actions.stopChat(); // The store will no longer be updated by the effect
The createStore function takes 2 arguments. The first argument is the initial state of the store. The second argument is a function to initialize your actions. This function gets passed an object with properties getState, setState, addAction and removeAction. createStore returns a store object.
getState is a function which gets the current state of the store. It can either be called with store.getState or in an acttion with the utility properties provided to the action creator function (2nd argument of createStore).
setState is a function which changes the state and informs all the listeners about the change. It can ither be called with store.setState or in an action with the utility properties provided to the action creator function (2nd argument of createStore).
addEffect is the idiomatic way to create effects in @plancky/state. It takes 2 arguments - the id of the effect, and the effect itself. An effect is a funtion which starts an async task (event listener, setInterval, etc.) and returns a cleanup function. For example:
1addEffect('my_effect', () => { 2 const intRef = setInterval(() => { 3 console.log('hello'); 4 }, 1000); 5 return () => clearInterval(intRef); 6});
Now the effect with id "my_effect" will be initialized and "hello" will be logged every second. The addEffect funciton can be called with store.addEffect or in an action with the utility properties provided to the action creator function (2nd argument of createStore).
If you attempt to create an effect with the same id - an error will be thrown.
removeEffect cleans up an already initialized effect with it's id. Following up on the previous example:
1removeEffect('my_effect');
Now the effect with id "my_effect" is cleaned up. "hello" will no longer be logged to the console. The removeEffect funciton can be called with store.removeEffect or in an action with the utility properties provided to the action creator function (2nd argument of createStore).
store.actions is the object containing all the actions returned from the action creator function (2nd argument of createStore).
store.subscribe takes a callback which is called every time the store is changes (by setState). Hook into store.subscribe to react to state changes.
No vulnerabilities found.
No security vulnerabilities found.