Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
Cancel pending idle callbacks
Browse files Browse the repository at this point in the history
If the package is requested to be deactivated before a pending idle
callback has ran we should cancel that callback as there is no point in
running it.
  • Loading branch information
Arcanemagus committed Apr 12, 2017
1 parent c19ab39 commit a2dcc6d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
24 changes: 19 additions & 5 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ let ignoredRulesWhenModified;
let ignoredRulesWhenFixing;
let disableWhenNoEslintConfig;

// Internal variables
const idleCallbacks = new Set();

// Internal functions
const idsToIgnoredRules = ruleIds => ruleIds.reduce((ids, id) => {
ids[id] = 0; // 0 is the severity to turn off a rule
Expand All @@ -32,17 +35,26 @@ const waitOnIdle = () => new Promise(resolve => {
// The worker is initialized during an idle time, since the queued idle
// callbacks are done in order, waiting on a newly queued idle callback will
// ensure that the worker has been initialized
window.requestIdleCallback(resolve);
const callbackID = window.requestIdleCallback(() => {
idleCallbacks.delete(callbackID);
resolve();
});
idleCallbacks.add(callbackID);
});

module.exports = {
activate() {
var _this = this;

const installLinterEslintDeps = () => require('atom-package-deps').install('linter-eslint');
if (!atom.inSpecMode()) {
window.requestIdleCallback(installLinterEslintDeps);
}
let callbackID;
const installLinterEslintDeps = () => {
idleCallbacks.delete(callbackID);
if (!atom.inSpecMode()) {
require('atom-package-deps').install('linter-eslint');
}
};
callbackID = window.requestIdleCallback(installLinterEslintDeps);
idleCallbacks.add(callbackID);

this.subscriptions = new _atom.CompositeDisposable();
this.active = true;
Expand Down Expand Up @@ -222,6 +234,8 @@ module.exports = {
window.requestIdleCallback(initializeWorker, { timeout: 5000 });
},
deactivate() {
idleCallbacks.forEach(callbackID => window.cancelIdleCallback(callbackID));
idleCallbacks.clear();
this.active = false;
this.subscriptions.dispose();
},
Expand Down
22 changes: 18 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ let ignoredRulesWhenModified
let ignoredRulesWhenFixing
let disableWhenNoEslintConfig

// Internal variables
const idleCallbacks = new Set()

// Internal functions
const idsToIgnoredRules = ruleIds =>
ruleIds.reduce((ids, id) => {
Expand All @@ -30,15 +33,24 @@ const waitOnIdle = () =>
// The worker is initialized during an idle time, since the queued idle
// callbacks are done in order, waiting on a newly queued idle callback will
// ensure that the worker has been initialized
window.requestIdleCallback(resolve)
const callbackID = window.requestIdleCallback(() => {
idleCallbacks.delete(callbackID)
resolve()
})
idleCallbacks.add(callbackID)
})

module.exports = {
activate() {
const installLinterEslintDeps = () => require('atom-package-deps').install('linter-eslint')
if (!atom.inSpecMode()) {
window.requestIdleCallback(installLinterEslintDeps)
let callbackID
const installLinterEslintDeps = () => {
idleCallbacks.delete(callbackID)
if (!atom.inSpecMode()) {
require('atom-package-deps').install('linter-eslint')
}
}
callbackID = window.requestIdleCallback(installLinterEslintDeps)
idleCallbacks.add(callbackID)

this.subscriptions = new CompositeDisposable()
this.active = true
Expand Down Expand Up @@ -208,6 +220,8 @@ module.exports = {
window.requestIdleCallback(initializeWorker, { timeout: 5000 })
},
deactivate() {
idleCallbacks.forEach(callbackID => window.cancelIdleCallback(callbackID))
idleCallbacks.clear()
this.active = false
this.subscriptions.dispose()
},
Expand Down

0 comments on commit a2dcc6d

Please sign in to comment.