Extension activation: `activate` versus main script

Not really a “getting started” question, but does anyone have any insights what specific functionality the activate function has when activating an extension? The docs state that

activate is invoked when the extension is loaded into a workspace, such as if a new workspace is opened or a new extension is installed while a workspace is already open.

which it does; however, both according to the docs and to my own observation, extensions load on a per workspace basis, meaning that all code in main, not just activate , runs when that happens. Try as I may, I couldn’t find any situation where activate is called without the entire main script running again, so what am I missing? Is it just a matter of synchronous versus asynchronous execution during activation (activate can be async)?

Hello Martin,

The biggest difference is API contract and guarantees. activate is guaranteed to be invoked at a point after which all other bits of the extension runtime are initialized asynchronously. With executing code directly in main.js at the top level, there is no guarantee that everything is fully initialized. This might not present itself currently, but may do so in the future, so we follow the CommonJS / Node.js paradigm of having an explicit entry point for the extension that guarantees it is at a safe point for startup.

It’s sort of equivalent to, in HTML / DOM / JavaScript, executing scripts when loaded vs. registering an onLoad event handler that is guaranteed to be invoked after the page is fully loaded.

Hope this helps!

Logan

2 Likes

Thanks @logan, that does clarify things quite a bit.