Skip to content

Commit

Permalink
🔨Issue #9 Refactored pre-fetch implementation for devices and constra…
Browse files Browse the repository at this point in the history
…ints
  • Loading branch information
maverickmishra committed Nov 14, 2017
1 parent 92309fe commit a4e5194
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions www/mediadevices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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', []);
}
});
};

Expand All @@ -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;

0 comments on commit a4e5194

Please sign in to comment.