From 1a2bc3a98f5331cb342d49b890139a72ddd5d194 Mon Sep 17 00:00:00 2001 From: futpib Date: Sun, 11 Nov 2018 13:52:32 +0300 Subject: [PATCH] Reconnect to pulse --- package.json | 3 ++- reducers/pulse.js | 2 ++ store/pulse-middleware.js | 19 ++++++++++++++++++- yarn.lock | 13 +++++++++---- 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index fe40db7..33b90ee 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ ] }, "dependencies": { - "@futpib/paclient": "^0.0.2", + "@futpib/paclient": "^0.0.3", + "bluebird": "^3.5.3", "camelcase": "^5.0.0", "electron-store": "^2.0.0", "freedesktop-icons": "^0.1.0", diff --git a/reducers/pulse.js b/reducers/pulse.js index 36b3098..325720b 100644 --- a/reducers/pulse.js +++ b/reducers/pulse.js @@ -43,6 +43,7 @@ const reducer = combineReducers({ } return omit([ payload.index ], state); }, + [pulse.close]: () => initialState.objects[key], }, initialState.objects[key]) ], things))), infos: combineReducers(fromPairs(map(({ key, type }) => [ key, handleActions({ @@ -60,6 +61,7 @@ const reducer = combineReducers({ [payload.index]: payload, }); }, + [pulse.close]: () => initialState.objects[key], }, initialState.infos[key]) ], things))), }); diff --git a/store/pulse-middleware.js b/store/pulse-middleware.js index ebf3a13..15b5fd9 100644 --- a/store/pulse-middleware.js +++ b/store/pulse-middleware.js @@ -1,4 +1,6 @@ +const Bluebird = require('bluebird'); + const PAClient = require('@futpib/paclient'); const { handleActions } = require('redux-actions'); @@ -62,6 +64,7 @@ module.exports = store => { }) .on('close', () => { store.dispatch(pulseActions.close()); + reconnect(); }) .on('new', (type, index) => { store.dispatch(pulseActions.new({ type, index })); @@ -73,9 +76,23 @@ module.exports = store => { }) .on('remove', (type, index) => { store.dispatch(pulseActions.remove({ type, index })); + }) + .on('error', error => { + console.error(error); }); - pa.connect(); + const reconnect = () => new Bluebird((resolve, reject) => { + pa.once('ready', resolve); + pa.once('error', reject); + pa.connect(); + }).catch(error => { + if (error.message === 'Unable to connect to PulseAudio server') { + return Bluebird.delay(5000).then(reconnect); + } + throw error; + }); + + reconnect(); const rethrow = error => { if (error) { diff --git a/yarn.lock b/yarn.lock index 14600f2..c15bd6c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -79,10 +79,10 @@ dependencies: arrify "^1.0.1" -"@futpib/paclient@^0.0.2": - version "0.0.2" - resolved "https://registry.yarnpkg.com/@futpib/paclient/-/paclient-0.0.2.tgz#de7d40014143a0aae8b3ceacd476cdbbaeefa5ec" - integrity sha512-ss0NdcFLcmgFi/vUDUgSHQTiZ+mumao9/heoOpuN9IskrsBQoMLr7eIm9odVnA+CwjdNxc2Cb899YiSSWoykaA== +"@futpib/paclient@^0.0.3": + version "0.0.3" + resolved "https://registry.yarnpkg.com/@futpib/paclient/-/paclient-0.0.3.tgz#54c10ac6d811c5104b66a9a4985809955c8ffee6" + integrity sha512-9OuBDQRb9U55y3Xu89tZV+oiwt2ghgTsSAqM+SAySoLCBt0yG5LaB9PCV+bxkBy6aY6bvvPuNrYd88hL57gaxQ== "@ladjs/time-require@^0.1.4": version "0.1.4" @@ -839,6 +839,11 @@ bluebird@^3.0.0: resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.2.tgz#1be0908e054a751754549c270489c1505d4ab15a" integrity sha512-dhHTWMI7kMx5whMQntl7Vr9C6BvV10lFXDAasnqnrMYhXVCzzk6IO9Fo2L75jXHT07WrOngL1WDXOp+yYS91Yg== +bluebird@^3.5.3: + version "3.5.3" + resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.3.tgz#7d01c6f9616c9a51ab0f8c549a79dfe6ec33efa7" + integrity sha512-/qKPUQlaW1OyR51WeCPBvRnAlnZFUJkCSG5HzGnuIqhgyJtF+T94lFnn33eiazjRm2LAHVy2guNnaq48X9SJuw== + body-parser@1.18.3, body-parser@^1.15.0: version "1.18.3" resolved "https://registry.yarnpkg.com/body-parser/-/body-parser-1.18.3.tgz#5b292198ffdd553b3a0f20ded0592b956955c8b4"