Skip to content

Commit

Permalink
added debounce to text-document-did-change (#183)
Browse files Browse the repository at this point in the history
* added debounce to text-document-did-change

* version bump

* adding stuff back in

---------

Co-authored-by: Red-Giuliano <red.giuliano@zero-true.com>
  • Loading branch information
Red-Giuliano and Red-Giuliano authored Feb 23, 2024
1 parent 1761a4f commit b2701dc
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions copilot/copilot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
,DidChangeTextDocumentParams, CopilotPayloadSignInInitiate\
,CopilotPayloadSignInConfirm, CopilotGetCompletionsResult\
,CopilotPayloadSignOut, TextDocumentItem, AcceptRequest, RejectRequest
from zt_backend.utils import async_debounce
import requests
from typing import Union
import os
Expand Down Expand Up @@ -115,6 +116,7 @@ async def text_document_did_open(params: DidOpenTextDocumentParams):
except requests.RequestException as e:
raise HTTPException(status_code=500, detail=str(e))

@async_debounce(0.2)
async def text_document_did_change(params):
global copilot_enabled
global copilot_doc_open
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = zero-true
description = A collaborative notebook built for data scientists
long_description = file: README.md
long_description_content_type = text/markdown
version = 0.0.dev58
version = 0.0.dev59


[options]
Expand Down
23 changes: 23 additions & 0 deletions zt_backend/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import traceback
import site
import json
import asyncio
import rtoml
from threading import Timer

Expand Down Expand Up @@ -44,6 +45,28 @@ def call_it():
return decorator


def async_debounce(wait):
"""Decorator that postpones an async function's execution until after `wait` seconds have elapsed since the last time it was invoked."""
def decorator(fn):
current_task = None

async def debounced(*args, **kwargs):
nonlocal current_task

# Cancel the previous task if it exists and is not done
if current_task is not None and not current_task.done():
current_task.cancel()

async def wait_and_invoke():
await asyncio.sleep(wait) # Delay execution
await fn(*args, **kwargs) # Invoke the async function

# Schedule the new task
current_task = asyncio.create_task(wait_and_invoke())

return debounced
return decorator

def get_notebook(id=''):
global zt_notebook
if id!='':
Expand Down

0 comments on commit b2701dc

Please sign in to comment.