Extract debounced load to service, so can be injected

This commit is contained in:
Alasdair Smith 2018-06-26 14:56:40 +01:00
parent 1f9c1ca6c6
commit b2abcfc3f2
2 changed files with 17 additions and 20 deletions

View file

@ -53,31 +53,12 @@ define [
lastCommandFragmentIsReqPack
if linesContainMeta or lastCommandFragmentIsMeta
@scheduleLoadCurrentDocMetaFromServer()
@Metadata.scheduleLoadDocMetaFromServer @$scope.docId
@editor.on "changeSession", (e) =>
e.oldSession.off "change", onChange
e.session.on "change", onChange
loadDocMetaFromServer: (docId) ->
@Metadata.loadDocMetaFromServer docId
scheduleLoadCurrentDocMetaFromServer: () ->
# De-bounce loading labels with a timeout
currentDocId = @$scope.docId
existingTimeout = @debouncer[currentDocId]
if existingTimeout?
clearTimeout(existingTimeout)
delete @debouncer[currentDocId]
@debouncer[currentDocId] = setTimeout(
() =>
@loadDocMetaFromServer currentDocId
delete @debouncer[currentDocId]
, 1000
, this
)
getAllLabels: () ->
@Metadata.getAllLabels()

View file

@ -3,6 +3,7 @@ define [
], (App) ->
App.factory 'metadata', ($http, ide) ->
debouncer = {} # DocId => Timeout
state = {documents: {}}
@ -46,4 +47,19 @@ define [
{_csrf: window.csrfToken}
)
metadata.scheduleLoadDocMetaFromServer = (docId) ->
# De-bounce loading labels with a timeout
existingTimeout = debouncer[docId]
if existingTimeout?
clearTimeout(existingTimeout)
delete debouncer[docId]
debouncer[docId] = setTimeout(
() =>
metadata.loadDocMetaFromServer docId
delete debouncer[docId]
, 1000
)
return metadata