I ask some guidance in understanding how the
dispatch() instruction works.
The code example below is coded into
( System: Atom V1.21.1 on Windows7. )
# === init.coffee === waitTask = false taskDone = (event) -> # make sure correct trigger return false unless waitTask return false unless event.type is 'core:save-as' waitTask = false console.log "completed" atom.commands.add 'atom-text-editor', 'test command', -> return false unless _editor = atom.workspace.getActiveTextEditor() # return false unless _editor.getGrammar().scopeName is 'text.plain' # @1 console.log "prepared" waitTask = true atom.commands.onDidDispatch(taskDone) # @2 atom.commands.dispatch(atom.views.getView(_editor), 'core:save-as') # @3 console.log "started" return true
When the command is triggered the following occurs:
- The save-as dialog pops up as if the code is holding at line @1.
- After closing the dialog, all three text lines come up at once on the developer console:
prepared started completed
When swapping lines @2 and @3, the
completed text line come up only when the save-as dialog is closed by
Questions and comments
onDidDispatch()required? Or is the code “frozen” until the dispatch command is complete?
Why is the lines before
dispatch()not executed? Is there a rule that applies?
Why if lines @2 and @3 are swapped does
taskDone()execute only when dialog
cancelis used? Is the save-as command dispatched twice?
More thoughts / comment are welcome.
Thank you for your attention.