};
-Client.prototype.mirrorObject = function(handle, cb) {
+Client.prototype.mirrorObject = function(handle, depth, cb) {
var self = this;
var val;
return;
}
- var mirror;
+ var mirror,
+ waiting = 1;
+
if (handle.className == 'Array') {
mirror = [];
} else {
mirror = {};
}
- for (var i = 0; i < handle.properties.length; i++) {
- var value = res.body[handle.properties[i].ref];
+
+ var keyValues = [];
+ handle.properties.forEach(function(prop, i) {
+ var value = res.body[prop.ref];
var mirrorValue;
if (value) {
mirrorValue = value.value ? value.value : value.text;
if (Array.isArray(mirror) &&
- typeof handle.properties[i].name != 'number') {
+ typeof prop.name != 'number') {
// Skip the 'length' property.
- continue;
+ return;
}
- mirror[handle.properties[i].name] = mirrorValue;
- }
+ keyValues[i] = {
+ name: prop.name,
+ value: mirrorValue
+ };
+ if (value.handle && depth > 0) {
+ waiting++;
+ self.mirrorObject(value, depth - 1, function(result) {
+ keyValues[i].value = result;
+ waitForOthers();
+ });
+ }
+ });
- if (cb) cb(mirror);
+ waitForOthers();
+ function waitForOthers() {
+ if (--waiting === 0 && cb) {
+ keyValues.forEach(function(kv) {
+ mirror[kv.name] = kv.value;
+ });
+ cb(mirror);
+ }
+ };
});
return;
} else if (handle.type === 'function') {
return;
}
- client.mirrorObject(res.body, function(mirror) {
+ client.mirrorObject(res.body, 3, function(mirror) {
callback(null, mirror);
self.resume(true);
});