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

relite

v3.0.7

Published

a redux-like library for managing state with simpler api

Downloads

40

Readme

relite

a redux-like library for managing state with simpler api (1kb)

Why

redux is awesome, but is not enough simple for small and middle application.

With relite, we don't need to combine | apply | bind anything, just write pure function and call actions, it done.

What's new(3.0.0)

  • Supoort Typescript.

  • Delete the support of return type Promise | Function of Action.

Installtion

npm install --save relite

How to use

Write pure function

the action of relite looks like a reducer of redux, but more simple and powerful.

/**
* an action consist of action-type, action-payload, action-handler and action-result
* at this example
* action-type is EXEC_BY
* action-handler is the function accepts two arguments: state and action-payload
* action-result is the result of function
*/

// javascript
export let EXEC_BY = (state, input) => {
	let value = parseFloat(input, 10)
	return isNaN(value) ? state : {
		...state,
		count: state.count + value
	}
}

// typescript
import { Action } from 'relite'

type State = {
  // ...
  count: number
}

export let EXEC_BY: Action<State, number> = (state, input) => {
	return isNaN(value) ? state : {
		...state,
		count: state.count + input
	}
}

Create store by actions and initialState

import { createStore } from 'relite'
import * as actions from './actions'

let initialState = {
	count: 0
}
let store = createStore(actions, intialState)

/*
* relite will bind state for every actions you gave to `createStore`
* so all the functions in store.actions can only accept one argument, action-payload
* no need to bindActionCreators
* each actions return currentState or promise with currentState
*/
let { INCREMENT, EXEC_BY } = store.actions
INCREMENT() // -> { count: 1 }
EXEC_ASYNC(9) // -> Promise[[{ count: 10 }]]

/**
* subscribe store by store.subscribe
* when the state was changed/updateed, relite would trigger the listeners
* if action-handler return the same state, listeners would not be triggered
*/
let unsubscribe = store.subscribe((data) => {
 let {
  actionType, // action-type
  actionPayload, // action-payload
  start, // start date
  end, // end date
  previousState, // prev-state
  currentState // cur-state
 } = data
})

let newState = {
 count: 0,
}
let simulateData = {
 actionType: 'REPLACE_STATE',
 actionPayload: null,
 start: new Date(),
 end: new Date,
 previousState: store.getState(), // get current state
 currentState: newState,
}
let keepSilent = false // if true, it will not trigger listeners

// replace state by store.replaceState
store.replaceState(newState, simulateData, false)

// trigger listener by store.publish
store.publish(simulateData)

store.dispatch('EXEC_ASYNC', 10) // dispatch the action manually

End

Issue and pull request is welcome!