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

dawei

v0.17.0

Published

A lightweight state library for react

Downloads

58

Readme

dawei... because it is da wei

Warning! for concurrent react must use version >= 0.14.0

versions less 0.14.0 can fail due to the way forceUpdate was written

the api has stabilized and is pretty solid at this point and I don't forsee any more breaking changes needing to be made. If that remains the case for a while that's cause for version 1.

Simple and fast state management for React.

Inspired by Zustand and Recoil. without the context or the fluff.

import { createStore } from 'dawei';

const formStore = createStore(/* this can be a value, a function or, nothing */);

// Use them anywhere in your app and they stay synced
const Input = () => {
  const [name, setName] = formStore.use('name');
  const [email, setEmail] = formStore.use('email');

  //  you can get and set deeply nested values too, even if they don't exist
  const [companyName, setCompanyName] = formStore.use('company.name');

  return (
    <form>
      <input value={name} onChange={e => setName(e.target.value)} />
      <input value={email} onChange={e => setEmail(e.target.value)} />
      <input value={companyName} onChange={e => setCompanyName(e.target.value)} />
    </form>
  );
};

function randomBitOfApi() {
  // you can update a bit of state from anywhere
  formStore.set({ saved: true });
}

// you can also subscribe to all changes
let unsubscribe = formStore.subscribe(state => {
  console.log('formStore changed', state);
});

API

store = createStore(initialState, storeName?: string)

  • initialState: Function | Object
    • initial state object or a function that returns initial state
    • (set: DaweiSetter, get: DaweiGetter) => initialState

store

  • get(selector string | function)
    • get value from the store, can be a pathed string into a nested store, or an accessor function
    • example: (both are equal)
      • store.get((store) => store.value.wanted)
      • store.get('value.wanted')
  • set(value, selector?)
    • set store value, can be any value, it will get merged into the current state
    • value - any value to set
    • selector - primarily used internally for use, can be a selector string similar to get to scope where the set value is to be applied
  • [value, setValue] = use(selector string | function)
    • use is a react hook that can be thought of similarly to react.useState(), except it uses a selector instead of a default value
    • selector - the store selector can be a pathed string or an accessor functon
      • example: ( both are equal) string selectors are preferred
        • store.use(state => state.value.wanted)
        • store.use('value.wanted')
    • returns
      • value - the value referenced by the selector
      • setValue

        WARNING! slightly different based on type of selector used

        • selector string - returns a scoped setter that will set value at the specified path
        • selector function - returns store.set
  • subscribe(listener: Function, receiveInitial: boolean)
    • subscribe to all store changes
    • listener(value: store values) - called for every change
    • receiveInitial - whether listener should be called with current state on subscribe
  • resolve()
    • mostly used internally or for testing libraries
    • since all set values accept async functions the entire store is promise based and is eventually consistent. chaining on resolve will guarentee that all of the changes made to the store will have settled.

Changelog

Version 0.17.0 fix: when the root was an array changes were incorrect, arrays now use assignment at root instead of Object.assign()

Version 0.16.0 feature: basic typings on use

Version 0.15.0 feature: basic inferred types on get based on intial store creation chore: switched to using pnpm for development

Version 0.11.0

chainMerge was rewritten to better handle pathing into arrays and now treats all numeric values in paths as array indexes

fixes a bug in version 0.10.0-alpha where promises had to be wrapped in functions

// {"arr":[]}
store.set({ test: 'through array' }, 'arr.0.test.1.ok');
//old chainMerge
// {"arr":[{"test":{"1":{"ok":{"test":"through array"}}}}]}

// new chainMerge
// {"arr":[{"test":[null,{"ok":{"test":"through array"}}]}]}

Version 0.10.0-alpha

This release makes use of unstable_batchedUpdates from react-dom to limit the number of rerenders that happen from making changes. This release also works slightly differently, it will merge synchronously if the update is a function