summaryrefslogtreecommitdiff
path: root/platform/diff-impl/src/com/intellij/diff/chains/AsyncDiffRequestChain.java
blob: 63f194fd190246c9921e1ed50c323bbb1dc8ba02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.diff.chains;

import com.intellij.diff.chains.SimpleDiffRequestChain.DiffRequestProducerWrapper;
import com.intellij.diff.requests.ErrorDiffRequest;
import com.intellij.diff.requests.LoadingDiffRequest;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.ListSelection;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.util.BackgroundTaskUtil;
import com.intellij.util.EventDispatcher;
import com.intellij.util.concurrency.annotations.RequiresBackgroundThread;
import com.intellij.util.concurrency.annotations.RequiresEdt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.EventListener;
import java.util.List;

public abstract class AsyncDiffRequestChain extends DiffRequestChainBase {
  private final EventDispatcher<Listener> myDispatcher = EventDispatcher.create(Listener.class);

  private volatile List<? extends DiffRequestProducer> myRequests = null;

  @Nullable private ProgressIndicator myIndicator;
  private int myAssignments = 0;

  public void addListener(@NotNull Listener listener, @NotNull Disposable disposable) {
    myDispatcher.addListener(listener, disposable);
  }

  public void removeListener(@NotNull Listener listener) {
    myDispatcher.removeListener(listener);
  }

  @NotNull
  @Override
  public List<? extends DiffRequestProducer> getRequests() {
    List<? extends DiffRequestProducer> requests = myRequests;
    if (requests == null) {
      return Collections.singletonList(new DiffRequestProducerWrapper(new LoadingDiffRequest()));
    }
    return requests;
  }

  @NotNull
  @RequiresBackgroundThread
  public ListSelection<? extends DiffRequestProducer> loadRequestsInBackground() {
    try {
      return loadRequestProducers();
    }
    catch (DiffRequestProducerException e) {
      return ListSelection.createSingleton(new DiffRequestProducerWrapper(new ErrorDiffRequest(e)));
    }
  }

  @RequiresEdt
  public void onAssigned(boolean isAssigned) {
    if (isAssigned) {
      if (myAssignments == 0 && myIndicator == null) {
        myIndicator = startLoading();
      }
      myAssignments++;
    }
    else {
      myAssignments--;
      if (myAssignments == 0 && myIndicator != null) {
        myIndicator.cancel();
        myIndicator = null;
      }
    }
    assert myAssignments >= 0;
  }

  @Nullable
  @RequiresEdt
  private ProgressIndicator startLoading() {
    if (myRequests != null) return null;

    return BackgroundTaskUtil.executeAndTryWait(indicator -> {
      ListSelection<? extends DiffRequestProducer> producers = loadRequestsInBackground();
      return () -> {
        indicator.checkCanceled();
        applyLoadedChanges(producers);
      };
    }, null);
  }

  @RequiresEdt
  private void applyLoadedChanges(@NotNull ListSelection<? extends DiffRequestProducer> producers) {
    if (myRequests != null) return;

    myRequests = producers.getList();
    setIndex(producers.getSelectedIndex());
    myIndicator = null;

    myDispatcher.getMulticaster().onRequestsLoaded();
  }

  @NotNull
  @RequiresBackgroundThread
  protected abstract ListSelection<? extends DiffRequestProducer> loadRequestProducers() throws DiffRequestProducerException;

  public interface Listener extends EventListener {
    @RequiresEdt
    void onRequestsLoaded();
  }
}