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

somestate

v0.2.28

Published

Simple state management

Downloads

48

Readme

Somestate

Intuitive and simple platform agnostic state management. Designed to separate logic from the UI.

Install

npm install somestate

Packages

React - https://github.com/Kottakji/somestate-react

Guide

store

Store any variable in the store, so you can listen to changes.

Note: Listeners will never be triggered when the old value and new value are equal

import { store } from 'somestate'

export const $items = store([])

export function addItem(item) {
    $items.set([...$items.get(), item])
}

computed

Create a store based on another store.

import { store, computed } from 'somestate'

export const $items = store([1,2,3])

export const $even = computed($items, items => items.filter(item => item % 2 === 0))

// Or multiple dependencies
export const $other = computed([$items, $even], [items, even] => {
    // Do something
})

fetched

Initiate the store from a fetch request.

Notes:

  • The default value will be {}. This allows for destructuring within the closure.
  • A computed value based on a fetched store will only be called when the fetched store has a value.
import { computed, fetched } from 'somestate'

export const $todos = fetched(`https://jsonplaceholder.typicode.com/todos`)

// Handle any errors
$todos.catch(error => console.log(error.status, error.body))

// The default value will be `{}`. This allows for destructuring
export const $completed = computed($todos, ({completed}) => !!completed)

persistent

Same as the store, but gets the value from localstorage.

import { persistent } from 'somestate'

// Without a default value (will be undefined by default)
export const $store = persistent('mylocalstoragekey')

// With a default value, if there is something in localstorage, it will use that value
export const $withDefaultValue = persistent('mylocalstoragekey', 1)

listen

Listen to changes on any type of store.

import { computed, fetched } from 'somestate'

export const $todos = fetched(`https://jsonplaceholder.typicode.com/todos`)

$todos.listen((todos) => {
    console.log(todos)
})

Key changes

Only listen to key changes on a store.

import { computed, fetched } from 'somestate'

export const $todo = fetched(`https://jsonplaceholder.typicode.com/todos/1`)

// Only update when $item.completed has changed
export const $isCompleted = computed($todo, todo => !!todo?.completed, ['completed'])

// Only listen when $item.completed has changed
$item.listen((item) => {
    console.log(item)
}, ['completed'])

Methods

State methods

import { store } from 'somestate'

export const $id = store();

// Get the latest data
$id.get()

// Set new data, triggering the listeners
$id.set()

Fetched methods

import { fetched } from 'somestate'

export const $todos = fetched(`https://jsonplaceholder.typicode.com/todos`)

// Corresponding to the HTTP request methods
// Will update the data and trigger the listeners
$todos.fetch()
$todos.patch(todos)
$todos.put(todos)
$todos.post(todos)
$todos.delete()

// Handle API request errors (on !response.ok)
$todos.catch(error => console.log(error.status, error.body))

Options

Fetched options

import { computed, fetched } from 'somestate'

export const $todos = fetched(`https://jsonplaceholder.typicode.com/todos`,
    // Fetched options
    {
        headers: {},
    },
    // Additional settings
    {
        // Custom api methods (can be used to set a custom url)
        fetcher: (url, options) => getFetcher(url, 'GET', null, options),
        patcher: (url, body, options) => getFetcher(url, 'PATCH', body, options),
        putter: (url, body, options) => getFetcher(url, 'PUT', body, options),
        poster: (url, body, options) => getFetcher(url, 'POST', body, options),
        deleter: (url, options) => getFetcher(url, 'DELETE', null, options),

        // Custom fetcher methods can also use a single url to only change the url, but keep the fetcher the same
        // patcher: `https://example.com/items`
        // putter: `https://example.com/items`
        // poster: `https://example.com/items`
        // deleter: `https://example.com/items`

        // Catch any api request errors
        catcher: (error) => {},

        // Refetch interval
        refetchInterval: 0,

        // Dependencies - only fetch when the dependencies are not null/undefined/false
        dependencies: [someVariable, $someStore]
    }
)

export const $completed = computed($todos, todos => todos.filter(todo => todo?.completed))

Best practices

Webpack

You can auto load any or all of the store files, so that the stores will be initiated by default. This allows you to separate logic and UI, by not having to import certain code within your UI files.

https://webpack.js.org/concepts/entry-points/

webpack.config.js

const glob = require('glob');

module.exports = {
    //...
    entry: {
        index: [...glob.sync('./src/stores/*.js'), ...['./src/index.js']]
    },
}

Dependency chains

You can then create dependency chains using computed, which will automatically be triggered when a depend store changes.

import { computed } from 'somestate'
import {$flag} from "./flagStore.js"

computed($flag, flag => {
    if (flag) {
        // Do some logic
        // Note that listeners will never be triggered when the old value and new value are equal`
    }
})