public static Registration addHandler()

in util/base/src/main/java/jetbrains/jetpad/base/ThrowableHandlers.java [54:82]


  public static Registration addHandler(Consumer<? super Throwable> handler) {
    final Registration handlerReg = ourHandlers.get().addHandler(handler);

    final Value<Registration> leakReg = new Value<>();
    if (DEBUG) {
      final RuntimeException leakStacktrace = new RuntimeException("Potential leak");
      synchronized (ourLeaks) {
        ourLeaks.add(leakStacktrace);
      }
      leakReg.set(new Registration() {
        @Override
        protected void doRemove() {
          synchronized (ourLeaks) {
            ourLeaks.remove(leakStacktrace);
          }
        }
      });
    }

    return new Registration() {
      @Override
      protected void doRemove() {
        handlerReg.remove();
        if (leakReg.get() != null) {
          leakReg.get().dispose();
        }
      }
    };
  }