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

@jrc03c/emitter

v0.0.4

Published

helps enforce a "props go down, events go up" OOP pattern

Downloads

1

Readme

Intro

npm install --save @jrc03c/emitter

The Emitter class defined here is useful for enforcing a "props go down, events go up" OOP pattern. One can start by listening for events, for example:

const Emitter = require("@jrc03c/emitter")
const a = new Emitter()
const b = new Emitter()

a.on(b, "foo", () => {
  console.log("b says 'foo'!")
})

b.emit("foo")
// b says 'foo'!

But this base class only handles event emission; it doesn't define any particular properties (except a subscriptions property by which it keeps track of event handlers). So, properties must be defined in a subclass, like this:

class Person extends Emitter {
  name = "Anonymous"
}

Then, to be reactive to prop changes, one can use setters:

class Person extends Emitter {
  _name = "Anonymous"

  get name() {
    return this._name
  }

  set name(newName) {
    this._name = newName
    console.log("Hey! My new name is:", newName)
  }
}

Justification

So, I made this class to help encourage a more "polite" pattern of object interaction in OOP contexts. I know that OOP has its flaws and that it'd probably be better to abandon it altogether, but short of that, I hope this pattern will make my OOP code more organized. I learned the "props go down, events go up" pattern from Vue, though I can't find that exact phrase in their docs any more. In any case, the point is that objects can no longer directly call each other's methods, which is what (in my code) accounts for a lot of confusion as it becomes increasingly unclear who has what responsibilities. Instead, this pattern forces me to truly treat objects as encapsulated code. At most, then, one object can set another's property values, and it's up to the other object to determine when and how to react to that prop change; and an object can listen for events emitted from another object. As I said, this establishes a more "polite" protocol for interaction among objects.

There are probably better ways of accomplishing the same thing in OOP, and I'm sure this is already a well-known problem with a well-known solution. But it's the solution that works well in Vue contexts, so I'm going with it!