OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <!-- | 2 <!-- |
3 Copyright 2016 The Chromium Authors. All rights reserved. | 3 Copyright 2016 The Chromium Authors. All rights reserved. |
4 Use of this source code is governed by a BSD-style license that can be | 4 Use of this source code is governed by a BSD-style license that can be |
5 found in the LICENSE file. | 5 found in the LICENSE file. |
6 --> | 6 --> |
7 | 7 |
8 <link rel="import" href="/tracing/value/diagnostics/diagnostic.html"> | 8 <link rel="import" href="/tracing/value/diagnostics/diagnostic.html"> |
9 | 9 |
10 <script> | 10 <script> |
11 'use strict'; | 11 'use strict'; |
12 | 12 |
13 tr.exportTo('tr.v.d', function() { | 13 tr.exportTo('tr.v.d', function() { |
14 class Breakdown extends tr.v.d.Diagnostic { | 14 class Breakdown extends tr.v.d.Diagnostic { |
15 constructor() { | 15 constructor() { |
16 super(); | 16 super(); |
17 this.values_ = new Map(); | 17 this.values_ = new Map(); |
18 this.colorScheme = undefined; | 18 this.colorScheme = undefined; |
19 } | 19 } |
20 | 20 |
| 21 get size() { |
| 22 return this.values_.size; |
| 23 } |
| 24 |
21 clone() { | 25 clone() { |
22 const clone = new Breakdown(); | 26 const clone = new Breakdown(); |
23 clone.colorScheme = this.colorScheme; | 27 clone.colorScheme = this.colorScheme; |
24 clone.addDiagnostic(this); | 28 clone.addDiagnostic(this); |
25 return clone; | 29 return clone; |
26 } | 30 } |
27 | 31 |
28 canAddDiagnostic(otherDiagnostic) { | 32 canAddDiagnostic(otherDiagnostic) { |
29 return ((otherDiagnostic instanceof Breakdown) && | 33 return ((otherDiagnostic instanceof Breakdown) && |
30 (otherDiagnostic.colorScheme === this.colorScheme)); | 34 (otherDiagnostic.colorScheme === this.colorScheme)); |
31 } | 35 } |
32 | 36 |
33 addDiagnostic(otherDiagnostic) { | 37 addDiagnostic(otherDiagnostic) { |
34 for (const [name, value] of otherDiagnostic) { | 38 for (const [name, value] of otherDiagnostic) { |
35 this.set(name, this.get(name) + value); | 39 this.set(name, this.get(name) + value); |
36 } | 40 } |
37 return this; | 41 return this; |
38 } | 42 } |
39 | 43 |
40 /** | 44 /** |
41 * Add a Value by an explicit name to this map. | 45 * Add a numeric value by name. |
42 * | 46 * |
43 * @param {string} name | 47 * @param {string} name |
44 * @param {number} value | 48 * @param {number} value |
45 */ | 49 */ |
46 set(name, value) { | 50 set(name, value) { |
47 if (typeof name !== 'string' || | 51 if (typeof name !== 'string' || |
48 typeof value !== 'number') { | 52 typeof value !== 'number') { |
49 throw new Error('Breakdown maps from strings to numbers'); | 53 throw new Error('Breakdown maps from strings to numbers'); |
50 } | 54 } |
| 55 |
51 this.values_.set(name, value); | 56 this.values_.set(name, value); |
52 } | 57 } |
53 | 58 |
54 /** | 59 /** |
55 * @param {string} name | 60 * @param {string} name |
56 * @return {number} | 61 * @return {number} |
57 */ | 62 */ |
58 get(name) { | 63 get(name) { |
59 return this.values_.get(name) || 0; | 64 return this.values_.get(name) || 0; |
60 } | 65 } |
61 | 66 |
62 * [Symbol.iterator]() { | 67 * [Symbol.iterator]() { |
63 for (const pair of this.values_) { | 68 for (const pair of this.values_) { |
64 yield pair; | 69 yield pair; |
65 } | 70 } |
66 } | 71 } |
67 | 72 |
68 asDictInto_(d) { | 73 asDictInto_(d) { |
69 d.values = {}; | 74 d.entries = Array.from(this); |
70 for (const [name, value] of this) { | 75 if (this.colorScheme) d.colorScheme = this.colorScheme; |
71 d.values[name] = tr.b.numberToJson(value); | 76 } |
| 77 |
| 78 static fromEntries(entries) { |
| 79 const breakdown = new Breakdown(); |
| 80 for (const [name, value] of entries) { |
| 81 breakdown.set(name, value); |
72 } | 82 } |
73 if (this.colorScheme) { | 83 return breakdown; |
74 d.colorScheme = this.colorScheme; | |
75 } | |
76 } | 84 } |
77 | 85 |
78 static fromDict(d) { | 86 static fromDict(d) { |
79 const breakdown = new Breakdown(); | 87 const breakdown = Breakdown.fromEntries(d.entries); |
80 for (const [name, value] of Object.entries(d.values)) { | 88 if (d.colorScheme) breakdown.colorScheme = d.colorScheme; |
81 breakdown.set(name, tr.b.numberFromJson(value)); | |
82 } | |
83 if (d.colorScheme) { | |
84 breakdown.colorScheme = d.colorScheme; | |
85 } | |
86 return breakdown; | 89 return breakdown; |
87 } | 90 } |
88 } | 91 } |
89 | 92 |
90 tr.v.d.Diagnostic.register(Breakdown, { | 93 tr.v.d.Diagnostic.register(Breakdown, { |
91 elementName: 'tr-v-ui-breakdown-span' | 94 elementName: 'tr-v-ui-breakdown-span' |
92 }); | 95 }); |
93 | 96 |
94 return { | 97 return { |
95 Breakdown, | 98 Breakdown, |
96 }; | 99 }; |
97 }); | 100 }); |
98 </script> | 101 </script> |
OLD | NEW |