diff options
Diffstat (limited to 'subprojects/language-web/src/main/java/tools/refinery/language/web/xtext/server/push/PushWebDocument.java')
-rw-r--r-- | subprojects/language-web/src/main/java/tools/refinery/language/web/xtext/server/push/PushWebDocument.java | 60 |
1 files changed, 32 insertions, 28 deletions
diff --git a/subprojects/language-web/src/main/java/tools/refinery/language/web/xtext/server/push/PushWebDocument.java b/subprojects/language-web/src/main/java/tools/refinery/language/web/xtext/server/push/PushWebDocument.java index 56fd12c9..ca97147a 100644 --- a/subprojects/language-web/src/main/java/tools/refinery/language/web/xtext/server/push/PushWebDocument.java +++ b/subprojects/language-web/src/main/java/tools/refinery/language/web/xtext/server/push/PushWebDocument.java | |||
@@ -5,11 +5,7 @@ | |||
5 | */ | 5 | */ |
6 | package tools.refinery.language.web.xtext.server.push; | 6 | package tools.refinery.language.web.xtext.server.push; |
7 | 7 | ||
8 | import java.util.ArrayList; | 8 | import com.google.common.collect.ImmutableList; |
9 | import java.util.HashMap; | ||
10 | import java.util.List; | ||
11 | import java.util.Map; | ||
12 | |||
13 | import org.eclipse.xtext.util.CancelIndicator; | 9 | import org.eclipse.xtext.util.CancelIndicator; |
14 | import org.eclipse.xtext.web.server.IServiceResult; | 10 | import org.eclipse.xtext.web.server.IServiceResult; |
15 | import org.eclipse.xtext.web.server.model.AbstractCachedService; | 11 | import org.eclipse.xtext.web.server.model.AbstractCachedService; |
@@ -17,60 +13,59 @@ import org.eclipse.xtext.web.server.model.DocumentSynchronizer; | |||
17 | import org.eclipse.xtext.web.server.model.XtextWebDocument; | 13 | import org.eclipse.xtext.web.server.model.XtextWebDocument; |
18 | import org.slf4j.Logger; | 14 | import org.slf4j.Logger; |
19 | import org.slf4j.LoggerFactory; | 15 | import org.slf4j.LoggerFactory; |
20 | 16 | import tools.refinery.language.web.generator.ModelGenerationManager; | |
21 | import com.google.common.collect.ImmutableList; | ||
22 | |||
23 | import tools.refinery.language.web.xtext.server.ResponseHandlerException; | 17 | import tools.refinery.language.web.xtext.server.ResponseHandlerException; |
24 | 18 | ||
19 | import java.util.ArrayList; | ||
20 | import java.util.List; | ||
21 | |||
25 | public class PushWebDocument extends XtextWebDocument { | 22 | public class PushWebDocument extends XtextWebDocument { |
26 | private static final Logger LOG = LoggerFactory.getLogger(PushWebDocument.class); | 23 | private static final Logger LOG = LoggerFactory.getLogger(PushWebDocument.class); |
27 | 24 | ||
28 | private final List<PrecomputationListener> precomputationListeners = new ArrayList<>(); | 25 | private final List<PrecomputationListener> precomputationListeners = new ArrayList<>(); |
29 | 26 | ||
30 | private final Map<Class<?>, IServiceResult> precomputedServices = new HashMap<>(); | 27 | private final ModelGenerationManager modelGenerationManager = new ModelGenerationManager(); |
28 | |||
29 | private final DocumentSynchronizer synchronizer; | ||
31 | 30 | ||
32 | public PushWebDocument(String resourceId, DocumentSynchronizer synchronizer) { | 31 | public PushWebDocument(String resourceId, DocumentSynchronizer synchronizer) { |
33 | super(resourceId, synchronizer); | 32 | super(resourceId, synchronizer); |
34 | if (resourceId == null) { | 33 | this.synchronizer = synchronizer; |
35 | throw new IllegalArgumentException("resourceId must not be null"); | ||
36 | } | ||
37 | } | 34 | } |
38 | 35 | ||
39 | public boolean addPrecomputationListener(PrecomputationListener listener) { | 36 | public ModelGenerationManager getModelGenerationManager() { |
37 | return modelGenerationManager; | ||
38 | } | ||
39 | |||
40 | public void addPrecomputationListener(PrecomputationListener listener) { | ||
40 | synchronized (precomputationListeners) { | 41 | synchronized (precomputationListeners) { |
41 | if (precomputationListeners.contains(listener)) { | 42 | if (precomputationListeners.contains(listener)) { |
42 | return false; | 43 | return; |
43 | } | 44 | } |
44 | precomputationListeners.add(listener); | 45 | precomputationListeners.add(listener); |
45 | listener.onSubscribeToPrecomputationEvents(getResourceId(), this); | 46 | listener.onSubscribeToPrecomputationEvents(getResourceId(), this); |
46 | return true; | ||
47 | } | 47 | } |
48 | } | 48 | } |
49 | 49 | ||
50 | public boolean removePrecomputationListener(PrecomputationListener listener) { | 50 | public void removePrecomputationListener(PrecomputationListener listener) { |
51 | synchronized (precomputationListeners) { | 51 | synchronized (precomputationListeners) { |
52 | return precomputationListeners.remove(listener); | 52 | precomputationListeners.remove(listener); |
53 | } | 53 | } |
54 | } | 54 | } |
55 | 55 | ||
56 | public <T extends IServiceResult> void precomputeServiceResult(AbstractCachedService<T> service, String serviceName, | 56 | public <T extends IServiceResult> void precomputeServiceResult(AbstractCachedService<T> service, String serviceName, |
57 | CancelIndicator cancelIndicator, boolean logCacheMiss) { | 57 | CancelIndicator cancelIndicator, boolean logCacheMiss) { |
58 | var result = getCachedServiceResult(service, cancelIndicator, logCacheMiss); | 58 | var result = getCachedServiceResult(service, cancelIndicator, logCacheMiss); |
59 | if (result == null) { | 59 | if (result != null) { |
60 | LOG.error("{} service returned null result", serviceName); | 60 | notifyPrecomputationListeners(serviceName, result); |
61 | return; | ||
62 | } | 61 | } |
63 | var serviceClass = service.getClass(); | ||
64 | var previousResult = precomputedServices.get(serviceClass); | ||
65 | if (previousResult != null && previousResult.equals(result)) { | ||
66 | return; | ||
67 | } | ||
68 | precomputedServices.put(serviceClass, result); | ||
69 | notifyPrecomputationListeners(serviceName, result); | ||
70 | } | 62 | } |
71 | 63 | ||
72 | private <T extends IServiceResult> void notifyPrecomputationListeners(String serviceName, T result) { | 64 | public <T extends IServiceResult> void notifyPrecomputationListeners(String serviceName, T result) { |
73 | var resourceId = getResourceId(); | 65 | var resourceId = getResourceId(); |
66 | if (resourceId == null) { | ||
67 | return; | ||
68 | } | ||
74 | var stateId = getStateId(); | 69 | var stateId = getStateId(); |
75 | List<PrecomputationListener> copyOfListeners; | 70 | List<PrecomputationListener> copyOfListeners; |
76 | synchronized (precomputationListeners) { | 71 | synchronized (precomputationListeners) { |
@@ -91,4 +86,13 @@ public class PushWebDocument extends XtextWebDocument { | |||
91 | } | 86 | } |
92 | } | 87 | } |
93 | } | 88 | } |
89 | |||
90 | public void cancelModelGeneration() { | ||
91 | modelGenerationManager.cancel(); | ||
92 | } | ||
93 | |||
94 | public void dispose() { | ||
95 | synchronizer.setCanceled(true); | ||
96 | modelGenerationManager.dispose(); | ||
97 | } | ||
94 | } | 98 | } |