OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 |
| 6 /** |
| 7 * @fileoverview A class for keeping track of the details of a player. |
| 8 */ |
| 9 |
| 10 var PlayerInfo = (function () { |
| 11 "use strict"; |
| 12 |
| 13 /** |
| 14 * A class that keeps track of properties on a media player. |
| 15 * @param id A unique id that can be used to identify this player. |
| 16 * @constructor |
| 17 */ |
| 18 function PlayerInfo(id) { |
| 19 this.id = id; |
| 20 // The current value of the properties for this player |
| 21 this.properties = {}; |
| 22 // All of the past (and present) values of the properties |
| 23 this.pastValues = {}; |
| 24 |
| 25 // Every single event in the order in which they were received |
| 26 this.allEvents = []; |
| 27 this.lastRendered = 0; |
| 28 |
| 29 this.firstTimestamp = -1; |
| 30 } |
| 31 |
| 32 |
| 33 /** |
| 34 * Adds or set a property on this player. |
| 35 * This is the default logging method as it keeps track of old values |
| 36 * @param timestamp The time in milliseconds since the Epoch |
| 37 * @param key A String key that describes the property |
| 38 * @param value The value of the property. |
| 39 */ |
| 40 PlayerInfo.prototype.addProperty = function (timestamp, key, value) { |
| 41 // The first timestamp that we get will be recorded. |
| 42 // Then, all future timestamps are deltas of that. |
| 43 if (this.firstTimestamp === -1) { |
| 44 this.firstTimestamp = timestamp; |
| 45 } |
| 46 |
| 47 if (typeof key !== 'string') { |
| 48 throw new Error(typeof key + " is not a valid key type"); |
| 49 } |
| 50 |
| 51 this.properties[key] = value; |
| 52 |
| 53 if (!this.pastValues[key]) { |
| 54 // Preallocate for performance |
| 55 this.pastValues[key] = new Array(512); |
| 56 } |
| 57 |
| 58 var recordValue = { |
| 59 time: timestamp - this.firstTimestamp, |
| 60 key: key, |
| 61 value: value |
| 62 }; |
| 63 |
| 64 this.pastValues[key].push(recordValue); |
| 65 this.allEvents.push(recordValue); |
| 66 }; |
| 67 |
| 68 |
| 69 /** |
| 70 * Adds or set a property on this player. |
| 71 * Does not keep track of old values. This is better for |
| 72 * values that get spammed repeatedly. |
| 73 * @param timestamp The time in milliseconds since the Epoch |
| 74 * @param key A String key that describes the property |
| 75 * @param value The value of the property. |
| 76 */ |
| 77 PlayerInfo.prototype.addPropertyNoRecord = function (timestamp, key, value) { |
| 78 this.addProperty(timestamp, key, value); |
| 79 this.allEvents.pop(); |
| 80 }; |
| 81 |
| 82 return PlayerInfo; |
| 83 }()); |
OLD | NEW |