mint-metrics / mojito-js-delivery

🧪 Source-controlled JS split testing framework for building and launching A/B tests.
https://mojito.mx/docs/js-delivery-intro
Other
16 stars 30 forks source link

Ensure multi observers on the same elements working as expected #53

Closed allmywant closed 4 years ago

allmywant commented 4 years ago

This is an isssue I found when I was building tfes tests. If we have multi observers on the same elements, then only the callback function of the first observer will be fired. Originally, I had this check:

for (var j=0,jc=elements.length;j<jc;j++)
{
    // Skip observed elements
    if (elements[j]._observed)
    {
        continue;
    }

    elements[j]._observed = true;
    listener.fn(elements[j]);
}

Now I fixed it by assign an index to each observer and using it to check if an element is observed:

for (var j=0,jc=elements.length;j<jc;j++)
{
    // Skip observed elements
    if (elements[j]['_observed' + listener.index])
    {
        continue;
    }

    elements[j]['_observed' + listener.index] = true;
    listener.fn(elements[j]);
}
kingo55 commented 4 years ago

Great catch, @allmywant - been checking this out this evening. Will let you know if I have any questions.

kingo55 commented 4 years ago

@dapperdrop - see what you think...