Created
August 5, 2018 04:56
-
-
Save imaustink/58139ef398e6e11e4bc591711bf465a0 to your computer and use it in GitHub Desktop.
user-media-selector component connectedCallback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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