Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Implement Throttling Prioritized Package Loading (WIP - Open to Comment) #1168

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/atom-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,8 @@ class AtomEnvironment {
* @desc Returns output of `preloadPackages()` for this Classes Instance of `Packages`.
*/
preloadPackages() {
return this.packages.preloadPackages();
// Should no longer be preloading packages
//return this.packages.preloadPackages();
}

attachSaveStateListeners() {
Expand Down Expand Up @@ -970,7 +971,8 @@ class AtomEnvironment {
StartupTime.addMarker(
'window:environment:start-editor-window:load-packages'
);
this.packages.loadPackages();
//this.packages.loadPackages();
this.packages.setupPackageEnvironment();

const startTime = Date.now();
StartupTime.addMarker(
Expand Down Expand Up @@ -1042,7 +1044,8 @@ class AtomEnvironment {
StartupTime.addMarker(
'window:environment:start-editor-window:activate-packages'
);
this.packages.activate();
await this.packages.activateAsync();
//this.packages.activate();
this.keymaps.loadUserKeymap();
if (!this.getLoadSettings().safeMode) this.requireUserInitScript();

Expand Down
94 changes: 94 additions & 0 deletions src/package-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,15 @@ module.exports = class PackageManager {
return pack;
}

setupPackageEnvironment() {
require("../exports/atom");
// We just lie, in case anyone needs it
this.initialPackagesLoaded = true;
this.emitter.emit("did-load-initial-packages");
}

loadPackages() {
return; // should no longer be called
// Ensure atom exports is already in the require cache so the load time
// of the first package isn't skewed by being the first to require atom
require('../exports/atom');
Expand Down Expand Up @@ -723,7 +731,83 @@ module.exports = class PackageManager {
}

// Activate all the packages that should be activated.
async activateAsync() {
// TODO implement new loading methodology, while being async,
// must handled loading of packages, and activation

// Since NOTHING is loaded/preloaded or prepared at all, we will handle it
// all here.
const availablePackages = this.getAvailablePackages();
const disabledPackageNames = new Set(
this.config.get("core.disabledPackages")
);

// Load all available packages
this.config.transact(() => {
for (const pack of availablePackages) {
this.loadAvailablePackage(pack, disabledPackageNames);
}
});

// Available Packages is now an array of items with:
// - isBundled: A boolean on whether or not the package is bundled
// - name: The name of the package
// - path: A path to the top-level-directory of the package.

// We will now want to iterate packages, and begin loading them asyncronously
// depending on priority
const priorityMatrix = { 0: [], 1: [], 2: [], 3: [], 4: [], 5: [] };

const priorityZeroPackages = [
// A temporary hardcoded list of packages to always load first
"timecop", "tree-view", "background-tips", "status-bar", "tabs", "notifications"
];

for (const pack of availablePackages) {
// Lets first handle some hardcoded items
if (priorityZeroPackages.includes(pack.name)) {
priorityMatrix["0"].push(pack);
} else if (this.config.get("core.themes").includes(pack.name)) {
// If the package is a by default enabled theme
priorityMatrix["0"].push(pack);
} else {
// Any other package
priorityMatrix["5"].push(pack);
}
}

// With our priority matrix settled, we now know how to handle loading our packages
const delay = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};

for (let priorityLvl = 0; priorityLvl < 6; priorityLvl++) {
console.log(`Triggering package activation of priority lvl ${priorityLvl}`);
// Packages now being loaded outside the activation step
for (let [activator, types] of this.packageActivators) {
const packages = this.getLoadedPackagesForTypes(types);
//await activator.activatePackages(packages);
if (typeof activator.activatePackagesAsync === "function") {
await activator.activatePackagesAsync(packages);
} else {
await activator.activatePackages(packages);
}
}

this.triggerDeferredActivationHooks();

this.initialPackagesActivated = true;
this.emitter.emit("did-activate-initial-packages");

// Wait some time before the next priority level occurs
await delay(2000);
}


}

activate() {
return; // Should no longer be used
let promises = [];
for (let [activator, types] of this.packageActivators) {
const packages = this.getLoadedPackagesForTypes(types);
Expand All @@ -748,6 +832,16 @@ module.exports = class PackageManager {
this.packageActivators.push([activator, types]);
}

async activatePackagesAsync(packages) {
for (const pack of packages) {
this.config.transactAsync(async () => {
await this.activatePackage(pack.name);
});
}
this.observeDisabledPackages();
this.observePackagesWithKeymapsDisabled();
}

activatePackages(packages) {
const promises = [];
this.config.transactAsync(() => {
Expand Down
Loading