Jump to content

Filtering Events

TheNuzziNuzz

I have an event emitter that gets events like "{type: 1, value=127}." I want a way to dynamically handle each "type" separately. Currently I just add a new listener with an `if (msg.type == 1) {}` but that seems clunky and unnecessary? Any suggestions?

Computers r fun

Link to comment
Share on other sites

Link to post
Share on other sites

If you have a ton of if-statement, you might as well make a switch statement out of it.

A switch is (long story short) a whole bunch of "if value == x"-statements in a row. It also has a 'default' option, for any input that doesn't fall within the other conditions. In this default statement you can add a message 'do something with this, programmer!', or rather save that to later see in a log what kind of input needs to be picked up..

 

If you can dynamically do something with this data though, depends on the data that is flowed into the method.

For example, if you have a method that just returns double of the input parameter, you can do:

int DoubleTheInput(int input)
{
	if(input == 1)
    	return 2;
    if(input == 2)
    	return 4;
    // Etc..
}

Or you can simply do:

int DoubleTheInput(int input)
{
	return (input*2)
}

 

That is of course a stupid simple example, but if you are catching numbers and returning numbers, you could try to figure out a pattern in your code...

Or perhaps a recursive loop that counts stuff up is more fit for your work? Just depends on what you need to do.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

@minibois

 

Basically am creating a MIDI Deice macro program. I have a device that sends messages like {channel: 0, controller: 27, value: 50} & {sysex: [0x13, 0x20, 0x05]} etc. They come through an event emitter "message"
I want the user to be able to add a new macro, listening for specific midi messages and output their value to the rest of my program. I don't think you can do that with switch.
 
I think i'm more concerned with the "best" way to do this rather than how to do it. I feel like having a million IF statments inside a ForEach is slow

Computers r fun

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, TheNuzziNuzz said:

@minibois

 

Basically am creating a MIDI Deice macro program. I have a device that sends messages like {channel: 0, controller: 27, value: 50} & {sysex: [0x13, 0x20, 0x05]} etc. They come through an event emitter "message"
I want the user to be able to add a new macro, listening for specific midi messages and output their value to the rest of my program. I don't think you can do that with switch.
 
I think i'm more concerned with the "best" way to do this rather than how to do it. I feel like having a million IF statments inside a ForEach is slow

You could create a Dictionary or other sort of 2D list (KeyValuePair for example) where you have the Key and Value (basically what triggers it and what the output will be) and then loop through that list once your users triggers it.

That way this list can be expanded or shrunk depending on user input even.

 

If you want to make it as easy as possible for the system to loop through this, you could create some sort of algorithm for it. You could make it so the list is always sorted in a good way (e.g. alphabetically) so you start halfway this list and just check if you need to go up or down depending on the value you're looking at.

 

That way you don't have to search each end of the list if the value you're looking for is at the other end of the list.

But of course using such an algorithm will not be quite useful if your list is a bit smaller; so it will depend on your use.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

In this situation I have an object with the keys as the types you want to use and its value is a function to call. On my phone juat now but tomorrow at work I'll drop an example if no one gets to it before then.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

@minibois @vorticalbox

 

Okay so here is my latest idea. Because I don't quite know what the messages im receiving are going to look like (They might have more than type such as type, channel, controller, etc in addition a value property), I came up with a little bit weird of an idea. 

 

Basically take the msg json, seperate the value from the rest of the message. Stringify, and try to find that string as a key in the filters object. Because of my specific use case, the msg's properties will always be in the same order, so to filter for simply add the string to the object. Thoughts?

 

Also for some explanation here, each filter is an EventEmitter with a name that gets called with the value as an argument when say, a button is pressed. Then I can listen for specific filtered events.


class InputDevice extends events {
    constructor(EventListener, filters) {
        super()
        try {
            filters ? this.filters = filters : this.filters = {}
            this.emit('filters', this.filters)
            if (EventListener.in) { EventListener = EventListener.in }
            this.EventListener = EventListener.on('message', function (msg) {
                const val = msg.value
                delete msg.value
                const string = JSON.Stringify(msg)
                if (this.filters[string]) {
                    this.filters[string].emit('data', val)
                } else {
                    this.emit('raw', [msg, ...val])
                }
            })
        } catch {
            throw new Error('Err')
        }
    }
    AddFilter(msg, name) {
        this.filter[msg] = { name: name, ...new events.EventEmitter() }
        this.emit('filters', this.filters)
    }
    RemoveFilter(msg) {
        this.filter[msg].emit('delete')
        delete this.filter[msg]
        this.emit('filters', this.filters)
    }
}

 

Computers r fun

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×