Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save imaustink/58139ef398e6e11e4bc591711bf465a0 to your computer and use it in GitHub Desktop.
Save imaustink/58139ef398e6e11e4bc591711bf465a0 to your computer and use it in GitHub Desktop.
user-media-selector component connectedCallback
export const ViewModel = DefineMap.extend({
// Setup and teardown
// This is called when the component is inserted into the DOM
connectedCallback () {
const deviceChangeHandler = () => {
navigator.mediaDevices.enumerateDevices()
.then(devices => (this.devices = devices))
.catch(error => console.error(error))
}
deviceChangeHandler()
navigator.mediaDevices.addEventListener('devicechange', deviceChangeHandler)
this.listenTo('constraints', (event, constraints) => {
if (constraints.video || constraints.audio) {
navigator.mediaDevices.getUserMedia(constraints)
.then(stream => (this.previewStream = stream))
.catch(error => console.error(error))
}
})
// This is called when the component is removed from the DOM
return () => {
navigator.mediaDevices.removeEventListener('devicechange', deviceChangeHandler)
this.stopListening()
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment