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

small-flux

v1.3.0

Published

Small-Flux is an event based, really small, library for Flux-like development. It has a small footprint, while yet being able to handle most use cases for an application striving to make use of Flux.

Downloads

16

Readme

SmallFlux

Build Status

SmallFlux is an event based, really small, library for Flux-like development. It has a small footprint, while yet being able to handle most use cases for an application striving to make use of the Flux pattern.

Installation

npm install --save-dev small-flux

Usage

var SmallFlux = require('small-flux');

Actions

An Action is simply just a payload being passed around between components and Stores.

var MyAction = SmallFlux.createAction('myAction');

// Trigger the action and notify listeners
// to the action (payload is optional)
MyAction.trigger({ payload: 'payload' });

// or create multiple actions at once;
var MyActions = SmallFlux.createActions(['myActionOne', 'myAcionTwo']);
// access myActionOne:
MyActions.myActionOne.trigger();

Stores

A Store is a central unit which keeps track of Actions, and is a data layer for sharing data between components.

var MyStore = SmallFlux.createStore({

	// `initialize` is automatically invoked,
	// directly when the `Store` is set up
	initialize: function () {

		// Tell SmallFlux that this store should
		// observe the `MyAction` Action.
		this.observe(MyAction, this.handler);

	},

	// Instance method, attached to the observable
	// set up in `initialize`
	handler: function (payload) {

		// Notify any listeners for this `Store`,
		// letting them know something has changed
		this.notify();

	}
});

// Set up a listener for changes in `MyStore`
var MyStoreListener = function () {
	// Something has been changed in `MyStore`!
};
// Attach it;
MyStore.attach(MyStoreListener);
// And later you can detach it, so no changes are reported to `MyStoreListener`
MyStore.detach(MyStoreListener);

Demo

You can clone this repository and run the example files.

Working example

index.html

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>Small-Flux example</title>
	</head>
	<body>
		<section id="application"></section>
		<script src="application.js"></script>
	</body>
</html>

application.js

var React = require('react'),
	SmallFlux = require('small-flux');

var TodoActions = SmallFlux.createActions(['add', 'remove']);

var TodoItemStore = SmallFlux.createStore({
	initialize: function () {
		this.items = [];
		this.observe(TodoActions.add, this.addTodoItem);
		this.observe(TodoActions.remove, this.removeTodoItem);
	},
	addTodoItem: function (todo) {
		this.items.push(todo);
		this.notify();
	},
	removeTodoItem: function (todo) {
		var index = -1;
		for (var i = 0; i < this.items.length; i++) {
			if (this.items[i] == todo) {
				index = i;
				break;
			}
		}

		if (index >= 0) {
			delete this.items[index];
			this.items.splice(index, 1);

			this.notify();
		}
	},
	get: function () {
		return this.items;
	}
});

var TodoItem = React.createClass({
	remove: function () {
		TodoActions.remove.trigger(this.props.todo);
	},
	render: function () {

		return <li>{this.props.todo} <em><span onClick={this.remove}>(remove)</span></em></li>;

	}
});

var TodoList = React.createClass({
	getInitialState: function () {
		return { items: TodoItemStore.get() };
	},
	componentDidMount: function () {
		TodoItemStore.attach(this.update);
	},
	componentWillUnmount: function () {
		TodoItemStore.detach(this.update);
	},
	update: function () {
		this.setState({ items: TodoItemStore.get() });
	},
	render: function () {

		return (
			<ul>
				{this.state.items.map(function (item) {
					return <TodoItem key={item.todo} todo={item} />;
				})}
			</ul>
		);

	}
});

var TodoForm = React.createClass({
	getInitialState: function () {
		return { todo: '' };
	},
	add: function () {
		TodoActions.add.trigger(this.state.todo);
		this.setState({ todo: '' });
		this.refs.todo.getDOMNode().value = '';
	},
	onTodoChange: function () {
		this.setState({ todo: this.refs.todo.getDOMNode().value });
	},
	render: function () {

		return (
			<div>
				<input type="text" ref="todo" placeholder="Todo" onChange={this.onTodoChange} defaultValue={this.state.todo} />
				<button type="button" onClick={this.add}>Add</button>
			</div>
		);
	}
});

var Application = React.createClass({
	getInitialState: function () {
		return { todoCount: TodoItemStore.get().length };
	},
	componentDidMount: function () {
		TodoItemStore.attach(this.update);
	},
	componentWillUnmount: function () {
		TodoItemStore.detach(this.update);
	},
	update: function () {
		this.setState({ todoCount: TodoItemStore.get().length });
	},
	render: function () {

		return (
			<div>
				<h1>Todo List</h1>
				<h5>Current item count is {this.state.todoCount}</h5>
				<TodoList />
				<hr />
				<TodoForm />
			</div>
		);

	}
});

window.onload = function () {
	React.render(<Application />, document.getElementById('application'));
}