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

spawn-x-effects

v1.0.2

Published

effects for spawn-x. (Reactive management for javaScript applications)

Downloads

8

Readme

spawn-x-effects

Interceptor (middleware) for spawn-x.

Effects is a small interceptor that enables make cascading state updates, when one action initialize many other actions.

install

With npm:

npm install spawn-x spawn-x-effects --save

With yarn:

yarn add spawn-x spawn-x-effects

With bower:

bower install spawn-x spawn-x-effects --save
<script src="path/to/spawn-x/lib/spawn-x.umd.min.js"></script>
<script src="path/to/spawn-x-effects/lib/spawn-x-effects.umd.min.js"></script>

Usage

app/store/index.js

import { createStore, addInterceptor } from 'spawn-x';
import { effects } from 'spawn-x-effects';

import { logger } from '../interceptors/logger';
import { tracksEffect } from '../effects/tracks';
import { renderEffect } from '../effects/render';

const initialState = {
  tracks: [
    'Puddle Of Mudd - Control',
    'American Hi-Fi - Flavor Of The Weak',
    'SR-71 - What A Mess'
  ]
}

//inject effects interceptor 
const store = createStore(
  initialState,
  addInterceptor(logger, effects)
);

//add effect into effects interceptor
effects.run(tracksEffect);
effects.run(renderEffect);

export {
  store
}

Effect is just a function which accept store and action and then updates state

app/effects/tracks.js

import {
  ADD_TRACK,
  UPDATE_STORE_INIT,
  UPDATE_STORE,
  UPDATE_STORE_COMPLETE,
  NEED_RENDER
} from '../constants';


const tracksEffect = (store, action) => {
  switch (action.type) {
    case ADD_TRACK: {
      store.update('', {
        type: UPDATE_STORE_INIT,
        data: null
      });
      store.update('tracks', {
        type: UPDATE_STORE,
        data: store.select('tracks') ? store.select('tracks').concat(action.data) : [].concat(action.data)
      });
      store.update('', {
        type: UPDATE_STORE_COMPLETE,
        data: null
      });
      store.update('', {
        type: NEED_RENDER,
        data: {
          render: store.select('tracks')
        }
      });
      break;
    }
  }
}

export {
  tracksEffect
}

app/effects/render.js

import {
  NEED_RENDER,
  RENDER_INIT,
  RENDER_COMPLETE
} from '../constants';

import { render } from '../methods/render';


const renderEffect = (store, action) => {
  switch (action.type) {
    case NEED_RENDER: {
      store.update('', {
        type: RENDER_INIT,
        data: null
      });

      render(action.data.render);

      store.update('', {
        type: RENDER_COMPLETE,
        data: null
      });
      break;
    }
  }
}

export {
  renderEffect
}

app/actions/tracks.js

import { store } from '../store';
import { ADD_TRACK } from '../constants';


const addTrack = data => {
  store.update('', {
    type: ADD_TRACK,
    data: data
  });
}

export {
  addTrack
}

And other files...

app/constants/index.js

export const ADD_TRACK = 'ADD_TRACK';
export const UPDATE_STORE_INIT = 'UPDATE_STORE_INIT';
export const UPDATE_STORE = 'UPDATE_STORE';
export const UPDATE_STORE_COMPLETE = 'UPDATE_STORE_COMPLETE';
export const NEED_RENDER = 'NEED_RENDER';
export const RENDER_INIT = 'RENDER_INIT';
export const RENDER_COMPLETE = 'RENDER_COMPLETE';

app/methods/render.js

const render = tracks => {
  const list = document.querySelector('#trackList');

  list.innerHTML = '';

  if (tracks === null) tracks = [];

  tracks.forEach(item => {
    const li = document.createElement('li');

    li.textContent = item;
    list.appendChild(li);
  });
}

export {
  render
}

app/interceptors/logger.js

function logger(store) {
  return next => action => {
    console.log('action: ', action.type + ': ', JSON.parse(JSON.stringify(action.data)));
    next(action);
  }
}

export {
  logger
}

app/index.js

import '../index.html';
import { store } from './store';
import { render } from './methods/render';
import { addTrack } from './actions/tracks';


const btn = document.querySelector('#addTrack');
const input = document.querySelector('#input');

btn.addEventListener('click', () => {
  addTrack(input.value);
  input.value = '';
});

render(store.select('tracks'));

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>App</title>
</head>
<body>
  <input type="text" id="input">
  <button id="addTrack">Add track</button>
  <ul id="trackList"></ul>

  <script src="dist/app.bundle.js"></script>
</body>
</html>

LICENSE

MIT © Alex Plex