Skip to content

Commit

Permalink
feat: observeAndWarn function
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Jan 7, 2021
1 parent da69859 commit 65de0e0
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/performance-monitor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

/* Utility function that observes a config and throws warnings once a day if it is not the recommended value */
function observeAndWarn (configName, recommendedValue, warningTitle, warningDescription) {
return atom.config.observe(configName, value => {
if (value !== recommendedValue) {
const storageName = `Minimap.${configName}`
const today = new Date()
const previousWarning = window.localStorage.getItem(storageName)
let previousWarningDay = null
if (previousWarning) {
previousWarningDay = (new Date(Date.parse(previousWarning))).getDay()
}
// throw the warning once a day
if (!previousWarningDay ||
(typeof previousWarningDay === 'number' && (previousWarningDay - today.getDay() >= 1))
) {
window.localStorage.setItem(storageName, today)

const notification = atom.notifications.addWarning(
warningTitle, {
description: warningDescription,
dismissable: true,
buttons: [
{
text: `Set to ${recommendedValue} and restart Atom`,
onDidClick () {
atom.config.set(configName, true)
notification.dismiss()
window.localStorage.removeItem(storageName)
setTimeout(() => {
atom.reload()
}, 1500)
}
}
]
})
}
}
})
}

0 comments on commit 65de0e0

Please sign in to comment.