Skip to content

Commit

Permalink
Only start one daemon
Browse files Browse the repository at this point in the history
With a recent commit to import-js [1], we no longer require one daemon per
working directory. Instead the path to the file will be used to find a
project root that then serves as the working directory.

[1]: Galooshi/import-js@dcd41b2
  • Loading branch information
trotzig committed Jan 21, 2017
1 parent 6a2ad03 commit af7997b
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions import-js.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import subprocess

import_js_environment = {}
daemons = {}
daemon = None


def extract_path():
Expand Down Expand Up @@ -54,10 +54,10 @@ def plugin_loaded():
print(import_js_environment)

def plugin_unloaded():
global daemons
for cwd, daemon in daemons.items():
print('Stopping ImportJS daemon process for cwd ' + cwd)
daemon.terminate()
global daemon
print('Stopping ImportJS daemon process')
daemon.terminate()
daemon = None

def no_executable_error(executable):
return dedent('''
Expand Down Expand Up @@ -94,18 +94,18 @@ class ImportJsCommand(sublime_plugin.TextCommand):
def project_root(self):
return self.view.window().extract_variables()['folder']

def start_or_get_daemon(self, cwd):
global daemons
if (cwd in daemons):
return daemons[cwd]
def start_or_get_daemon(self):
global daemon
if (daemon != None):
return daemon

is_windows = os.name == 'nt'
executable = 'importjsd'

try:
daemon = subprocess.Popen(
[executable, 'start', '--parent-pid', str(os.getppid())],
cwd=cwd,
cwd=self.project_root(),
env=import_js_environment,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
Expand All @@ -118,8 +118,6 @@ def start_or_get_daemon(self, cwd):
# commands.
daemon.stdout.readline()

daemons[cwd] = daemon

return daemon
except FileNotFoundError as e:
if(e.strerror.find(executable) > -1):
Expand Down Expand Up @@ -153,7 +151,7 @@ def run(self, edit, **args):


print(payload)
process = self.start_or_get_daemon(self.project_root())
process = self.start_or_get_daemon()
process.stdin.write((json.dumps(payload) + '\n').encode('utf-8'))
process.stdin.flush()
resultJson = process.stdout.readline().decode('utf-8')
Expand Down

0 comments on commit af7997b

Please sign in to comment.