Skip to content

Commit

Permalink
Merge pull request #42 from Galooshi/lazy-load
Browse files Browse the repository at this point in the history
Lazy-load background importjs process
  • Loading branch information
trotzig authored Jun 5, 2018
2 parents fc49457 + 5d6131a commit 6ba1b22
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
18 changes: 13 additions & 5 deletions autoload/importjs.vim
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ function importjs#TryExecPayload(payload, tryCount)
endfunction

function importjs#ExecCommand(command, arg, ...)
" lazy-load the background process
call importjs#Init()

let sendContent = (a:0 >= 1) ? a:1 : 1
if sendContent == 1
let fileContent = join(getline(1, '$'), "\n")
Expand Down Expand Up @@ -236,6 +239,10 @@ function! s:JobHandler(job_id, data, event) dict
endfunction

function! importjs#Init()
if exists("s:job")
return
endif

let s:callbacks = {
\ 'on_stdout': function('s:JobHandler'),
\ 'on_stderr': function('s:JobHandler'),
Expand All @@ -251,17 +258,18 @@ function! importjs#Init()
let s:job_executable='importjs'
endif

" neovim
if exists("*jobstart")
" neovim
let s:job = jobstart([s:job_executable, 'start', '--parent-pid', getpid()], s:callbacks)
endif

" vim
if exists("*job_start")
elseif exists("*job_start")
" vim
let s:job=job_start([s:job_executable, 'start', '--parent-pid', getpid()], {
\'exit_cb': 'importjs#JobExit',
\})

let g:ImportJSChannel=job_getchannel(s:job)
" ignore first line of output, which is something like
" > ImportJS (v2.10.1) DAEMON active.
call ch_readraw(g:ImportJSChannel, { "timeout": 2000 })
endif
endfunction
2 changes: 0 additions & 2 deletions plugin/import-js.vim
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
command! ImportJSWord call importjs#Word()
command! ImportJSGoto call importjs#Goto()
command! ImportJSFix call importjs#Fix()

call importjs#Init()

0 comments on commit 6ba1b22

Please sign in to comment.