Skip to content

Commit

Permalink
Refactor to declutter global namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jalenng committed Jun 1, 2021
1 parent c800fb2 commit 364b788
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 34 deletions.
4 changes: 2 additions & 2 deletions public/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const { BrowserWindow, nativeTheme, app } = require('electron')

const createWindow = require('./createWindow')

/* Initialize the stores and systems */
require('./store/storeInitializer')
// Initialize the stores and systems
require('./store/initializeStore')
require('./systems/initializeSystems')

require('./ipcHandlers')
Expand Down
4 changes: 2 additions & 2 deletions public/initializeMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const menu = Menu.buildFromTemplate([
{
label: 'Start/Stop',
accelerator: 'CmdOrCtrl+s',
click: () => { global.timerSystem.togglePause() }
click: () => { global.systems.timer.togglePause() }
}
]
},
Expand All @@ -59,7 +59,7 @@ const menu = Menu.buildFromTemplate([
{
label: 'Test break',
accelerator: 'CmdOrCtrl+e',
click: () => { global.timerSystem.end() }
click: () => { global.systems.timer.end() }
}
]
}]
Expand Down
2 changes: 1 addition & 1 deletion public/initializeTray.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let appTray = null
// Function to get path of icon file
const getTrayImage = function () {
// Calculate percentage
const timerStatus = global.timerSystem.getStatus()
const timerStatus = global.systems.timer.getStatus()
const percentage = timerStatus.remainingTime / timerStatus.totalDuration * 100
const percentageMultOfFive = Math.round(percentage / 5) * 5

Expand Down
18 changes: 9 additions & 9 deletions public/ipcHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,47 +116,47 @@ ipcMain.on('get-theme-name', (event) => {

// Reset the timer
ipcMain.handle('timer-reset', () => {
global.timerSystem.reset()
global.systems.timer.reset()
})

// End the timer (and start the break)
ipcMain.handle('timer-end', () => {
global.timerSystem.end()
global.systems.timer.end()
})

// Toggle pause/play
ipcMain.handle('timer-toggle', () => {
global.timerSystem.togglePause()
global.systems.timer.togglePause()
})

// Block the timer from running
ipcMain.handle('timer-block', () => {
global.timerSystem.block()
global.systems.timer.block()
})

// Get break status
ipcMain.on('get-break-status', (event) => {
event.reply('receive-break-status', global.breakSystem.getStatus())
event.reply('receive-break-status', global.systems.break.getStatus())
})

// Play sound file
ipcMain.handle('play-sound', () => {
global.breakSystem.playSound()
global.systems.break.playSound()
})

// Get list of open windows
ipcMain.on('get-open-windows', async (event) => {
event.returnValue = global.appSnapshotSystem.getLastSnapshot()
event.returnValue = global.systems.appSnapshot.getLastSnapshot()
})

// Get timer status
ipcMain.on('get-blockers', (event) => {
event.reply('receive-blockers', global.blockerSystem.getBlockers())
event.reply('receive-blockers', global.systems.blocker.getBlockers())
})

// Clear blockers
ipcMain.handle('clear-blockers', () => {
global.blockerSystem.clear()
global.systems.blocker.clear()
})

/* ------------------------------------------------------------------------- */
Expand Down
File renamed without changes.
42 changes: 22 additions & 20 deletions public/systems/initializeSystems.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,55 @@
const { powerMonitor } = require('electron')

const TimerSystem = require('./TimerSystem')
const BreakSystem = require('./breakSystem')
const NotificationSystem = require('./notificationSystem')
const BreakSystem = require('./BreakSystem')
const NotificationSystem = require('./NotificationSystem')
const AppSnapshotSystem = require('./AppSnapshotSystem')
const BlockerSystem = require('./BlockerSystem')

global.timerSystem = new TimerSystem()
global.breakSystem = new BreakSystem()
global.notificationSystem = new NotificationSystem()
global.appSnapshotSystem = new AppSnapshotSystem()
global.blockerSystem = new BlockerSystem()
global.systems = {
timer: new TimerSystem(),
break: new BreakSystem(),
notification: new NotificationSystem(),
appSnapshot: new AppSnapshotSystem(),
blocker: new BlockerSystem()
}

/* ------------------------------------------------------------------------- */
/* If timer has stopped and the notification interval updates, update the timer with the new value. */

global.store.onDidChange('preferences.notifications.interval', () => {
if (global.timerSystem.isStopped) global.timerSystem.update()
if (global.systems.timer.isStopped) global.systems.timer.update()
})

/* ------------------------------------------------------------------------- */
/* Configure event listeners and connect the various systems */

// Start break when timer ends
global.timerSystem.on('timer-end', () => global.breakSystem.start())
global.systems.timer.on('timer-end', () => global.systems.break.start())

// Create notification windows when break starts
global.breakSystem.on('break-start', () => global.notificationSystem.createWindows())
global.systems.break.on('break-start', () => global.systems.notification.createWindows())

// Minimize notification when the break time is set/reset
global.breakSystem.on('break-times-set', () => global.notificationSystem.minimize())
global.systems.break.on('break-times-set', () => global.systems.notification.minimize())

// Expand notification when the break time is past the intermediate point
global.breakSystem.on('break-intermediate', () => global.notificationSystem.maximize())
global.systems.break.on('break-intermediate', () => global.systems.notification.maximize())

// Close notification windows when break ends
global.breakSystem.on('break-end', () => global.notificationSystem.closeWindows())
global.systems.break.on('break-end', () => global.systems.notification.closeWindows())

// Start timer when break ends
global.breakSystem.on('break-end', () => global.timerSystem.start())
global.systems.break.on('break-end', () => global.systems.timer.start())

// Process the list of open apps through the blocker system when a snapshot is taken
global.appSnapshotSystem.on('app-snapshot-taken', (snapshot) => global.blockerSystem.processAppSnapshot(snapshot))
global.systems.appSnapshot.on('app-snapshot-taken', (snapshot) => global.systems.blocker.processAppSnapshot(snapshot))

// Block the timer when a blocker is detected
global.blockerSystem.on('blocker-detected', () => global.timerSystem.block())
global.systems.blocker.on('blocker-detected', () => global.systems.timer.block())

// Unblock the timer when all blockers are cleared
global.blockerSystem.on('blockers-cleared', () => global.timerSystem.unblock())
global.systems.blocker.on('blockers-cleared', () => global.systems.timer.unblock())

/* ------------------------------------------------------------------------- */
/* Update blockers when the power state updates */
Expand All @@ -62,7 +64,7 @@ global.blockerSystem.on('blockers-cleared', () => global.timerSystem.unblock())

if (powerMonitor.isOnBatteryPower()) {
if (global.store.get('preferences.blockers.blockOnBattery')) {
global.blockerSystem.add({
global.systems.blocker.add({
type: 'other',
key: 'batteryPower',
message: 'Your computer is running on battery power.'
Expand All @@ -73,7 +75,7 @@ if (powerMonitor.isOnBatteryPower()) {
// Add a blocker if switched to battery power
powerMonitor.on('on-battery', () => {
if (global.store.get('preferences.blockers.blockOnBattery')) {
global.blockerSystem.add({
global.systems.blocker.add({
type: 'other',
key: 'batteryPower',
message: 'Your computer is running on battery power.'
Expand All @@ -84,6 +86,6 @@ powerMonitor.on('on-battery', () => {
// Remove the blocker if switched to AC power
powerMonitor.on('on-ac', () => {
if (global.store.get('preferences.blockers.blockOnBattery')) {
global.blockerSystem.remove('other', 'batteryPower')
global.systems.blocker.remove('other', 'batteryPower')
}
})

0 comments on commit 364b788

Please sign in to comment.