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

ampel

v0.1.0

Published

Ampel.js - library for signals === (this library is WIP and changes are not published to npm. Check out later for the npm release).

Downloads

69

Readme

Ampel.js - library for signals

(this library is WIP and changes are not published to npm. Check out later for the npm release).

building blocks:

  • Signal - represents single value changing in time. It can be used as reactive primitive (something similar to observable).
  • Listener - allows for listening event targets (e.g. DOM elements). It changes events into signals
  • ProxyState - wraps object and exposes the reactive proxy. Thus changes can be traced.

JS concepts Ampel.js supports:

  • async/await - you can await signals
  • asynchronous generators - functions that can use await but also can emit values. This can be used for constructing powerful helpers. Ampel.js provides helpers for asynchronous generators (A.map, A.filter, A.pairs)

Look into examples folder to check for usage.

What's nice about this library?

You can await signals (which can be constructed from e.g. DOM events). This allows for more straightforward implementation of complex interactions. Programmers often write such interactions using boolean flags or hand-made state machines. But this can be written in less convoluted way. Just write what you have in mind and await things you wait for (e.g. mouse events):

import * as A from 'ampel';

const whenFirst = A.Listener.fromEventTarget(document.getElementById('first')).on;
const whenSecond = A.Listener.fromEventTarget(document.getElementById('second')).on;

async function main() {
	while (true) {
		alert("click first");
		await whenFirst('click');
		alert("click second");
		await whenSecond('click');
	}
}
main();

Signal

Signal represents single value changing in time. It can be used as reactive primitive (something similar to observable).

You can:

  • set and get values (.set and .get methods)
  • subscribe via .subscribe method
  • await signal
  • create signal from promise (static method .fromPromise)
  • cancel signal via .cancel cancelled signal can not worked be set. Additionally it would reject subscribers set via .then or await (so awaited signal would throw)

Listener

Listener allows for listening event targets (e.g. DOM elements). It changes events into signals. And it keeps and provides multiple signals (differentiated by string e.g. 'click'). These strings are changed into signals. And also customizable function listen is called (you can pass it in the constructor) which should subscribe to relevant event.

example:


async function main() {
	const foo = new A.Listener((eventType, s) => {
		document.getElementById('foo').addEventListener(eventType, (v) => {
			s.set(v);
		});
	});
	
	await foo.on('click');
}
main();

It's usually not needed to manually create Listener like above because Listener has static method fromEventTarget which does exactly that (i.e. allows for listening to DOM elements or other objects that implement addEventListener method).

example:

async function main() {
	const foo = A.Listener.fromEventTarget(document.getElementById('foo'));
	await foo.on('click');
	alert('you clicked #foo');
}
main();