Skip to content

Commit

Permalink
add debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
z80dev committed Oct 27, 2023
1 parent e489191 commit f19aaec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
19 changes: 19 additions & 0 deletions vyper_lsp/debounce.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import threading


class Debouncer:
def __init__(self, wait):
self.wait = wait
self.timer = None
self.lock = threading.Lock()

def debounce(self, func):
def debounced(*args, **kwargs):
with self.lock:
if self.timer is not None:
self.timer.cancel() # Cancel the existing timer if there is one
# Create a new timer that will call func with the latest arguments
self.timer = threading.Timer(self.wait, lambda: func(*args, **kwargs))
self.timer.start()

return debounced
5 changes: 5 additions & 0 deletions vyper_lsp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
from pygls.server import LanguageServer
from vyper_lsp.analyzer.AstAnalyzer import AstAnalyzer
from vyper_lsp.analyzer.SourceAnalyzer import SourceAnalyzer
from vyper_lsp.debounce import Debouncer

from vyper_lsp.navigation import ASTNavigator
from vyper_lsp.utils import get_installed_vyper_version


from .ast import AST

server = LanguageServer("vyper", "v0.0.1")
Expand All @@ -48,6 +50,8 @@

ast = AST()

debouncer = Debouncer(wait=0.5)


def check_minimum_vyper_version():
vy_version = get_installed_vyper_version()
Expand All @@ -58,6 +62,7 @@ def check_minimum_vyper_version():
)


@debouncer.debounce
def validate_doc(ls, params):
text_doc = ls.workspace.get_document(params.text_document.uri)
source_diagnostics = source_analyzer.get_diagnostics(text_doc)
Expand Down

0 comments on commit f19aaec

Please sign in to comment.