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

tiny-sangtae

v0.0.2

Published

A state management library written in TypeScript. It can be used with Vanilla JS and React.

Downloads

3

Readme

Tiny Sangtae

Tiny Sangtae is a state management library written in TypeScript. It can be used with Vanilla JS and React.

'Sangtae' means 'state' in Korean.

Install

npm install tiny-sangtae

Usage

Sangtae

sangtae can be used to store string, number, array and any type.

import { sangtae } from 'tiny-sangtae';

const $counter = sangtae(0);

get can get the saved state.

console.log($counter.get()); // 0

set can set the saved state.

$counter.set(1);
console.log($counter.get()); // 1

A function can be passed to set, and the current state is provided as an argument.

$counter.set(5);
$counter.set((v) => v + 5);
console.log($counter.get()); // 10

subscribe allows you to register a callback that will be called when the state changes.

$counter.subscribe(() => console.log(`$counter: ${$counter.get()}`));
$counter.set(1); // $counter: 1

The callback is called as many times as the set is invoked.

$counter.subscribe(() => console.log(`$counter: ${$counter.get()}`));
$counter.set(1); // $counter: 1
$counter.set(2); // $counter: 2
$counter.set(3); // $counter: 3

If the function unsubscribe returned by subscribe is called, the callback will no longer be invoked.

const unsubscribe = $counter.subscribe(() => console.log(`$counter: ${$counter.get()}`));
$counter.set(1); // $counter: 1

unsubscribe();
$counter.set(2);
$counter.set(3);

Computed

computed can create derived state from sangtae.

import { computed, sangtae } from 'tiny-sangtae';

const $lastName = sangtae('Lee');
const $fullName = computed($lastName, ln => ln + ' Hyanggi');
console.log($fullName.get()); // Lee Hyanggi

computed can also create derived state from computed.

import { computed, sangtae } from 'tiny-sangtae';

const $lastName = sangtae('Lee');
const $fullName = computed($lastName, ln => ln + ' Hyanggi');
const $info = computed($fullName, fn => ({ name: fn, age: 20 }));
console.log($info.get()); // { name: Lee Hyanggi, age: 20 }

computed can also create derived state from multiple sangtae or computed.

import { computed, sangtae } from 'tiny-sangtae';

const $lastName = sangtae('Lee');
const $firstName = sangtae('Hyanggi');
const $fullName = computed([$lastName, $firstName], (ln, fn) => `${ln} ${fn}`);
console.log($fullName.get()); // Lee Hyanggi

When the original sangtae changes, the computed state also changes.

$lastName.set('Kim');
console.log($fullName.get()); // Kim Hyanggi

Just like sangtae, you can call subscribe on computed.

const unsubscribe = $fullName.subscribe(() => console.log(fullName));
$lastName.set('Park'); // Park Hyanggi;

unsubscribe();
$lastName.set('Choi');

Action

If set is called consecutively in action, the callback is called only once at the end.

$counter.subscribe(() => console.log(`$counter: ${$counter.get()}`));
action(() => {
  $counter.set(1);
  $counter.set(2);
  $counter.set(3);
  $counter.set(4); // $counter: 4
});

If an asynchronous task is called within action, subsequent tasks are not included in the action.

import { resolve } from 'path';

$counter.subscribe(() => console.log(`$counter: ${$counter.get()}`));
action(async () => {
  $counter.set(1);
  $counter.set(2);

  await new Promise(resolve => setTimeout(resolve, 0));

  $counter.set(3);
  $counter.set(4);
});

This code will first print $counter: 2 to the console, and after 1 second, it will print$counter: 3 and $counter: 4.

Integration

React

@tiny-sangtae/react provides useSangtae hook.

This hook takes either a sangtae or computed as an argument and returns the state.

// counter.ts
import { sangtae, computed } from 'tiny-sangtae';

export const $counter = sangtae(0);

export const $counterAdded10 = computed($counter, v => v + 10);

export const increase = sangtae.set(v => v + 1);
export const decrease = sangtae.set(v => v - 1);

// Counter.tsx
import { useSangtae } from '@tiny-sangtae/react';
import { $counter, $counterAdded10, increase, decrease } from './counter';

export default function Counter() {
  const counter = useStore($counter);
  const counterAdded10 = useStore($counterAdded10);

  return (
    <div>
      <h1>{counter} + 10 = {counterAdded10}</h1>
      <button type="button" onClick={increase}>+</button>
      <button type="button" onClick={decrease}>-</button>
    </div>
  );
}