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

s-is-for-store

v3.2.0

Published

A no frills state container written in TypeScript.

Downloads

2

Readme

S is for Store

Build Status

S is for Store is a state container for JavaScript applications. It offers straightforward state management with the following features:

  • Simple architecture. Setup projects without having to rely on a large amount of boilerplate.
  • TypeScript support is available out of the box.

Installation

Install via npm:

npm install s-is-for-store

Or via yarn:

yarn add s-is-for-store

Basic Example

Note: The code for this example is written in TypeScript. However, S is for Store works just fine with plain JavaScript. If you aren't using TypeScript, a JavaScript example is available in this package's repository at examples/basic-implementation.js.

import { createStore } from 's-is-for-store'

// define the store interface
interface State { message: string, count: number }

// create the store
const store = createStore<State>({ message: '', count: 0 })

// expose the update  and current functions
const { current, update } = store

// define the updater functions
const setMessage = (message: string) => update({ message })
const incrementCount = (by: number) => {
  const state = current()

  return update({ count: state.count + by })
}

// create a listener
const listener = (state: State) => console.log(state)

// subscribe to the store
store.subscribe(listener)

// run the update functions

setMessage('Hello World.')
// Outputs { message: 'Hello World', count: 0 }

setMessage('Hello Again.')
// Outputs { message: 'Hello Again', count: 0 }

incrementCount(4)
// Outputs { message: 'Hello Again', count: 4 }

incrementCount(2)
// Outputs { message: 'Hello Again', count: 6 }

Core Concepts

When using S is for Store you'll be performing one of the following operations:

  • Creating a store.
  • Updating a store.
  • Subscribing to a store.
  • Unsubscribing from a store.

Creating a Store

First, import the createStore function:

import { createStore } from 's-is-for-store'

If you are using TypeScript, define the interface for your store. In the above example that store implements this interface:

interface State { message: string, count: number }

Create a new store with the createStore function. The createStore function takes the store's initial state as a parameter.

const store = createStore<State>({ message: '', count: 0 })

Updating a Store

Updating state is accomplished by calling the store's update function. The update function is passed an object that contains the properties of state that will be updated.

In the above example calls to update are wrapped in an updater functions:

// define the update functions
const setMessage = (message: string) => update({ message })
const incrementCount = (by: number) => {
  const state = current()

  return update({ count: state.count + by })
}

Handeling changes to a store.

Whenever the state updates, any listener functions that are subscribed to the store get called with the new state.

In the original code example, the listener function simply logs the new state to the console:

const listener = (state: State) => console.log(state)

For this listener function to get called, it needs to be subscribed to the store:

store.subscribe(listener)

Unsubscribing

When the subscribe function gets called, it returns an unsubscribe function:

const unsubscribe = store.subscribe(listener)

Calling the unsubscribe function will unsubscribe the listener. It will no longer get called when the state updates.

unsubscribe()
// lister is no longer subscribed

Calling the store's unsubscribeAll function will unsubscribe all listeners.

Usage With React

There is a React hook that's available for S is for Store. It can be installed via npm:

npm install @s-is-for-store/react

Or via yarn:

yarn add @s-is-for-store/react

Using the hook is very straightforward:

import React from 'react'
import exampleStore from './wherever/your/store/is/defined/'
import { useStore } from '@s-is-for-store/react'

const ExampleComponent: React.FC = () => {
  const store = useStore(exampleStore)

  return <div>
    <h1>This component uses the s is for store hook and will rerender whenever the store is updated.</h1>
    <p>{store.someValueToDisplay}</p>
  </div>
}

More resources

A simple example app is available at https://github.com/heathgr/s-is-for-store-example.git. It deomonstrates how an S is for Store app can be used with React and tested.