Issue with an event from a javascript widget (Tradingview) where only first added event is triggered when created on a loop

I have the following issue with a javascript chart widget Im using. I think my issue is more to do with my logic than with a problem with the chart library itself. I have already asked this on their GitHub repo, but again I believe this is just me not doing this properly.

This javascript widget provides events that I can act upon. This is a javascript library from TradingView (chart library), it has a function to create a line on the chart. This function is called .createOrderLine(). When you create an orderLine, it returns an object that you can manipulate (set price, events, etc)

This object has an event called onMove(), with a callback function that I can write code to. So what I am doing is, looping through an array of orders and creating a line on the chart for each order that exists. So every time I move a line on the chart, this callback is called.

When the chart is loaded initially, I add each line object to a Map by setting its key to the orderID and the value to the object itself. If I can’t find the orderID on the map, I will set it to the map.

The issue Im having that I can not figure out is, every time I move ANY of the lines created, only the code from the first order added is triggered, ie: the console.log(order.OrderID) always returns the id of the first line created, no matter which line I move. So in essence, it seems the onMove() from the first order is the only one that runs.

Here is some simplified version of my logic / code:


var objectMapOrders = new Map();

for(var order of currentSelectedMarketOrderIds){

    var apiOrderObject = objectMapOrders.get(parseInt(order.OrderID))
    if(!apiOrderObject){
    
        var apiOrderObject = widget.activeChart()
        .createOrderLine()
        .setPrice(order.EntryPrice)
        
        objectMapOrders.set(parseInt(order.OrderID), apiOrderObject);
    
    
    }else{
    
        apiOrderObject
        .setPrice(order.EntryPrice)
        .onMove(async function() {
            //Call function that updates the order price
            console.log(order.OrderID)
            updateOrder(order.OrderID,this.price())
    
        })
    
    }

}

Not sure if Im using the Map correctly or if there’s an alternative way that I can make this work?

I have tried all sorts of changes around (setting the onMove on inside each if block) to no avail