Home Download Buy Blog Forum Support

prevent concurrent run of callback?

prevent concurrent run of callback?

Postby hgraca on Sat Apr 20, 2013 5:25 pm

Hi,

Im new to phyton and sublime plugin development.
I am now creating a plugin and at some point I want to get info from the user, and use that info in the next lines of code.

I have figured out how to get the info, but the lines after the user input are executed before the callback function.

Here's what I mean:
Code: Select all
window.show_input_panel("What is the new function name?", '', self.onNewFcNameGiven, None, None)
self.runCommandLine(self.view.file_name(), self.firstLine, self.lastLine, self.newFcName)

So on line 1 I ask for user input, the self.onNewFcNameGiven function receives the user input and puts it in the variable self.newFcName, which would be used in line 2. However line 2 executes before self.newFcName is set in the callback function in line 1.

So, how can I make line 2 run only after the callback function, in line 1, is finished?

Tkx in advance for any help.

Herb
hgraca
 
Posts: 7
Joined: Fri Dec 07, 2012 10:09 am

Re: prevent concurrent run of callback?

Postby skuroda on Sat Apr 20, 2013 5:49 pm

I'm confused. If you want to run the method after the input content panel has been given, why not just put it in the on_done callback?

Code: Select all
class TestCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        # Your other stuff here...
        window.show_input_panel("What is the new function name?", "", self.onNewFcNameGive, None, None)

    def onNewFcNameGive(self, content):
        # Callback stuff here
        self.runCommandLine(self.view.file_name(), self.firstLine, self.lastLine, self.newFcName)

skuroda
 
Posts: 674
Joined: Fri Oct 05, 2012 8:11 am

Re: prevent concurrent run of callback?

Postby hgraca on Sat Apr 20, 2013 6:10 pm

I get your point, it defenitly works, however it doesn't seem right to be forced to create a new function every time I want to get user input...

What happens if I need 10 user inputs? I have to create and chain 10 functions?
It doesn't seem right... So beeing a rookie on this, I thought there should be some other way...
hgraca
 
Posts: 7
Joined: Fri Dec 07, 2012 10:09 am

Re: prevent concurrent run of callback?

Postby sapphirehamster on Sat Apr 20, 2013 7:12 pm

Yes, you will need to chain several functions. This is because the entire sublime process blocks while a plugin is running. I'm guessing this vastly simplifies concurrency issues.
sapphirehamster
 
Posts: 74
Joined: Sun Jul 01, 2012 11:19 pm

Re: prevent concurrent run of callback?

Postby skuroda on Sat Apr 20, 2013 7:53 pm

If you are just storing the values, or want to perform the same actions on each input panel callback, you could probably do something like this.

Code: Select all
class TestCommand(sublime_plugin.WindowCommand):
    def run(self):
        # Your other stuff here...
        self.map = {}
        self.keys = ["a", "b", "c", "d", "e"]
        self.key_index = 0
        self.window.show_input_panel("What is the new function name?", "", self.onNewFcNameGive, None, None)

    def onNewFcNameGive(self, content):
        # Callback stuff here
        self.map[self.keys[self.key_index]] = content
        if len(self.map) == len(self.keys):
            print(self.map)
        else:
            self.key_index += 1
            self.window.show_input_panel("What is the new function name?", "", self.onNewFcNameGive, None, None)



Of course, rather than print the map, call whatever complete function you want.
skuroda
 
Posts: 674
Joined: Fri Oct 05, 2012 8:11 am

Re: prevent concurrent run of callback?

Postby hgraca on Sun Apr 21, 2013 10:41 am

Just for the record, I followed your advice and didn't waist more time looking for a way to do it (tkx).

However, I used a lambda function, just because it saved me from creating an extra non-lambda function (the callback can only have a string parameter and I needed to pass more parameters):
The input panel call:
Code: Select all
window.show_input_panel("What is the new function name?", '', lambda newFcName: self.runCommandLine(self.view.file_name(), firstLine, lastLine, newFcName, execute), None, None)

The callback function signature:
Code: Select all
def runCommandLine(self, filePath, fromLine, toLine, newFcName, execute=False):


Just tought of putting it here, it might be usefull to another rookie.

Tkx for your help.
hgraca
 
Posts: 7
Joined: Fri Dec 07, 2012 10:09 am


Return to Plugin Development

Who is online

Users browsing this forum: Google [Bot] and 7 guests

cron