P i
P i

Reputation: 30734

for(x in X) fails, but writing it out in full succeeds

This is vexing. As can be seen from the below code annotation, the clean way fails yet the messy way succeeds.

But the messy way is just surely doing what the clean way does behind the scenes.

But obviously something must be different!

What's going on?

function onSuccess( access ) 
{
    log("requestMIDIAccess -- granted!");

    midiAccess = access; // for persistence over closure

    var inputs = midiAccess.inputs;
    var gotOne = false;

    if (inputs.length === 0)
        log( "No MIDI inputs detected. Maybe reload Chrome THEN connect?" );
    else {   

//      var inputs = midiAccess.inputs.values();      // A WORKS
//      for ( var input = inputs.next(); input && !input.done; input = inputs.next()) 

        for ( input in midiAccess.inputs.values() )   // B FAILS
        {
            input.value.onmidimessage = myMIDIMessagehandler;
            gotOne = true;
        }
        if( ! gotOne ) log( "Need at least one MIDI input" );

    }

EDIT: using for..of and var input now, but still fails.

EDIT: I am using https://github.com/cwilso/WebMIDIAPIShim -- that is where the access object is coming from.

Upvotes: 0

Views: 57

Answers (3)

P i
P i

Reputation: 30734

I needed to change input.value.onmidimessage to input.onmidimessage, and it works!

    for ( var input of midiAccess.inputs.values() ) 
    {
        log( "Input: " + input );
        input.onmidimessage = myMIDIMessagehandler;
        gotOne = true;
    }
    if( ! gotOne ) log( "Need at least one MIDI input" );

Upvotes: 0

Jaromanda X
Jaromanda X

Reputation: 1

The for..in statement iterates over the enumerable properties of an object, in arbitrary order. Probably not what you want

It looks like midiAccess.inputs.values(); is an ES6 iterator? if that is the case, you may be able to use

for (var input of midiAccess.inputs.values())

Upvotes: 1

ale10ander
ale10ander

Reputation: 986

Try for (var input in midiAccess.inputs.values() ).

You have to delcare input.

Upvotes: 0

Related Questions