Skip to content

Commit

Permalink
feat: Realtime init returns terminate function
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed May 27, 2019
1 parent 445a527 commit 1a9a366
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/lib/realtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const APPS_DOCTYPE = 'io.cozy.apps'
/**
* Initialize realtime sockets
*
* Returns function that closes the realtime and unsubscribe handlers
*
* @private
* @param {object}
* @returns {Promise}
Expand All @@ -30,13 +32,22 @@ function initializeRealtime({ getApp, onCreate, onDelete, cozyClient }) {
}
}

let realtime
try {
const realtime = new CozyRealtime({ cozyClient })
realtime = new CozyRealtime({ cozyClient })
realtime.subscribe('created', APPS_DOCTYPE, handleAppCreation)
realtime.subscribe('deleted', APPS_DOCTYPE, handleAppRemoval)
} catch (error) {
console.warn(`Cannot initialize realtime in Cozy-bar: ${error.message}`)
}

return () => {
if (!realtime) {
return
}
realtime.unsubscribe('created', APPS_DOCTYPE, handleAppCreation)
realtime.unsubscribe('deleted', APPS_DOCTYPE, handleAppRemoval)
}
}

export default initializeRealtime

0 comments on commit 1a9a366

Please sign in to comment.