How to handle the 'Stop' button event when 'plugin is working...'?

Sorry if this is a stupid question, but how do we handle the event when the ‘Stop’ button is pressed on the ‘Plugin is working’ dialog?

For example I have an async function and after Stop is pressed it throws the error:

Plugin 2fd73edc is not permitted to make changes from the background. Return a Promise to continue execution asynchronously.

And that is at this line selectedShapes.items[i].fill = fill, I assume because the edit context somehow is lost?

1 Like

The Stop button is a pretty hard stop here; once invoked, your plugin is effectively terminated. In this way it’s a lot like the user pressing ESC in a dialog – you can’t prevent the closure.

IIRC, the actual process involves a rejection of the promise at the highest level, so I don’t think there’s much your plugin can do at this point to save itself. You might be able to do a finally step to clean up, though.

One option is to handle progress in your own dialog – I do this in the e2e-adobe-stock sample. The harder part is how to handle the user need to stop the plugin if it’s downloading a lot of assets, but now that we have setTimeout it should be fairly easy to schedule work in batches and listen for user input appropriately.

Thanks Kerri. The download in batches will do the job I think. I will modify the code and post the result here.

Hi @kerrishotts, sorry for bothering you again, but I couldn’t figure this out.

It seems as after the STOP is pressed, the plugin continues to work in the background and throws an “…is not permitted to make changes from the background” at each for loop i.e. executing the ‘await’ function. Here’s the async function that needs to be stopped:

async function generateImages(selection) {
    for (var i = 0; i < selection.items.length; i++) {
		await downloadImage(selection.items[i], photoUrl);
	}
}

Either I don’t understand something or the STOP button (although closes the modal) it doesn’t stop the async function and the loop inside.

I assume the loop goes through, without waiting, executes the ‘downloadImage’ function each time, but after STOP is pressed we get to see results of each ‘downloadImage’ execution, but then the edit context is lost and there’s the error.

Sample code in context: tpdne-test.zip (41.8 KB)

Async actions like downloads won’t stop – that’s one of the downsides here. They’ll continue to completion, and then, as you see, trigger an edit context if a change is made to the document.

So although the plugin appears to be terminated from the user’s perspective, it’s still active from your perspective. Apologies for not being as clear on this in my prior comment; this is something we’re coming to terms with internally as well!

If you want to have full control over the entire process, you’ll need your own dialog with its own Stop mechanism.

1 Like

I understand, thanks Kerri. That’s why my initial question was if there was a way to listen for the ‘STOP’ button event, so I could put a if(!stop) statement in the for loop. I’ll see what I can do and send an update.