# How to handle cancel during long editDocument()?

**URL:** <https://forums.creativeclouddeveloper.com/t/how-to-handle-cancel-during-long-editdocument/1914>\
**Category:** UXP Plugin API\
**Tags:** api-feedback\
**Created:** [July 20, 2020, 2:03pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-handle-cancel-during-long-editdocument/1914 "2020-07-20T14:03:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpryland](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/cpryland/32/1172_2.png) [@cpryland](https://forums.creativeclouddeveloper.com/u/cpryland)\
**Post date:** [July 20, 2020, 2:03pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-handle-cancel-during-long-editdocument/1914/1 "2020-07-20T14:03:24Z")

</div>

So when a long `editDocument()` is proceeding, and the user clicks the `Stop` button, what happens exactly, and how could we detect this instead of just crashing?

All I see in my plugin is the edits made to the scenegraph start failing out of the blue with exceptions.

This seems like a very poor UX.

So how could a plugin detect a cancellation and do something intelligent?

---

<div class="post-metadata">

**Author:** ![Yevheniy](https://avatars.discourse-cdn.com/v4/letter/y/ea666f/32.png) [@Yevheniy](https://forums.creativeclouddeveloper.com/u/Yevheniy)\
**Post date:** [May 29, 2021, 2:21pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-handle-cancel-during-long-editdocument/1914/2 "2021-05-29T14:21:59Z")

</div>

Hi there!  
Any updates about this? I also have the same problem and did not found anything in official documentation about this. Nothing about how to handle this or detect inside editDocument callback that user canceled the execution.  
I would be thankful for any advice from you, AdobeXD team.  
Thanks.

---

<div class="post-metadata">

**Author:** ![mightyalex](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/mightyalex/32/777_2.png) [@mightyalex](https://forums.creativeclouddeveloper.com/u/mightyalex)\
**Post date:** [May 29, 2021, 9:13pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-handle-cancel-during-long-editdocument/1914/3 "2021-05-29T21:13:40Z")

</div>

See if this maybe helps [How to handle the 'Stop' button event when 'plugin is working...'? - #6 by mightyalex](http://forums.creativeclouddeveloper.com/t/how-to-handle-the-stop-button-event-when-plugin-is-working/682/6).

---

<div class="post-metadata">

**Author:** ![Yevheniy](https://avatars.discourse-cdn.com/v4/letter/y/ea666f/32.png) [@Yevheniy](https://forums.creativeclouddeveloper.com/u/Yevheniy)\
**Post date:** [May 31, 2021, 8:17am UTC](https://forums.creativeclouddeveloper.com/t/how-to-handle-cancel-during-long-editdocument/1914/4 "2021-05-31T08:17:52Z")

</div>

@mightyalex Hi there. Yep it looks like the only way is to implement your own dialog which is a pity. It would be much better to have either static boolean flag available somewhere to check if user did an Undo operation or at least an event on which we can subscribe and implement our own flag. But anyway thanks for help.

---

<div class="post-metadata">

**Author:** ![cpryland](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/cpryland/32/1172_2.png) [@cpryland](https://forums.creativeclouddeveloper.com/u/cpryland)\
**Post date:** [May 31, 2021, 3:06pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-handle-cancel-during-long-editdocument/1914/5 "2021-05-31T15:06:04Z")

</div>

Unfortunately, for a panel-based plugin, there’s no way to provide our own “stop” mechanism – the user is clicking the one in the “working” dialog that XD puts up.

Any ideas, @kerrishotts?

---

<div class="post-metadata">

**Author:** ![kerrishotts](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/kerrishotts/32/8_2.png) [@kerrishotts](https://forums.creativeclouddeveloper.com/u/kerrishotts)\
**Post date:** [June 1, 2021, 1:30pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-handle-cancel-during-long-editdocument/1914/6 "2021-06-01T13:30:13Z")

</div>

If XD’s default “working” dialog is insufficient, the only other option is to show your own. The benefit here is that you can now control the experience, including giving the user better information about what might be going on. For example, if your plugin is download a lot of images, you could include that information in the dialog.

Dialogs can be created from panels – and you can use a `Promise` to hold the `editDocument` session open until the dialog is dismissed (either automatically or by the user).

Be sure when you use your own dialog that you appropriately batch your long-running computations or other blocking work. Otherwise the UI remains clickable, but your plugin won’t have a chance to respond. `setTimeout` or `setInterval` are your friends here.

---

<div class="post-metadata">

**Author:** ![Velara](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/velara/32/1278_2.png) [@Velara](https://forums.creativeclouddeveloper.com/u/Velara)\
**Post date:** [June 2, 2021, 3:37pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-handle-cancel-during-long-editdocument/1914/7 "2021-06-02T15:37:16Z")

</div>

I had to redo part of my plugin because of a long process (see Vectorize plugin vectorizing the document). It wasn’t as difficult as I thought.

I used a pseudo sleep() method to give my loop time to breath and time to allow user interaction. Without user interaction the UI would lock up until the loop was finished.

Because editDocument modifications have to occur within a click event you can’t do some actions. So sleep() won’t work for everything. You would have to present a dialog and add a cancel button to it.

> [@XD using 28 gigs of ram](http://forums.creativeclouddeveloper.com/t/xd-using-28-gigs-of-ram/1497/4):
>
> It sounds like an infinite loop and unreleased memory. Does UXP have a script timeout limit? I think the browsers show a prompt if a script runs over a certain amount of time. I was able to break up large loops by using promises to allow the user to cancel the operation if it’s taking too long. In the [log utilities](http://forums.creativeclouddeveloper.com/t/statistics-plugin-and-plugin-developer-utilities/621/5) you can use a pseudo [sleep](https://github.com/velara3/xd-examples/blob/c205c1d5c1238b5db33486443f97c45bddcd4fa7/statistics/log.js#L949) function: async function myProcessorIntensiveFunction(data) { let commands = require("commands"); var items = []; // do stuff for(var index=0; ind…

Maybe UXP could dispatch a stop event or have a stop callback method for when the automatic dialog appears? Then, @cpryland can handle for that bc sleep won’t work inside editDocument panel event.
