Consuming the Snippet service and activationCommands


#1

Hi, I’m the developer of the LaTeXTools package. In order to be a good Atom citizen, I’m trying to support lazy-loading and only activate the plugin when a LaTeXTools command is invoked.

I’m having the following issue. Some of the commands I implemented insert a snippet (e.g., to wrap the selected test in an \emph or \textbf command). In the main package file, I have a consumeService method that saves the snippet service object for future use, as per the documentation. The problem is that that method is only called when the LaTeXTools package is activated. So, if I open a file, and invoke one of the snippet-insertion commands, the command fails. The problem is that the package is correctly activated, but the command is executed before consumeService is invoked. As a consequence, the snippet service object is not yet available, and the command fails.

Is there any way to ensure that consumeService is invoked before anything else, and in before any of the package commands is actually executed? I guess not, because consumeService is a callback, but perhaps I misunderstand.

[Note that all is OK if I invoke some other LaTeXTools command: then, the package is activated, the snippet serivice object is retrieved, and all works as advertised.]

Thanks!


#2

If you’re consuming a service, you probably don’t want to use activationCommands in your package.json. You want the package to be loaded and ready to receive the service whenever it is ready to be provided. What you can do, is delay requiring anything inside the package’s main file until it is needed to perform some functionality. You can look at my tabs-to-spaces package for an example of how to do this.

Additionally, this is possibly a bug with the PackageManager. It should probably warn that providing or consuming services and activationCommands don’t mix well … or just ignore activationCommands if services are provided or consumed. Would you mind opening an Issue on https://github.com/atom/atom and include as much information as you can to reproduce the problem?


#3

Thank you @leedohm!! That helped. I implemented a lazy-loading scheme following the example in your package, though in my case things were slightly more complex. Still, I got a pretty significant reduction in loading time.