From a4e51948c9ffe110cd942b236c7b1a440b36b80f Mon Sep 17 00:00:00 2001 From: maverickmishra Date: Tue, 14 Nov 2017 13:55:53 -0800 Subject: [PATCH] :hammer:Issue #9 Refactored pre-fetch implementation for devices and constraints --- www/mediadevices.js | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/www/mediadevices.js b/www/mediadevices.js index 2fa6384..2b6fb3d 100644 --- a/www/mediadevices.js +++ b/www/mediadevices.js @@ -21,9 +21,10 @@ /* globals Promise, cordova, MediaStream */ var exec = cordova.require('cordova/exec'); var channel = require('cordova/channel'); - +var flagConstraints = true; +var flagDevices = true; var mediaDevices = { - _devices: null, + _devices: [], _supportedConstraints: { width: true, height: true, @@ -42,13 +43,29 @@ var mediaDevices = { }; mediaDevices.getSupportedConstraints = function () { - return this._supportedConstraints; + var successConstraints = function (constraints) { + mediaDevices._supportedConstraints = constraints; + flagConstraints = false; + }; + if (!flagConstraints) { + return this._supportedConstraints; + } else { + exec(successConstraints, null, 'Stream', 'getSupportedConstraints', []); + } }; mediaDevices.enumerateDevices = function () { var that = this; return new Promise(function (resolve, reject) { - resolve(that._devices); + var successDevices = function (device) { + mediaDevices._devices = device.devices; + flagDevices = false; + }; + if (!flagDevices) { + resolve(that._devices); + } else { + exec(successDevices, null, 'Stream', 'enumerateDevices', []); + } }); }; @@ -73,15 +90,16 @@ mediaDevices.getUserMedia = function (constraints) { }; channel.onCordovaReady.subscribe(function () { - var success = function (value) { - if ('devices' in value) { - mediaDevices._devices = value; - } else { - mediaDevices._supportedConstraints = value; - } + var successConstraints = function (constraints) { + mediaDevices._supportedConstraints = constraints; + flagConstraints = false; + }; + var successDevices = function (device) { + mediaDevices._devices = device.devices; + flagDevices = false; }; - exec(success, null, 'Stream', 'getSupportedConstraints', []); - exec(success, null, 'Stream', 'enumerateDevices', []); + exec(successConstraints, null, 'Stream', 'getSupportedConstraints', []); + exec(successDevices, null, 'Stream', 'enumerateDevices', []); }); module.exports = mediaDevices;