forked from eclipse-lemminx/lemminx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wait a bit after change before sending diagnostics
Fixes eclipse-lemminx#1162 Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
1 parent
e77594e
commit b2f159b
Showing
2 changed files
with
101 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/commons/ModelValidatorDelayer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.commons; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Validate a given model document with delay. | ||
* | ||
* @author Angelo ZERR | ||
* | ||
* @param <T> | ||
*/ | ||
public class ModelValidatorDelayer<T> { | ||
|
||
private static final long DEFAULT_VALIDATION_DELAY_MS = 200; | ||
|
||
private final ScheduledExecutorService executorService; | ||
|
||
private final Consumer<T> validator; | ||
|
||
private final Map<String, Future<?>> pendingValidationRequests; | ||
|
||
private final long validationDelayMs; | ||
|
||
public ModelValidatorDelayer(ScheduledExecutorService executorService, Consumer<T> validator) { | ||
this(executorService, validator, DEFAULT_VALIDATION_DELAY_MS); | ||
} | ||
|
||
public ModelValidatorDelayer(ScheduledExecutorService executorService, Consumer<T> validator, | ||
long validationDelayMs) { | ||
this.executorService = executorService; | ||
this.validator = validator; | ||
this.pendingValidationRequests = new HashMap<>(); | ||
this.validationDelayMs = validationDelayMs; | ||
} | ||
|
||
/** | ||
* Validate the given model <code>document</code> identified by the given | ||
* <code>uri</code> with a delay. | ||
* | ||
* @param uri the document URI. | ||
* @param document the document model to validate. | ||
*/ | ||
public void validateWithDelay(String uri, T document) { | ||
cleanPendingValidation(uri); | ||
Future<?> request = executorService.schedule(() -> { | ||
synchronized (pendingValidationRequests) { | ||
pendingValidationRequests.remove(uri); | ||
} | ||
validator.accept(document); | ||
}, validationDelayMs, TimeUnit.MILLISECONDS); | ||
synchronized (pendingValidationRequests) { | ||
pendingValidationRequests.put(uri, request); | ||
} | ||
} | ||
|
||
private void cleanPendingValidation(String uri) { | ||
synchronized (pendingValidationRequests) { | ||
Future<?> request = pendingValidationRequests.get(uri); | ||
if (request != null) { | ||
request.cancel(true); | ||
pendingValidationRequests.remove(uri); | ||
} | ||
} | ||
} | ||
} |