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

@janghye0k/observable

v1.0.2

Published

A small, fast state-management solution using observer pattern.

Downloads

3

Readme

Build Size Version

A small, fast state-management solution using observer pattern. This library allows you to create observables, subscribe to them, and publish changes to them. It also supports computed observables,

You can try a live demo here.

Installation

npm install @janghye0k/observable # or yarn add @janghye0k/observable or pnpm add @janghye0k/observable

Usage

Create observable state

import { observable } from '@janghye0k/observable';

const ov = observable('init');
const listener = function (state, prevState) {};
ov.subscribe(listener);

ov(); // 'init'
ov('init'); // 'init', no change
ov('changed'); // fn('changed', 'init')
ov.value = 'silent'; // silent update
ov.publish(); // fn('silent', 'init');

Subscribe to the observable state

const ov = observable(1);
const listener = function (state, prevState) {};
ov.subscribe(listener);

ov(13); // listener(13, 1)
ov(0); // listener(0, 13)

Unsubscribe exist listener

const ov = observable('init');

Modifying arrays and objects

Modifying arrays and objects will not publish, but replacing them will.

const ov = observable([]);
const listener = function (state, prevState) {};
ov.subscribe(listener);

ov([1]); // listener([1], [])
ov.value.push(2);
ov().push(3);
ov.pusblish(); // listener([1, 2, 3], [])

Computed value

Passing a function caches the result as the value. Any extra arguments will be passed to the function. Any observables called within the function will be subscribed to, and updates to those observables will recompute the value. Child observables must be called, mere references are ignored. If the function returns a Promise, the value is assigned async after resolution.

const a = observable(1); // a() == 1
const b = observable(() => a() + 1); // b() == 2
const c = observable((arg) => a() + b() + arg, 3); // c() == 6

a(3);
console.log(a(), b(), c()); // 3, 4, 10
function sleep(timeout) {
  new Promise((resolve) => setTimeout(resolve, timeout));
}

const ov = observable(async () => {
  await sleep(1000);
  return 1;
});

console.log(ov()); // null;
await sleep(1000);
console.log(ov()); // 1

License

MIT