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

@sruth/core

v1.1.0

Published

A library for reactive state management using observables.

Downloads

1

Readme

Sruth Core

A reactive state management library. It uses RxJS to create observables, so you'll need it installed too.

npm i @sruth/core rxjs

API

Store

subscribe :: (patch, factory) -> Subscription

The main function that gets everything going. It creates an observable using the factory function and subscribes to it using the patch function.

Usually you'll want to pass a function that receives the current state and updates the DOM as the patch.

import { interval } from 'rxjs'
import { subscribe } from '@sruth/core'

const patch = state => vdom(state)
const factory = () => interval(3_000)

subscribe(patch, factory)

factory receives an observable that you can use if you need to know the current state at some point in the observable stream.

import { interval, map, withLatestFrom } from 'rxjs'
import { subscribe } from '@sruth/core'

const patch = state => vdom(state)
const factory = stream =>
	interval(3_000)
		.pipe(
			withLatestFrom(stream),
			map(([_, prev]) =>
				prev % 2 === 0
					? prev + 1
					: prev * 2
			)
		)

subscribe(patch, factory)

It can also be useful to run unrelated effects every time the state changes.

subscribe(
	patch,
	stream => {
		stream.subscribe(console.log)
		return observable$
	}
)

Reducers

Sruth exposes a couple of reducer functions to facilitate merging multiple observables into one state. They are there for your convenience, but you can also just create your own if they don't suit you.

change :: initState -> Operator

Returns an operator that expects an object describing state changes and, optionally, filters.

Each change is a function that receives the current state and returns a new state value or an observable. If multiple change functions are passed, they will all be merged and then a new state will be emitted.

import { reducers } from '@sruth/core'

const initState = { count: 0 }
const increaseCount = value => state => ({ count: state.count + value })

const factory = () => interval(1_000)
	.pipe(map(value => ({ changes: [ increaseCount(value) ]})))

If filters are passed, it will first run all the filters against the current state. If any filter fails, it'll stop. If all pass, it'll apply the changes to state and emit a new state.

You can also emit multiple state changes from the same set of changes if you pass them in nested arrays. This is useful if you need multiple changes triggered depending on the same filter, for example, when making an http request and setting a loader.

const load = () => ({
	changes: [
		[ setStatus('loading') ],
		[ fetchUser, setStatus('idle') ]
	],
	filters: [ isNotLoading ]
})

The above set of changes will trigger one state change first and set the status to loading, and another one later when it has finished fetching the user. But if the status is already loading, it will skip both changes.

For more examples on this reducer, you can check the joke example.

merge :: initState -> Operator

Returns an object that expects a partial state replacement and merges it with the previous state. It optionally takes an initial state.

import { listen, reducers } from '@sruth/core'

const factory = () => {
	const name$ = listen('name-change')
		.pipe(map(name => ({ name })))
	const age$ = listen('age-change')
		.pipe(map(age => ({ age })))

	return merge(name$, age$)
		.pipe(
			startsWith({ name: 'empty', age: 0 }),
			reducers.merge()
		)
}