diff --git a/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/eventsync/EventSyncPubSubHolder.java b/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/eventsync/EventSyncPubSubHolder.java index 32e53b2e38e9..2046e0ab26ef 100644 --- a/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/eventsync/EventSyncPubSubHolder.java +++ b/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/eventsync/EventSyncPubSubHolder.java @@ -35,6 +35,7 @@ * @since 2201.1.1 */ public class EventSyncPubSubHolder { + private static final LanguageServerContext.Key SUBSCRIBERS_HOLDER_KEY = new LanguageServerContext.Key<>(); private static final Map publisherMap = new HashMap<>(); @@ -56,8 +57,8 @@ private void initialize(LSClientLogger lsClientLogger) { ServiceLoader publishers = ServiceLoader.load(EventPublisher.class); publishers.forEach(eventPublisher -> { - for (EventSubscriber eventSubscriber: eventSubscribersMap.get(eventPublisher.getKind())) { - publisherMap.put(eventPublisher.getKind(), eventPublisher); + publisherMap.put(eventPublisher.getKind(), eventPublisher); + for (EventSubscriber eventSubscriber : eventSubscribersMap.get(eventPublisher.getKind())) { eventPublisher.subscribe(eventSubscriber); lsClientLogger.logTrace(String.format("%s subscribed to %s", eventSubscriber.getName(), eventPublisher.getName())); @@ -73,9 +74,13 @@ private void initialize(LSClientLogger lsClientLogger) { public static EventSyncPubSubHolder getInstance(LanguageServerContext serverContext) { EventSyncPubSubHolder subscribersHolder = serverContext.get(SUBSCRIBERS_HOLDER_KEY); if (subscribersHolder == null) { - subscribersHolder = new EventSyncPubSubHolder(serverContext); + synchronized (SUBSCRIBERS_HOLDER_KEY) { + subscribersHolder = serverContext.get(SUBSCRIBERS_HOLDER_KEY); + if (subscribersHolder == null) { + subscribersHolder = new EventSyncPubSubHolder(serverContext); + } + } } - return subscribersHolder; } diff --git a/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/eventsync/publishers/ProjectUpdateEventPublisher.java b/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/eventsync/publishers/ProjectUpdateEventPublisher.java index cd05b32b3e4a..e58d7be0f164 100644 --- a/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/eventsync/publishers/ProjectUpdateEventPublisher.java +++ b/language-server/modules/langserver-core/src/main/java/org/ballerinalang/langserver/eventsync/publishers/ProjectUpdateEventPublisher.java @@ -59,7 +59,7 @@ public void publish(ExtendedLanguageClient client, LanguageServerContext serverC Executor delayedExecutor = CompletableFuture.delayedExecutor(DIAGNOSTIC_DELAY, TimeUnit.SECONDS); CompletableFuture scheduledFuture = CompletableFuture.supplyAsync(() -> true, delayedExecutor); latestScheduled = scheduledFuture; - scheduledFuture.thenAcceptAsync(aBoolean -> - subscribers.forEach(subscriber -> subscriber.onEvent(client, context, serverContext))); + scheduledFuture.thenAcceptAsync(aBoolean -> subscribers.parallelStream() + .forEach(subscriber -> subscriber.onEvent(client, context, serverContext))); } }