I'm running the following code in Chrome and I'm a little confused as to why oldCurrent seems to reference currentStats.
I create a new object called oldCurrent and use jQuery.extend
to copy the properties of the currentStats object. Then I attempt to copy the new data into currentStats object, to overwrite it with the properties of the new statsReport.audio property, also with jQuery.extend
.
If I stop the debugger at this point, I can see that all of a sudden oldCurrent has all of the values that currentStats has, even though currentStats was written to after I copied into oldCurrent.
If jQuery.extend()
is making a deep copy of the currentStats object, then why would oldCurrent suddenly contain all those values, as if it were a reference to currentStats?
The peer object is created from createPeerConn( ). The statsReport data could be any object. I know that the audio and video properties exist in statsData and are correct.
function createPeerConn() {
var peer = {
conn: null,
remotePeerID: "",
statsIntervalID: -1,
stats: {
count: 0,
prev: createStatsObj(),
current: createStatsObj(),
delta: createStatsObj()
},
};
return peer;
}
function StreamStats()
{
this.bytesSent = 0;
this.packetsLost = 0;
return this;
}
function createStatsObj()
{
var a = new StreamStats();
var v = new StreamStats();
return { audio: a, video: v };
}
if ( statsReport ) {
// 0. Copy the existing current values from the stats because we need to update
// the previous stats to the existing current.
var currentStats = peer.stats.current;
var oldCurrent = $.extend(true, {}, currentStats);
// 1. Copy the current stats to the current object.
// console.log("updateStats: peer: %o statsReport: %o", peer, statsReport);
$.extend(true, currentStats.audio, statsReport.audio);
$.extend(true, currentStats.video, statsReport.video);
if ( currentStats.audio.bytesSent != statsReport.audio.bytesSent || currentStats.audio.packetsLost != statsReport.audio.packetsLost ) {
console.log("bytesSent is not equal. %s, %s", currentStats.audio.bytesSent, statsReport.audio.bytesSent);
console.log("packetsLost is not equal copy: %s original: %s", currentStats.audio.packetsLost, statsReport.audio.packetsLost);
} else {
console.log("equal: bytesSent copy: %s original: %s", currentStats.audio.bytesSent, statsReport.audio.bytesSent);
console.log("equal: packetsLost copy: %s original: %s", currentStats.audio.packetsLost, statsReport.audio.packetsLost);
}
// 3. Crunch the deltas.
var prevStats = peer.stats.prev;
var delta = peer.stats.delta;
delta.audio.bytesSent = currentStats.audio.bytesSent - prevStats.audio.bytesSent;
// 4. Update the previous stats to what current was before it was overwritten.
prevStats = oldCurrent;
}