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

stas-other-immutable

v0.3.1

Published

A state management with promise middleware, immutable data and model normalization

Downloads

1

Readme

A state management with promise middleware, immutable data and model normalization

RECOMMEND: Use stas with simple and seamless immutability support!

Features

  • Express-like and promise-based middlewares and routers, see Middleware and Router
  • Immutable state and operations, see Immutable State
  • Normalize the data by model

Example

// store.js
import { Store, Model } from 'stas-other-immutable';
import router from './router';

const models = [
  ['User', {
    posts: 'Post',
  }],
  ['Post', {
    author: 'User',
  }],
];

const store = new Store({ users: [] }, { models });
store.use(router);

module.exports = store;

// router.js
import createRouter from 'uni-router';
const router = createRouter();

router.all('/users/query', async (req, resp, next) => {
  const { store } = req,
    { User } = store.models,
    { offset, limit } = req.body;

  const result = await fetch('/api/users/query', { offset, limit });

  store.mutate((newState) => {
    const ids = User.merge(result);
    newState.set('users', ids);
  });
});

router.all('/users/:userId/posts/create', async (req, resp, next) => {
  const { store } = req,
    { User, Post } = store.models,
    { userId } = req.params,
    { title } = req.body;

  const result = await fetch(`/api/users/${userId}/posts/create`, { title });

  store.mutate((newState) => {
    const postId = Post.merge(result);
    const user = User.get(userId).set('posts', posts => posts.push(postId));
    store.User.set(userId, user);
  });
});

module.exports = router;

// UserListPage.js
import { PureComponent, PropTypes } from 'react';
import { connect } from 'react-stas';

class UserListPage extends PureComponent {
  static propTypes = {
    users: PropTypes.array.isRequired,
    dispatch: PropTypes.func.isRequired,
  }

  render() {
    const { users } = this.props;
    return (<ul>
      {users.map(user => <li>
        <span>{user.name}</span>
        <span>{user.posts.map(post => post.title).join(',')}</span>
      </li>)}
    </ul>);
  }
}

module.exports = connect(({ state, dispatch, props, store }) => {
  const { User, Post } = store.models;
  const userIds = state.get('users').toJSON();
  const users = User.mget(userIds).map((user) => {
    user = user.toJSON();
    user.posts = user.posts.map(postId => Post.get(postId).toJSON());
    return user;
  });
  return { users };
})(UserListPage);

// index.js
import ReactDom from 'react-dom';
import { Provider } from 'react-stas';
import store from './store';
import UserListPage from './UserListPage';

ReactDom.render(
  <Provider store={store}>
    <UserListPage />
  </Provider>,
  document.getElementById('app'),
);

Installation

yarn add stas react-stas uni-router -S

or if your'd like to use npm:

npm install stas react-stas uni-router -S

Middleware

store.use((req, resp, next)=>{})

Router

Like express router but with promise support. For detail see uni-router

router.use(pattern?: string, middleare)

Prefix match req.url with pattern

router.all(pattern?: string, middleare)

Exact match req.url with pattern

Immutable State

Use List, Map and Model to manipulate the state. For detail see immutable-state.

store.mutate(callback)

Start a new mutation operation.

.get(keysPath: array|string)

Get the value on the specific keys path.

.set(keysPath: array|string, value: json|function)

Set the value on the specific keys path. If passed function, the function must return the result value.

.toJSON() or .toJS()

Convert to plain json object

.keys()

Return the keys

.length or .size

Return the keys length

.filter()/.find()/.findKey()/.forEach()/.map()/.reduce()/

Like array corresponding methods, most with (value, key, thisArg)=>{} signature.

.remove(keyPath) or .delete(keyPath)

Remove the leaf key

.merge(strategy?: bool|function, input)

Merge the value with specific strategy.

.slice()/.findIndex()/.push()/.pop()/.unshift()/.shift()

Like array corresponding methods. List only.

Hot Reload(HMR)

Since react-native doesn't support module.hot.accept(path, callback) but module.hot.accept(callback), we have to use a function to export the store, then replace store's middlewares(routers) by utilizing closure.

import Store from 'stas';
import routers from './routers';

export default function createStore() {
  const store = new Store()
  store.use(routers);

  if (module.hot) {
    module.hot.accept(() => {
      const newRouters = require('./routers').default;
      store.clearMiddlewares();
      store.use(newRouters);
    });
  }
  return store;
}

Contributing

Checkout the CONTRIBUTING.md if you want to help

License

Licensed under MIT

Copyright (c) 2017 Tian Jian