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

dispatch-message-to-listeners

v2.0.2

Published

To dispatch events to listeners on recived event, and listeners if is message is new

Downloads

10

Readme

DispatchMessage2Listeners

  • Author: Renato Miawaki

When you need to dispatch events to methods (listeners) and filter if message is equal.

For socket stream you can use to separate messages and distribute just for clients who want to get the message

For use in stream


const DispatchMessage2Listeners = require('./index') ;

const NmeaBuffer = require( 'nmea-buffer' ) ;
const Stream2Event = require( 'socketstream2event' ) ;

//Example 1, parsing NMea messages
const n1 = new DispatchMessage2Listeners() ;

let s2e = new Stream2Event( 10 ) ;
s2e.addOnData((data)=>{
    let nmeaMessage = NmeaBuffer.parse(data) ;
    n1.dispatchToListeners( nmeaMessage.header, nmeaMessage ) ;
})

n1.showLog = true ;
n1.addListener("a", (d)=>{
    console.log("\n\n*1 recived A in n1", d.message) ;
} ) ;

n1.addListener("B", (d)=>{
    console.log("\n\n*2 recived B in n1", d.message) ;
} ) ;
n1.addListenerAll((d)=>{
    console.log("\n\n*3 recived becouse listener all message in n1", d.message) ;
} ) ;
n1.addListenerAllOnChange((d)=>{
    console.log("\n\n*4 recived listen all if CHANGE in n1", d.message) ;
} ) ;
let data = [ ... NmeaBuffer.getMessageBuffer("a", "just for A, test 1"), ...NmeaBuffer.getMessageBuffer("a", "just for A test 2"), ...NmeaBuffer.getMessageBuffer("B", "just for Bs"), ...NmeaBuffer.getMessageBuffer("c", "just for c"), ...NmeaBuffer.getMessageBuffer("c", "just for c") ];

s2e.parseData(data) ;

console.log("\n End test n1. \n ................................. \n\n") ;

//Exemple 2 - parsing using json with method and body attributes
const n2 = new DispatchMessage2Listeners() ;
s2e.addOnData((data)=>{
    let nmeaMessage = NmeaBuffer.parse(data) ;
    n2.dispatchToListeners( nmeaMessage.header, data ) ;
})
n2.addListener("*",  (d)=>{
    console.log("\n\n*recived * in n2", NmeaBuffer.parse(d)) ;
} )

n2.addListenerOnChange("B",  (d)=>{
    console.log("\n\n*recived B in n2", NmeaBuffer.parse(d)) ;
} )
const Buffer        = require('buffer').Buffer ;

data = [ ... Buffer.from('{"method":"a", "body":"testing 111"}\n'), ... Buffer.from('{"method":"B", "body":"testing 111"}\n') ];

s2e.parseData(data) ;

//if you want to force send message to listeners:

n2.dispatchToListeners("B", "message here")
n2.dispatchToListeners("B", "message here")
n2.dispatchToListeners("B", "message here")

console.log("\n End test n2. \n ................................. \n\n") ;



const n3 = new DispatchMessage2Listeners() ;
var objGroup = {__id:23234234}

n3.addListener("B",  (d)=>{
    console.log("recived B again in n3", d) ;
}, objGroup ) ;

n3.addListenerOnChange("B",  (d)=>{
    console.log("recived B once in n3", d) ;
}, objGroup ) ;

n3.dispatchToListeners("B", "another message again") ;
n3.dispatchToListeners("B", "another message again") ;
n3.dispatchToListeners("B", "another message again") ;
n3.removeListenersByGroup( objGroup ) ;
//just 3 first message neet to recive response
n3.dispatchToListeners("B", "another message again") ;
n3.dispatchToListeners("B", "another message again") ;
n3.dispatchToListeners("B", "another message again") ;

console.log("\n End test n3. \n ................................. \n\n") ;

console.time("n4")
const n4 = new DispatchMessage2Listeners() ;
//when you send object to listeners and need change way to compare:
n4.xcompare = (n1,n2)=>{
    if(!n1 && !n2){
        return true ;
    }
    if(! n1 || !n2 ){
        return false ;
    }
    return (n1.m == n2.m)
}
n4.addListener("C",  (d)=>{
    console.log("recived C again in n4", d) ;
}, objGroup ) ;

n4.addListenerOnChange("C",  (d)=>{
    console.log("recived C once in n4", d) ;
}, objGroup ) ;

for( var i = 0; i < 4; i++){
    n4.dispatchToListeners("C", {m:"another message again"}) ;
}


console.timeEnd("n4")
console.log("\n End test n4. \n ................................. \n\n") ;