Skip to content

Commit

Permalink
Unsubscribe from config changes on dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
dcalhoun committed Oct 1, 2017
1 parent 33a642b commit f800f87
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ const dispose = require('./modules/dispose')
const { generateActivateCallback, onApp } = require('./modules/app')
const { generateBlurCallback, hideWindows, showWindows } = require('./modules/windows')

let handleActivate, handleBlur
let cfgUnsubscribe, handleActivate, handleBlur

exports.onApp = app => {
handleBlur = generateBlurCallback(hideWindows)(app)
handleActivate = generateActivateCallback(showWindows)(app)

onApp(app, handleActivate, handleBlur)
cfgUnsubscribe = onApp(app, handleActivate, handleBlur)
}
exports.onUnload = app => dispose(app, handleActivate, handleBlur)

exports.onUnload = app => dispose(app, { cfgUnsubscribe, handleActivate, handleBlur })
14 changes: 13 additions & 1 deletion modules/__tests__/dispose.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ jest.mock('../windows')
const app = generateApp()
const handleActivateMock = jest.fn()
const handleBlurMock = jest.fn()
const cfgUnsubscribeMock = jest.fn()

describe('dispose', () => {
beforeEach(() => {
dispose(app, handleActivateMock, handleBlurMock)
dispose(
app,
{
cfgUnsubscribe: cfgUnsubscribeMock,
handleActivate: handleActivateMock,
handleBlur: handleBlurMock
}
)
})

it('shows all windows', () => {
Expand All @@ -23,6 +31,10 @@ describe('dispose', () => {
expect(app.dock.show).toHaveBeenCalledTimes(1)
})

it('unsubscribes from config changes', () => {
expect(cfgUnsubscribeMock).toHaveBeenCalledTimes(1)
})

xit('unregisters the shortcut', () => {
// expect(unregisterShortcut).toHaveBeenCalledTimes(1)
})
Expand Down
3 changes: 1 addition & 2 deletions modules/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ const generateActivateCallback = callback => app => event => callback(app)
const onApp = (app, handleActivate, handleBlur) => {
app.on('activate', handleActivate)
applyConfig(app, handleBlur)
// TODO: Is there a way to unsubscribe within dispose?
app.config.subscribe(() => applyConfig(app, handleBlur))
return app.config.subscribe(() => applyConfig(app, handleBlur))
}

module.exports = {
Expand Down
5 changes: 4 additions & 1 deletion modules/dispose.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// const { unregisterShortcut } = require('hyperterm-register-shortcut')
const { showWindows } = require('./windows')

module.exports = (app, handleActivate, handleBlur) => {
module.exports = (app, callbacks) => {
const { handleActivate, handleBlur, cfgUnsubscribe } = callbacks

showWindows(app)
app.dock.show()
// TODO: Un-register shortcut when supported
// unregisterShortcut()
cfgUnsubscribe()
app.removeListener('activate', handleActivate)
app.removeListener('browser-window-blur', handleBlur)
}

0 comments on commit f800f87

Please sign in to comment.