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

k-signals

v1.0.0

Published

Js signals implementation, make your state reactive

Downloads

7

Readme

K Signals

Signals are about universal state management, they are performant and will notify only actual dependencies. No need in creating your own specific state manager.

Installation

Use package manager to install the library

npm i k-signals # npm
bun a k-signals # bun
yarn add k-signals # yarn

Table of content

Docs

signal<T>(initValue: T)

import { signal } from 'k-signal';

const a = signal('a');
console.log(a.value); // a

a.value = 'aa';
console.log(a.value); // aa

Creates a signal with init value.

signal.peek()

const a = signal('a');
effect(() => console.log(a.peek())); // prints a

a.value = 'aa'; // prints nothing

Peek method used to get signal value without subscribing to it.

computed<T>(cb: () => T): T

import { signal, computed } from 'k-signal';

const a = signal(1);
const doubled = computed(() => a.value * 2);

console.log(doubled.value); // 2

a.value = 2;
console.log(doubled.value); // 4

Computes new value based on the other value. Updates computed value every time dependencies change.

effect(() => void)

import { signal, effect } from 'k-signal';

const a = signal(1);
const eff = effect(() => console.log(a.value)); // prints 1

a.value = 2; // prints 2

eff.isActive = false;
a.value = 3; // prints nothing

Immediately run an effect, reruns on dependencies change. Can be turned off by setting isActivated to false.

effect.dispose()

const eff = effect(() => {});

eff.onDispose = () => {
  console.log(`Disposed`);
};

eff.dispose(); // prints disposed

Dispose method will unsubscribe dependencies and deactivate effect.

batch(cb: () => void)

import { signal, computed, batch, effect } from 'k-signal';

const a = signal('a');
const b = signal('b');

const comp = computed(() => a.value + b.value);

// prints ab
effect(() => {
  console.log(comp.value);
});

// prints aabb
batch(() => {
  a.value = 'aa';
  b.value = 'bb';
});

Batch function will postpone all effects execution until batch complete. Use batch to avoid getting transient values.

watch(cb: () => void, deps: Signal[])

import { signal, watch } from 'k-signal';

const a = signal('a');
const b = signal('b');

watch(() => { console.log(a.value); console.log(b.value) }, [a]);

a.value = 'aa'; // prints aa, b
b.value = 'bb'; // prints nothing

The watch function is not called immediately like effect function does and also listens only provided dependencies.