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

trocar-class-events

v1.0.0

Published

event container for class

Downloads

2

Readme

README

Trocar class events

npm install trocar-class-events

Usage:

const {eventContainerify, createEvent} =require('trocar-class-events');
class Foo{
    #publish
    constructor(){
        this.#publish = eventContainerify(this);
        //You can internally subscribe
        const subscriptionId = this.subscribe("bar",this.onBar);
        this.detatch =()=>{
            console.log("detach was called");
            this.unsubscribe("bar",subscriptionId);
        }
    }
    onBar(event){
        console.log({name:"onBar Event from Foo",event});
    }
    bar(){
        this.#publish(createEvent("bar", {message:"a bar was called"}));
    }
}

var myFoo = new Foo();
console.log("no subscription yet");
myFoo.bar();
//You can externally subscribe
var id = myFoo.subscribe("bar",(event)=>{
    console.log("external subscription fired...");
});
console.log(`you need the id ${id} for unsubscribing`);
myFoo.bar();
myFoo.unsubscribe("bar",id);
console.log('unsubscribed happened');
myFoo.bar();
myFoo.detatch();
console.log("detaching onBar");
myFoo.bar();

Possible outputs:


no subscription yet
{
  name: 'onBar Event from Foo',
  event: {
    type: 'bar',
    correlation: '1195d1f9-b3f4-466b-8025-5a34df8e2971',
    eventDate: 1622403448779,
    message: 'a bar was called'
  }
}
you need the id fa8c3f59-a3bd-4027-a40f-ebaf3b8a58f5 for unsubscribing
{
  name: 'onBar Event from Foo',
  event: {
    type: 'bar',
    correlation: '102617da-0fc8-4ddd-9bdd-5d9bc8d7d7cb',
    eventDate: 1622403448781,
    message: 'a bar was called'
  }
}
external subscription fired...
unsubscribed happened
{
  name: 'onBar Event from Foo',
  event: {
    type: 'bar',
    correlation: '8649e21b-da5f-4e58-bf7e-d1c10b7a5b4b',
    eventDate: 1622403448782,
    message: 'a bar was called'
  }
}
detach was called
detaching onBar

Functions:

eventContainerify(objectToEventContainerify) returns publish event function.

Why not just attach a publish? then just anyone could call it, by returning it, we place the control back in your hands. In the Foo example we made it a private method.

objectToEventContainerify.subscribe(typeToSubscribe, callback) returns an id objectToEventContainerify.unsubscribe(typeTonUnsubscribeFrom, idToUnsubscribe)

publish(eventToPublish)

createEvent(eventType, dictionaryWithProperties)

takes the dictionaryWithProperties and adds:

  • type
  • correlation
  • eventDate

type is a string, the name of the event type. correlation is a string, serving for a correlationid if you are doing many things you might want to tie the events together. eventDate is the date the event was created.