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

@etchedjs/dispatcher

v1.0.5

Published

A utility to easily dispatch updates on etched objects, without any state

Downloads

4

Readme

@etchedjs/dispatcher

A utility to easily dispatch updates on @etchedjs/etched objects, without any state

Why a dispatcher?

When we're trying to only manipulate immutable objects to improve the project stability/integrity, we can have to manage some mutable objects (e.g. DOM nodes).

We can also maybe need to create a common channel between them, but it isn't a good idea to store the mutable things statically (aka state), because it breaks the idempotency.

Then the idea is simple: provide a way to subscribe/unsubscribe for updates to some immutable objects that handle the changes to the mutable things.

A concrete example

import { etch, model } from '@etchedjs/etched'
import { dispatcher, subscription } from '@etchedjs/dispatcher'

const incrementer = model(dispatcher, {
  // a set of dispatcher subscriptions
  subscriptions: new Set(),
  decrement (step = -1) {
    if (!Number.isInteger(step)) {
      throw new TypeError('Must be an integer')
    }

    this.dispatch({ step })
  },
  increment (step = 1) {
    if (!Number.isInteger(step)) {
      throw new TypeError('Must be an integer')
    }

    this.dispatch({ step })
  }
})

const incrementerSubscription = model(subscription, {
  // just to know which subscription
  set name (value) {
    if (typeof value !== 'string') {
      throw new TypeError('Must be a string')
    }
  },
  // can be filled by something like `document.querySelector('input')`
  set target (target) {
    if (Object(target) !== target) {
      throw new TypeError('Must be an Object')
    }
  },
  // a key that ca be updated
  set step (value) {
    const { target } = this

    target.value += value
    // show the result
    console.log(JSON.stringify(this))
  }
})

const subscription1 = etch(incrementerSubscription, {
  name: 'subscription1',
  target: { value: 0 }
})

const subscription2 = etch(incrementerSubscription, {
  name: 'subscription2',
  target: { value: 0 }
})

incrementer.subscribe(subscription1)
incrementer.increment()
// {"target":{"value":1},"name":"subscription1"}
incrementer.subscribe(subscription2)
incrementer.increment()
// {"target":{"value":2},"name":"subscription1"}
// {"target":{"value":1},"name":"subscription2"}
incrementer.decrement()
// {"target":{"value":1},"name":"subscription1"}
// {"target":{"value":0},"name":"subscription2"}

Install

npm i @etchedjs/dispatcher

API

dispatcher

An etched model that have the following properties

set subscriptions

A setter to define a Set to register the subscriptions

dispatch(update, subscriptions = this.subscriptions)

A method to dispatch an update to a subscriptions set

subscribe(subscription)

A method to subscribe to the updates

unsubscribe(subscription)

A method to unsubscribe to the updates

subscription

An empty etched model that represents a subscription

Licence

MIT