blob: 27dfa04f99fa05729f79adfd2f6be730cf9f96fa [file] [log] [blame]
<!DOCTYPE html>
<!--
Copyright 2016 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<link rel="import" href="/tracing/base/iteration_helpers.html">
<link rel="import" href="/tracing/value/diagnostics/diagnostic.html">
<link rel="import" href="/tracing/value/diagnostics/value_ref.html">
<script>
'use strict';
tr.exportTo('tr.v.d', function() {
class RelatedValueSet extends tr.v.d.Diagnostic {
constructor(opt_values) {
super();
this.valuesByGuid_ = new Map();
if (opt_values)
for (var value of opt_values)
this.add(value);
}
/**
* @param {!(tr.v.d.ValueRef|tr.v.Histogram)} v
*/
add(value) {
if (!(value instanceof tr.v.Histogram) &&
!(value instanceof tr.v.d.ValueRef))
throw new Error('Must be instanceof Histogram or ValueRef: ' + value);
if (this.valuesByGuid_.get(value.guid))
throw new Error('Tried to add same value twice');
this.valuesByGuid_.set(value.guid, value);
}
has(value) {
return this.valuesByGuid_.has(value.guid);
}
get length() {
return this.valuesByGuid_.size;
}
*[Symbol.iterator]() {
for (var [guid, value] of this.valuesByGuid_)
yield value;
}
/**
* Resolve all ValueRefs into Values by looking up their guids in
* |valueSet|.
* If a value cannot be found and |opt_required| is true, then throw an
* Error.
* If a value cannot be found and |opt_required| is false, then the ValueRef
* will remain a ValueRef.
*
* @param {!tr.v.ValueSet} valueSet
* @param {boolean=} opt_required
*/
resolve(valueSet, opt_required) {
for (var [guid, value] of this.valuesByGuid_) {
if (!(value instanceof tr.v.d.ValueRef))
continue;
value = valueSet.lookup(guid);
if (value instanceof tr.v.Histogram)
this.valuesByGuid_.set(guid, value);
else if (opt_required)
throw new Error('Unable to find Histogram ' + guid);
}
}
asDictInto_(d) {
d.guids = [];
for (var value of this)
d.guids.push(value.guid);
}
static fromDict(d) {
return new RelatedValueSet(d.guids.map(
guid => new tr.v.d.ValueRef(guid)));
}
}
tr.v.d.Diagnostic.register(RelatedValueSet, {
elementName: 'tr-v-ui-related-value-set-span'
});
return {
RelatedValueSet: RelatedValueSet,
};
});
</script>