# Event listeners crash InDesign when rebuilding a UXP plugin

**URL:** <https://forums.creativeclouddeveloper.com/t/event-listeners-crash-indesign-when-rebuilding-a-uxp-plugin/7535>\
**Category:** InDesign\
**Tags:** bug, uxp\
**Created:** [February 15, 2024, 2:37pm UTC](https://forums.creativeclouddeveloper.com/t/event-listeners-crash-indesign-when-rebuilding-a-uxp-plugin/7535 "2024-02-15T14:37:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedro](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/pedro/32/3524_2.png) [@pedro](https://forums.creativeclouddeveloper.com/u/pedro)\
**Post date:** [February 15, 2024, 2:37pm UTC](https://forums.creativeclouddeveloper.com/t/event-listeners-crash-indesign-when-rebuilding-a-uxp-plugin/7535/1 "2024-02-15T14:37:48Z")

</div>

Reproduction steps:

- Get the [react-starter](https://github.com/AdobeDocs/uxp-indesign-samples/tree/main/plugins/react-starter) sample project for InDesign;
- Add an event listener when the `Home` component is rendered;

```auto
import React, { useEffect } from "react";
import { app } from "indesign";

import { WC } from "./WC.jsx";

import "./Home.css";

export const Home = () => {
  useEffect(() => {
    app.addEventListener("afterSelectionChanged", function (event) {
      console.log("Selection changed");
    });
  }, []);

  return (
    <div>
      <WC>
        <p className="display">
          Congratulations! You just created your first React Plugin.
        </p>
      </WC>
    </div>
  );
};

```

- Run the `watch` script and load and watch the plugin into InDesign using the UXP dev tools;
- Force a plugin rebuild;
- InDesign crashes when the plugin is reloaded.

It would be nice to get this sorted out. This won’t be an issue when the plugin is deployed but it’s annoying to developers.

---

<div class="post-metadata">

**Author:** ![pklaschka](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/pklaschka/32/3162_2.png) [@pklaschka](https://forums.creativeclouddeveloper.com/u/pklaschka)\
**Post date:** [February 16, 2024, 8:24pm UTC](https://forums.creativeclouddeveloper.com/t/event-listeners-crash-indesign-when-rebuilding-a-uxp-plugin/7535/2 "2024-02-16T20:24:54Z")

</div>

I can confirm this. I’m not quite sure about “won’t be an issue when the plugin is deployed”, though. It won’t consistently break the plugin once deployed, but I’ve had several weird crashes that weren’t reproducible but seemed to correlate with using InDesign event listeners. I suspect this also played a role in

> [@Operating system specific problems with InDesign APIs?](http://forums.creativeclouddeveloper.com/t/operating-system-specific-problems-with-indesign-apis/6650):
>
> My plugin just got rejected in the review process. Now that, by itself, isn’t why I’m posting here. The reason I’m writing about this is that it alludes to some sort of operating system dependency in some InDesign API that (in my opinion) can only be a bug. My plugin (the code of which you can find at [https://github.com/pklaschka/indesign-pride-flagger/blob/main/index.cjs](https://github.com/pklaschka/indesign-pride-flagger/blob/main/index.cjs)) was rejected for this reason: When attempting to […] [run the plugin], a critical problem arises. Unfortunately, this res…

I can confirm that it consistently happens when rebuilding the plugin, though.

I’m currently replicating this kind of functionality (in all of my InDesign-based projects) to re-evaluating any dynamic state on a `setInterval()` loop. Far from ideal, but the only way I found that doesn’t seem to increase InDesign crashes (or at least: not significantly enough to confirm it during my tests).

Overall (and this isn’t just limited to events), InDesign seems to become unstable when more complex event loop interactions take place between UXP and InDesign APIs. This also, for example, affects interactions between `app.doScript()` and promises / async operations. Definitely something Adobe needs to fix if they want UXP to be a viable option for broader InDesign extensibility.

---

<div class="post-metadata">

**Author:** ![pedro](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/pedro/32/3524_2.png) [@pedro](https://forums.creativeclouddeveloper.com/u/pedro)\
**Post date:** [February 21, 2024, 3:38pm UTC](https://forums.creativeclouddeveloper.com/t/event-listeners-crash-indesign-when-rebuilding-a-uxp-plugin/7535/3 "2024-02-21T15:38:10Z")

</div>

This is what I ended up doing to avoid these crashes using event listeners.

```auto
export const Home = () => {
  const handleSelectionChanged = () => {
    console.log("Selection changed");
  };

  useEffect(() => {
    const eventListener = app.eventListeners.item("myEventName");
    if (eventListener.isValid) {
      // app.removeEventListener("afterSelectionChanged", handleSelectionChanged); // this will crash InDesign
      eventListener.remove();
    }
    const action = app.addEventListener(
      "afterSelectionChanged",
      handleSelectionChanged
    );
    if (action.isValid) action.name = "myEventName";
  }, []);

  return (
    ...
  );
};

```

It would be nice if someone from the InDesign team could explain what is the difference between using `app.removeEventListener` and `eventListener.remove`.

---

<div class="post-metadata">

**Author:** ![dspice](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/dspice/32/3668_2.png) [@dspice](https://forums.creativeclouddeveloper.com/u/dspice)\
**Post date:** [February 24, 2024, 1:40pm UTC](https://forums.creativeclouddeveloper.com/t/event-listeners-crash-indesign-when-rebuilding-a-uxp-plugin/7535/4 "2024-02-24T13:40:28Z")

</div>

> It would be nice if someone from the InDesign team could explain what is the difference between using `app.removeEventListener` and `eventListener.remove`.

From what I’ve observed is that the `EventListeners` collection contains ALL app events registered in InDesign. The `remove` method on that allows you to unregister _any_ listener, even those not registered by your script or plugin! In fact you can even disable built-in handlers (for “homeScreenVisibilityChange”) this way… icky stuff.

The `app.removeEventListener` has an additional safety net because it requires you to specify the original handler function, which only the original script/plugin has the reference of. But this only works for as long as the script/plugin is still loaded, after reload the reference is no longer valid, meaning that you cannot remove it that way. This dangling reference to a function which no longer exists is probl. also why indesign crashes (well the cleanup part might be missing somewhere).

@pedro Your solution works nicely, but there is no way of knowing if the `myEventName` is _your_ event handler - to be super-sure you should use a super-unique name
