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

@pragma-web-utils/common-state

v0.3.2

Published

types

Downloads

47

Readme

CommonState

CommonState is about simple managing async states.

Interfaces

Base state entity interfaces type

interface State<T = unknown, E = unknown> {
  value: T
  error: E | undefined
  isLoading: boolean
  isActual: boolean
}

export interface Cacheable<T = unknown> {
  cached?: T
}

interface Refreshable {
  softRefresh: () => Destructor | void
  hardRefresh: () => Destructor | void
}

interface CacheableState<T = unknown, E = unknown> extends State<T, E>, Cacheable<T> {}
interface RefreshableState<T = unknown, E = unknown> extends State<T, E>, Refreshable {}

interface CommonState<T = unknown, E = unknown> extends State<T, E>, Refreshable, Cacheable<T> {}

Short field description:

  • value - stored value
  • error - last error on value update
  • isLoading - is loading new value right now
  • isActual - is current value is actual or need update for actualizing
  • cached - last loaded actual value
  • softRefresh - update value without resetting actuality (emit load new value, set isLoading to true until wait new value, after update set isActual as true)
  • hardRefresh - update value with resetting actuality (emit load new value, set isLoading to false and isActual to false until wait new value)

Usage

For init and manage CommonState use useCommonState hook.

interface StateRefreshOption<T, E> {
  refreshFn: () => Promise<T>
  requestKey?: string
  onError?: (error: E, state: CacheableState<T, E>) => void
}

interface StateManager<T = unknown, E = unknown> {
  state: CommonState<T, E>
  setState: Dispatch<SetStateAction<State<T, E>>>
  setRefresh: (params: StateRefreshOption<T, E>) => void
}

// without initial value
function useCommonState<Value, Error = unknown>(initial?: undefined): StateManager<Value | undefined, Error> {}
// with initial value
function useCommonState<Value, Error = unknown>(initial: Value | (() => Value)): StateManager<Value, Error> {}

Inputs:

  • initial - could be optional, if initial value of CommonState is undefined

Outputs (returned object fields):

  • state - associated CommonState
  • setState - for update state directly (for your custom logic of state behaviour). Not recommended for use without an urgent need
  • setRefresh - set refresh function, on error callback handler and request key for caching requests

Usage example:

function examples(url: string, slug: string): CommonState<ReturnType | undefined> {
  const { state, setRefreshFn } = useCommonState<ReturnType>()

  useEffect(() => {
    // function for get value for CommonState
    const refreshFn = () => getEntityDetails(url, slug)
    // on catch updating error
    const onError = (error)=> console.error('EntityDetails', error)
    // key for memoize request calls
    const requestKey = `${url}_${slug}`
    // set makeRequest as refresh function for state
    setRefreshFn({ refreshFn: makeRequest, requestKey: requestKey })
  }, [url, slug])

  // set refreshing state 
  useEffect(state.hardRefresh, [url, slug])

  return state
}

Utils

useMapCommonState

For mapping CommonState value

const state: CommonState<ReturnType | undefined> = useCommonState<ReturnType>()

//  ...

const mappedState: CommonState<MappedType | undefined> = useMapCommonState(
  state,
  (value: ReturnType | undefined): MappedType | undefined => {
    // ... return mapped value
  },
  [additionalDeps]
)

useCombineCommonStates

For combine few CommonState values in one CommonState in array of values

const state1: CommonState<ReturnType1> = useSomeCommonState1<ReturnType1>(init1)
const state2: CommonState<ReturnType2> = useSomeCommonState2<ReturnType2>(init2)
const state3: CommonState<ReturnType3> = useSomeCommonState3<ReturnType3>(init3)

//  ...

const combinedState: CommonState<[ReturnType1, ReturnType2, ReturnType3]> = useCombineCommonStates(state1, state2, state3)