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

autoreact

v0.0.20

Published

Automatic state/view dependency management

Downloads

9

Readme

autoreact

A library that infers UI state dependencies and automatically updates views.

Example: Chat App

Note the lack of any setState() or forceUpdate(). Yet, it works.

var autoreact = require('../src/autoreact') // normally: require('autoreact')
var ReactDOM = require('react-dom')
var React = require('react')
var _ = require('lodash')

// State
////////

var State = autoreact.createState({
	username: String,
	currentRoomIndex: Number,
	rooms: Array({
		name: String,
		lastMessage: String,
		messages: Array({
			from: String,
			text: String,
			time: Number
		})
	})
})

var store = {
	setUsername: function(username) {
		State.username = username
	},
	addRoom: function(name) {
		var newRoom = { lastMessage:null, messages:[], name:name }
		State.rooms.push(newRoom)
	},
	addMessage: function(text) {
		var newMessage = { from: State.username, text: text, time: new Date().getTime() }
		State.rooms[State.currentRoomIndex].messages.push(newMessage)
	},
	selectRoom: function(roomIndex) {
		State.currentRoomIndex = roomIndex
	}
}

State.rooms = []
store.addRoom("#General")
store.addRoom("#CatGifs")
store.addRoom("#Random")

// UI
/////

class AppView extends autoreact.Component {
	componentWillMount() {
		super.componentWillMount()
		window.gApp = this
	}
	render() {
		return <div>
			<div>{ this.renderControls() }</div>
			<div>username: { State.username || '(none)' }</div>
			<RoomListView/>
			<ChatView/>
		</div>
	}
	renderControls() {
		return <div>Controls:
			<button onClick={this.addRoom}>Add Room</button>
			<button onClick={this.setUsername}>Set username</button>
			<input id='usernameInput' />
		</div>
	}
	addRoom() {
		store.addRoom("Another Room "+new Date().getTime())
	}
	setUsername() {
		store.setUsername(document.getElementById('usernameInput').value)
	}
}

class RoomListView extends autoreact.Component {
	render() {
		return <div>
			{_.map(State.rooms, function(Room, roomIndex) {
				return <RoomView key={Room.name} roomIndex={roomIndex} />
			})}
		</div>
	}
}

class RoomView extends autoreact.Component {
	render() {
		var Room = State.rooms[this.props.roomIndex]
		var isCurrent = (this.props.roomIndex == State.currentRoomIndex)
		return <div onClick={this.selectRoom.bind(this)}>
			{Room.name}
			{Room.messages.length ? ' ('+Room.messages.length+')' : ''}
			{isCurrent ? ' (current)' : ''} 
		</div>
	}
	selectRoom() {
		store.selectRoom(this.props.roomIndex)
	}
}

class ChatView extends autoreact.Component {
	render() {
		var Room = State.rooms[State.currentRoomIndex]
		if (!Room) {
			return <div>No room selected</div>
		}
		return <div>
			{_.map(Room.messages, (message) =>
				<div key={message.time}>
					{message.from}: {message.text}
				</div>
			)}
			<input id='messageInput' onKeyPress={this.onKeyPress} />
		</div>
	}
	onKeyPress(event) {
		if (event.key != 'Enter') { return }
		event.preventDefault()
		var input = document.getElementById('messageInput')
		store.addMessage(input.value.trim())
		input.value = ''
	}
}

ReactDOM.render(<AppView />, document.body.appendChild(document.createElement('div')))