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

@halo-lab/store

v0.0.1

Published

Ministore for applications

Downloads

1

Readme

@halo/store

Every application need to handle a cache (also known as state).

Prerequisites

For using this package you should have NodeJS version 12.22.0 and above (that suports ES modules natively) or transpile the package.

Installing

npm i @halo/store

Using

The package exports the store function for creating reactive data container.

function store<S extends object>(value: S): Store<S>;

It accepts an object whose values are treated as chunks of the cache. To get a data from Store object reach a property that are pointed to the needed value.

const cache = store({ url: '...' });

const url: string = cache.url;

For changing a value of some property inside the store you should assign a new value to it.

// A value inside the cache store will be changed.
cache.url = 'https://new.url';

Changing the value inside the store is reactive. You can listen to this changes through on method of the Store.

// Create subscription.
const onUrlChange = cache.on('url');
// Listen to the url property changes.
onUrlChange((url) => {
	/* Do something */
});

on method accepts a list of property names which you want to be dependent on. It returns a function that allows to register a listener that will be invoked when those properties change.

const cache = store({ a: 1, b: '', c: false });

const onABChange = cache.on('a', 'b');

onABChange((a, b) => {
	/* ... */
});

Latter returns the unsubscribe function that stops listening to changes.

const onUrlChange = cache.on('url');

// ...

const unsubscribe = onUrlChange((url) => {
	/* Do something */
});

// Somewhere in the code...

unsubscribe(); // You do not listen to _url_ changes now.

When you delete a property, then listener will be invoked with undefined value:

delete cache.url;

Integrations

For easy using store with UI libraries there is a Pointer interface that helps interact with any value in the store.

For now, there are integraitons for React and edelweiss. Each of them exports a createPointer function that creates Pointer instance. It allows to get exact value from the store no matter how deep it is.

You can create many pointers for one store, but usually it isn't necessary to do so.

React

For creating pointer you should import createPointer function from @halo/store/react submodule.

import { store } from '@halo/store';
import { createPointer } from '@halo/store/react';

const appStore = store({ keyFromStore: { inner: '' } });

const useStoreValue = createPointer(appStore);

With pointer you can create reactive container that is depend on a value from the store.

const [value, setValue] = useStoreValue('keyFromStore');

By default, you can get and update the value of keyFromStore property in the store. But you can customize what it should return and update by providing getter and setter arguments.

const [value, setInnerValue] = useStoreValue(
	({ keyFromStore }) => keyFromStore.inner,
	// You should return new state in setter to trigger update
	// in places where reactive container is used.
	(currentState, newValue) => ({
		...currentState,
		keyFromStore: { ...currentState.keyFromStore, inner: newValue },
	}),
);

This is useful for create dependency on two or more store values. Also, getter and setter should be pure functions and do not mutate original store.

Edelweiss

The same createPointer function exist in @halo/store/edelweiss submodule. But instead of returning a tuple it returns a Data function.

import { store } from '@halo/store';
import { createPointer } from '@halo/store/edelweiss';

const appStore = store({ key: { inner: 'foo' } });

const appStorePointer = createPointer(store);

const keyValue = createPointer('key');

And the same getter and setter parameters are available here.

const innerValue = useStoreValue(
	({ key }) => key.inner,
	// You should return new state in setter to trigger update
	// in places where reactive container is used.
	(currentState, newValue) => ({
		...currentState,
		key: { ...currentState.key, inner: newValue },
	}),
);
`

Word from author

Have fun ✌️