I'm opening a popup with system.perspective.openPopup
and then trying to populate some values in the popup / do some things by sending a message with system.perspective.sendMessage
.
I'm running into the obvious timing issue that the popup View might not quite be loaded up and ready to receive via the message handlers at the time the calling function broadcasts.
This could be solved by adding a small time delay between the opening of the popup and the sending of the message, with something like time.sleep(0.1)
, but it got me thinking about what an appropriate or minimum time delay would be...
Is there any clever way to have an asynchronous call that lets the calling script know that the openPopup function has been completed and the View is ready to go?
I don't really want to rely on a timer if we don't have to..
If you need to pass values to a popup, the parameters (using the params prop) are usually the best and easiest way to do this:
Messages also work, but as you can appreciate, they introduce added complexity. Timers can also cause issues, unless they are run on a different thread:
If you still wanted to do a message handler, you could have the popup send a message to the view once it has loaded and then page send a message back with the data.
Thanks, yea I did start with sending a parameter through in the openPopup call, but this is for a Trend popup where we wanted to be able to add multiple tag pens to it via some other mechanism.
The parameter was ok for adding an initial pen by sending through a tagPath parameter, but adding additional pens we used the message handler.
I was hoping to simplify the work flow a bit by not using the parameter for the initial call, so that it is consistent.
If it is a popup that multiple views are modifying, you could use store the pens in a custom session prop and both the views and popup can read/write pens?
Ha, I did the same thing recently and ran into the same problem. I don't like my solution.. But it works.. But like you, I wasn't sure how to get around it.
I had the msg handler in the popup which adds the tags to the trend.
I added params to the popup to pass a list of tag paths into. I added an on value change script handler on the tag list to send it to the message handler which then adds the tags to the trend.
The button to open the popup sends the tags via params. Importantly, the openPopup call is discarded automatically when the popup isn't already open (can't reopen a popup with same id), so the params are only ever sent once when the popup is first opened.
The button also sends a message to the message handler to add the tags as well, with no delays or anything.
This works because if the popup isn't open, the params add the trend tags and the message sent from the button doesnt make it to the popup because it's too slow to open. (This was very consistently the case in my testing)
If the popup is already open, the params don't make it into the popup, but this time the message does make it to the popup and the tags are added that way...
It feels gross and it is, but I'm not sure what else to do
Consider sending the message just before opening the Popup with parameters. That will ensure there's no race condition at all.
Haha, I realised that actually after I typed it, I was going to fix it up when on the laptop as it was already a long non-tactile-feedback run
1 Like
Thanks Nick,
Didn't want to start with it, but we've actually done this exact method initially, was trying to see if there was alternative ways
But seems like the best way we've got for now...
1 Like