Multiple Sync-UDElement

Product: PowerShell Universal
Version: 3.3.6

I am trying to run several Sync-UDElement at the same time.
Each of them fetches some data from API (so they are slow). They all display one table from each API.
I run them all directly after each other:

New-UDButton -OnClick {
    Sync-UDElement -Id "Dyn1"
    Sync-UDElement -Id "Dyn2"
    Sync-UDElement -Id "Dyn3"
    Sync-UDElement -Id "Dyn4"
}
New-UDDynamic -Id "Dyn1" -Content {
    $Dyn1Data = Get-DataFromAPI1
    New-UDTable -Data $Dyn1Data
}
New-UDDynamic -Id "Dyn2" -Content {
    $Dyn2Data = Get-DataFromAPI2
    New-UDTable -Data $Dyn2Data
}
New-UDDynamic -Id "Dyn3" -Content {
    $Dyn3Data = Get-DataFromAPI3
    New-UDTable -Data $Dyn3Data
}
New-UDDynamic -Id "Dyn4" -Content {
    $Dyn4Data = Get-DataFromAPI4
    New-UDTable -Data $Dyn4Data
}

I was hoping they would then all start loading at the same time, and load (one by one) when done.
This doesn’t seem to be case, it seems that as soon as the “next” Sync-UDElement is run, the previous one is cancelled.
Every click of the button generates different amount of data in the table, and more often than not, empty data.

Is this a bug of Sync-UDElement meant to work like this, or am I doing something wrong?

So, your example has multiple errors, not sure if they may be present in your production code…

First, do you mean Do you mean Sync-UDElement? Or did you create another function called Sync-UDDynamics?

Second, all the variables in your example are Dyn1Data instead of $Dyn1Data. Also they are all looking at the same data and trying to write to the same variable…

Sorry, i always mix up Sync-UDElement and Sync-UDDynamic.
The post was written just to display the issue, it is not the actual code.
I have corrected my post.

Got it… What happens if you do Sync-UDElement -Id @("Dyn1","Dyn2","Dyn3","Dyn4")

Same issue, they all load, but the result is random. Every button click gives back different data.
Only way i have gotten it to work so far is to do the Sync-UDElement one after each other, but this slows it down significally.

So; things I would try next…

  1. Use the $Session: scope. So $Session:Dyn1Data.
  2. Instead of using New-UDTable -Data use… New-UDTable -LoadData. This makes the Table itself dynamic, and doesn’t rely on wrapping it in a New-UDDynamic.

Also some more information on what you are trying to accomplish will probably be helpful with figuring out options. Does the Data need to be refreshed on click, or is this a manual process because that’s what you know how to do? There is the option to use a UDEndpoint and updating a $Cache: scope variable, which all users would be able to use.

I’ll try to explain a little more, the biggest issue is that my code is much too big to post here (around 1000 lines).
I am trying to do a search page, for users in our AD. This information should then be displayed in 4 UDCards, one for each category of information, for example “Mail”, “General”.
I have created 4 functions, that are loaded in at the start of the dashboard, to check and manipulate the data, and return a table with this information.

I then run the Sync-UDElement, one for each of the UDCards, where the functions are called with a variable that has the user information stored.
Running all the Sync-UDElement at the same time (as my example) gives completely random output. Different tables are display, with different data each time I press the button.
The only solution i have found so far is that they call the next one when done:

New-UDButton -OnClick {
    Sync-UDElement -Id "Dyn1"
}
New-UDDynamic -Id "Dyn1" -Content {
    $Dyn1Data = Get-DataFromAPI1
    New-UDTable -Data $Dyn1Data
    Sync-UDElement -Id "Dyn2"
}
New-UDDynamic -Id "Dyn2" -Content {
    $Dyn2Data = Get-DataFromAPI2
    New-UDTable -Data $Dyn2Data
    Sync-UDElement -Id "Dyn3"
}
New-UDDynamic -Id "Dyn3" -Content {
    $Dyn3Data = Get-DataFromAPI3
    New-UDTable -Data $Dyn3Data
    Sync-UDElement -Id "Dyn4"
}
New-UDDynamic -Id "Dyn4" -Content {
    $Dyn4Data = Get-DataFromAPI4
    New-UDTable -Data $Dyn4Data
}

But this also makes it slow, as the whole for these 4 UDCards was/is that the data is loaded in independently of each other.

I have tested with “New-UDTable -LoadData”, no difference, still random output.

Okay, first try putting the variables in the $Session: scope.

Now, some more questions.

You say you are displaying AD information separated out into cards… Where do the long running API calls come in?

Does data get manipulated on this page and actually changed in AD/any other system?

Can you provide a screenshot of the dashboard?

My current feelings on this is you may have some unneeded complexity in the script as well. I’m not sure if there are limits on posting code (long code blocks go into a scroll window). But having that will be useful as well.

On Monday I will be back at work.
I will post some of the code, and if possible, a video of the issue.

Basically I do the same but I use a UDSelect with ONChange which syncronizes several UDDyns. But I use as already mentioned for the data a variable $session:DashData = @{} with $session:DashData.Api1 or $session:DashData.Api2 or $session:DashData.somethink. All UDDyns sync more or less at the same time without any data missing or anything wrong.

Sometimes the data is already updated in the UDSelect via the API and sometimes the Update happens in the UDDyn area. Which sometimes, according to my observation, makes a difference the first time visit the site.

Just as info.

I have uploaded my code to justpast.it.
Here is the main page:
https://justpaste.it/74dbq

Here are 2 of the functions:
https://justpaste.it/5qugt
https://justpaste.it/2apze

And here is a video of it:
Animation

I dont spend much time on the code but why use more than one Dynamic if all are updated by one click?

Secound is: try if it is different if you put the tablecolumn definition in the uddyn content area. Sometimes it makes a different… i think :wink:

Because the 4 UDCards take different time to load, if i put them in one the slowest one sets the loading time for all 4.

Thanks for the tip, ill give it a test!