How do Packages actually work?

#1

I’ve been looking at the Atom source, but it’s a large one, and there are some things I’m still not sure on.

I’m trying to figure out how exactly the packages system in Atom works due to needing something similar in an application I’m working on. Some questions may be purely due to me being a newbie with Electron.

  • How does the Atom render process load the folders in the packages folder?
  • Is there any isolation of loaded Packages?

Any help is appreciated, thanks!

0 Likes

#2

I can’t give you specifics about all the jobs Atom performs when loading a package, but here’s the general sketch:

  1. Atom scans .atom/packages/ and .atom/dev/packages/ for folders.
  2. In each folder, Atom reads the package.json file to determine metadata and which file to start in.
  3. Atom uses require() to load the exported object from the main code file into an object that it stores in the atom.packages global. You can and should inspect this in the dev tools (all you have to do is type atom.packages into the console).
  4. Atom runs the activate() method in each package to register commands and perform any other setup tasks the package author has designed.
  5. When Atom needs that package’s code (you have used a command that points to that package), it goes back to the global and looks for the function it’s been told to run.

Packages aren’t isolated at all as far as I know, and this can cause problems if two packages that want to work on the same thing in Atom are active at the same time. The Atom API and usage of asynchronous calls make it so that most packages don’t run into each other most of the time, but it’s still very easy to make a package cause havoc for other parts of the system.

0 Likes

#3

Thanks the the write-up, I really appreciate it!
Interesting that there’s no isolation, however there is still something I’m clueless on:

I’m guessing this is all done in the main process, given the render process wouldn’t have access to the Node APIs to load the json + require the code. So, how does the render process know which packages are loaded and how to run/handle them, such as any frontend changes?

0 Likes

#4

Renderer processes do have access to Node APIs (you can test this by using fs = require('fs') in init.coffee) and Atom handles it in the renderer process.

0 Likes