jovotech / jovo-framework

🔈 The React for Voice and Chat: Build Apps for Alexa, Messenger, Instagram, the Web, and more
https://www.jovo.tech
Apache License 2.0
1.67k stars 310 forks source link

:bug: Fix merge order for arrays in mergeInstances #1613

Closed sadlowskij closed 5 months ago

sadlowskij commented 6 months ago

Proposed Changes

Taking this code snippet in a handler:

  await this.$send({
      platforms: {
        alexa: {
          nativeResponse: {
            response: {
              directives: [
                {
                  type: 'Alexa.Presentation.APL.ExecuteCommands',
                  token: this.$session.id,
                  commands: [
                    {
                      type: 'SetPage',
                      componentId: 'rootPager',
                      value: 1,
                    },
                  ],
                },
              ],
            },
          },
        },
      },
    });
    await this.$send({
      platforms: {
        alexa: {
          nativeResponse: {
            response: {
              directives: [
                {
                  type: 'Alexa.Presentation.APL.ExecuteCommands',
                  token: this.$session.id,
                  commands: [
                    {
                      type: 'SetPage',
                      componentId: 'rootPager',
                      value: 2,
                    },
                  ],
                },
              ],
            },
          },
        },
      },
    });

Currently leads to the directive with value 2 to be placed first in the resulting alexa response:

    [
            {
                "type": "Alexa.Presentation.APL.ExecuteCommands",
                "token": "amzn1.echo-api.session.6c0b46c5-6db8-4f48-a71b-feb268d3b51c",
                "commands": [
                    {
                        "type": "SetPage",
                        "componentId": "rootPager",
                        "value": 2
                    }
                ]
            },
            {
                "type": "Alexa.Presentation.APL.ExecuteCommands",
                "token": "amzn1.echo-api.session.6c0b46c5-6db8-4f48-a71b-feb268d3b51c",
                "commands": [
                    {
                        "type": "SetPage",
                        "componentId": "rootPager",
                        "value": 1
                    }
                ]
            }
        ]

The order is very important for some combination of directives, and to me there is no logical reason to reverse the order here. Feel free to let me know, if this was not implemented this way by accident, but for some specific reason, where putting array elements of the following output to the front. Maybe instead of changing the mergeInstances function, one could also just switch the argument order in SingleResponseOutputTemplateConverterStrategy.

Types of Changes

Checklist

sadlowskij commented 6 months ago

Oh, looking at the failed action, I think I'd have to look into this more, sorry!