npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pstore

v1.1.2

Published

A state management library mostly for react and convenience

Downloads

5

Readme

pStore

Global / local state management with js proxies, originally made for React hooks but can be used without. As of v1.1.0 Currently under active development after discovering concerning bugs - should not be used by anyone or any project at the moment.

Updates

  • v1.1.0 - declared this repo not usable yet
  • v1.1.1 - Error fixes
  • v1.1.2 - Added some types for autocomplete

Disclaimer

This package was not intended to replace redux.
If your project has any level of complex logic that is best stored globally,
please use a more reliable global state management package which npm has a plethora to offer.

How to use

npm install pstore yarn install pstore

Setup

// in "src/contexts/myStore";
import Store from "pStore";
const initWallet = {  
  sideMenu: false,  
  preview: false,
  langauge: "eng",
};
//	these are the default options, any missing options will be merged with defaults.
const options = {
  preventExtensions: false,  // 	true to prevent store having extra properties after init.
  useHooks: true,	//	true to use hooks
  useEventListener: false,	//	true to use bindEvent, unbindEvent methods
  debugMode: false,	//	true for extra debugging info
  immutable: false,	//	true for frozen objects from store (always)
  typeChecks: false,  //	true to prevent attempts to switch store value to a different type(currently based on typeof, planning to expand features)
};
  
//	initStore, keyName(if not set, integer will be assigned), storeOptions
const myStore = new Store(initWallet, "myStore", options);  
//	pick needed methods
const [useMyStore, getMyState, getMyValue, updateMyStore, bindMyStore, unbindMyStore] = [  
	myStore.createUseStore(),  //	hook HOC. Only use once per store.
	myStore.getStore,	//	returns copy of current store frozen.
	myStore.getValue,	//	get any value from store 
	myStore.updateStore,
	myStore.bindEvent,
	myStore,unbindEvent
];
export {useMyStore, getMyState, getMyValue, updateMyStore, bindMyStore, unbindMyStore};

Usage example - hooks

//	...your imports
import {useMyStore} from "src/contexts/MyStore";  
  
export default function Header() {  
	// const [myStore, updateStore] = useMyStore(); // keep empty to get entire object
	const [sideMenu, updateStore] = useMyStore("sideMenu");	//	use param name for single parameter
	//	component will only rerender when that parameter is changed
	return (
		<div>
			{sideMenu ? <div>sidemenu</div>}
		</div>
	)
}

// some other component
//...
import {useMyStore} from "src/contexts/MyStore";

export default function Something() {
	const updateMyStore = useMyStore("");	//	undefined to only use updateStore
	React.useEffect(() => {
		//	update using object
		updateMyStore({sideMenu: true});
		
		//	update specifing property
		updateMyStore(true, "sideMenu");
		
		//	update using callback -> function input will be value inside "sideMenu"
		updateMyStore(v => !v, "sideMenu", {callback: true});
		
		//	update silently, store value will by updated but values will not rerender
		//	NOT RECOMMENDED TO USE EVER. Unless you know. Reasons.(I made it so guess)
		updateMyStore({sideMenu: true}, null, {silent: true});
		
		//	multiple simultaneous updates, single re-render. 
		//	Will be applied synchronously in order of array. 
		//	Can make silent by using third parameter.
		//	specifing second parameter does nothing atm.
		updateMyStore([
			[{sideMenu:true}],	//	enter params like single update in array form
			[v => !v, "preview", {callback: true}]
		]);
	}, [updateMyStore]);
}

Usage example - inside functions

//	...imports
import {updateMyStore, getMyValue} from "src/contexts/MyStore";

function myFunctionThatIsFiftyFunctionsNestedFromAnyReactComponent () {
	//	update using object
	updateMyStore({sideMenu: true});
	//	exactly the same as the hook example actually :)
	
	const value = getMyValue("sideMenu")	//	returns null if key is not defined
}

Usage example - events

By binding events you can watch value changes, but being a React developer, this has not been even used before.(as of writing) Will add example when the occasion calls for it.

Which project is this for

  • The main dev hates / is not bothered to use redux
  • Stores do not need multi-nested data updates.
  • The dev is sick of keeping multiple files open to check app logic
  • The dev wishes to update global state anywhere in the app without 'function drilling' the store update function
  • Intuitive for simple use, minimal learning curve for semi-advanced use.
  • Not for advanced usage. Learn redux and it's mega-ecosystem
  • As of writing this readme, this package has not been battle-tested by any production-level use-cases so is not recommended for large-scale serious apps(yet)
  • A co-worker finds Redux difficult to use and you agree(the origin story)

Plans

  • Optimizations for multi-updates
  • Currently only merges with store by default. Add different merge options
  • Remove events dependency with custom made event functionality
  • User suggestions if this package gets traction.
  • Write some test code
  • Use typescript?