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

harris

v0.0.0

Published

A Predictable State Container

Downloads

4

Readme

Harris

A predictable State Container inspired by Redux; in an OOP style.

Installation

import {Store, Update} from 'harris'

// First, we define our model. In our case,
// a simple number is fine.
type Model = number

// Next, we define a few actions. Here is
// an action for incrementing the model.
class IncrementAction {
  // Note that the type of the `type` field
  // is a string literal. This is important,
  // so remember it.
  readonly type: 'Increment' = 'Increment'
}

// Here we have a corresponding decrement
// action. Note also that these actions are
// classes, and can contain context data
// which can be conveniently injected in
// the constructor.
class DecrementAction {
  readonly type: 'Decrement' = 'Decrement'
}

// We declare a union of all the different
// action types for future reference.
type Action = IncrementAction | DecrementAction

// We define the initial state of our app.
const initialState: Model = 0

// Here is an Update. Updates describe the
// transitions from one state to another,
// given an action object.
const update: Update<Model, Action> =
  (state: Model, action: Action): Model => {
    // Here, because all actions have a
    // literal `type` field, the compiler
    // knows that `action.type` here has
    // the type `'Increment' | 'Decrement'`.
    switch (action.type) {
      case 'Increment':
        // The TypeScript compiler is smart
        // enough to understand that since
        // `action.type` apparently is
        // `'Increment'`, `action` must be
        // an `IncrementAction`.
        return state + 1

      case 'Decrement':
        return state - 1
    }
  }

// Using an initial state and an update function
// we can create a `Store`, which is a mutable
// object containing the state of the app.
const store = new Store(initialState, update)

// We can access the current state like so
store.state // === 0

// The only way to mutate a `Store` is by dispatching
// an `Action`. Like this:
store.dispatch(new IncrementAction())

// The Store then uses the `Update` function to
// figure out the next state.
store.state // === 1

// We can get notified of updates automatically by
// subscribing to the store:
store.subscribe(state => {
  console.log('State updated:', state)
})

First Class Tweed Support

If you're using Tweed, the Store class will automatically update the UI when a command is dispatched:

import {Store, Update} from 'harris'
import {Node} from 'tweed'
import render from 'tweed/render/dom'

class AddAction {
  readonly type: 'Add' = 'Add'

  constructor (
    public readonly value: number
  ) {}
}

type Model = number
type Action = AddAction | ...

const update: Update<Model, Action> =
  (state: Model, action: Action): Model => {
    switch (action.type) {
      case 'Add':
        return state + action.value
      default:
        return state
    }
  }

class Root {
  readonly store = new Store<Model, Action>(0, update)

  render () {
    const onClick = () => this.store.dispatch(new AddAction(1))

    return (
      <button on-click={onClick}>
        Clicked {this.store.state} times
      </button>
    )
  }
}

render(new Root(), document.querySelector('#root'))