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

resynchronize

v2.0.1

Published

Redux utils to manage async functions and promises

Downloads

20

Readme

RESYNCHRONIZE

npm (scoped)

Simple library/bunch of functionalities/bunch of notes on files to fetch/promise async payloads and put a meaninful state for your appplication, either way if is redux or react hooks.

This tries to follow the standards put on place by the creators of redux, but it's still opinionated, feel free to add your own pretty features!

Install

Copy and run this in your project folder

npm i --save resynchronize

Create actions

Create your async actions, the returned object will contain different actions inside that can be used to set the steps of your async functions.

// my-actions.js
import { createAsyncActions } from 'resynchronize'
const getList = createAsyncActions('GEL_LIST')

Dispatch the actions on the right moment

Use your created actions to set the right status on the right moment of your requests

Basic example

// my-actions.js
// Could be redux, or could be something else as far as you can get the state from it
import store from './my-store.js'

const myGetList = (dispatch) => {
  dispatch(getList.start()) // Loading starts
  fetch('/api/list')
    .then(data => {
      dispatch(getList.done(data)) // The async action is done
    })
    .catch(ex => {
      dispatch(getList.error(ex)) // The async action failed
    })

}

Other example

Anything that you have to wait for it can be expressed with the async actions

// my-actions.js
// Could be redux, or could be something else as far as you can get the state from it
import store from './my-store.js'

const myGetList = (dispatch) => {
  dispatch(getList.start()) // Loading starts
  setTimeOut(() => {
    dispatch(getList.done('the wait is over!')) // The async action is done
  }, 1000)
}

Create reducers

All this actions will trigger state changes thru a reducer, you can always use the action object to tackle the different moments yourself, but to simplify it even more you can use the creator function.

// my-reducers.js
import { createAsyncReducer } from 'resynchronize'
import { getList } from './my-actions.js'

// Simple implementation
const getListReducer = createAsyncReducer(
  [],
  getList
)

Combinable with other actions

const getListReducer = createAsyncReducer(
  [],
  { getList }
)

Custom reducers for the different parts, we will go there eventually dont worry

const getListReducer = createAsyncReducer(
  [],
  {
    [getList]: {
      done: (currentPayload, action) => action.payload || currrentPayload
    }
  }
)

Get payload and check status

With the actions being reduced, the next step is using this async state on your code. You can always use it raw, the shape of the state will be consistent if you use the creators, but again to simplify our logic and controls you can use the getters and be completely sure that no matter what internal changes could happen on the internals, it will be reliable.

Quick checks

Simple and quick way to obtain the async state, it will contain the status object with all the status on boolean shape, the payload (as you set it on your dispatchs) and the error (same as payload)

// my-reducers.js
import { getAsyncProps } from 'resynchronize'
// Could be redux, or could be something else as far as you can get the state from it
import store from './my-store.js'

const state = store.getState()
const list = getAsyncProps(state.list)
/*
list = {
  status: {
    done: false,
    loading: false,
    cancelled: false,
    error: false
  },
  payload: [],
  error: null
}
*/

Individual checks

In case you don't need all the object, just basic checks, you can use the individual getters.

// my-reducers.js
import { isDone, isLoading, isCancelled, hasError, getError, getPayload } from 'resynchronize'
// Could be redux, or could be something else as far as you can get the state from it
import store from './my-store.js'

const state = store.getState()
const done = isDone(state.list)
const loading = isLoading(state.list)
const cancelled = isCancelled(state.list)
const someError = hasError(state.list)
const theErrorObject = getError(state.list)
const payload = getPayload(state.list)

Connect?

Now, we need to use this to render something right? check the examples