Chromium Code Reviews| Index: chrome/browser/resources/media_internals/item_store.js |
| diff --git a/chrome/browser/resources/media_internals/item_store.js b/chrome/browser/resources/media_internals/item_store.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2cd934527abd3da8b857afbec5ed0865a6a2536b |
| --- /dev/null |
| +++ b/chrome/browser/resources/media_internals/item_store.js |
| @@ -0,0 +1,77 @@ |
| +// Copyright (c) 2011 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. |
| + |
| +cr.define('media', function() { |
| + |
| + /** |
| + * This class stores hashes by their id field and provides basic methods for |
| + * iterating over the collection. |
| + * |
| + * @constructor |
| + */ |
| + function ItemStore() { |
| + this.items_ = {}; |
| + }; |
| + |
| + ItemStore.prototype = { |
| + /** |
| + * Get a sorted list of item ids. |
| + * |
| + * @return {Array} A sorted array of ids. |
| + */ |
| + ids: function() { |
| + var ids = []; |
| + for (var i in this.items_) |
| + ids.push(i); |
| + return ids.sort(); |
| + }, |
| + |
| + /** |
| + * Add an item to the store. |
| + * |
| + * @param {Object} item The item to be added. |
| + * @param {string} item.id The id of the item. |
| + */ |
| + addItem: function(item) { |
| + this.items_[item.id] = item; |
| + }, |
| + |
| + /** |
| + * Add a dictionary of items to the store. |
| + * |
|
Evan Stade
2011/07/15 22:31:05
don't need blank line between description and para
Scott Franklin
2011/07/15 23:18:15
On a few with really long descriptions. It just fe
|
| + * @param {Object} items A dictionary of individual items. The keys are |
| + * irrelevant but each must have an id field. |
| + */ |
| + addItems: function(items) { |
| + for (id in items) |
| + this.addItem(items[id]); |
| + }, |
| + |
| + /** |
| + * Remove an item from the store. |
| + * |
| + * @param {string} id The id of the item to be removed. |
| + */ |
| + removeItem: function(id) { |
| + delete this.items_[id]; |
| + }, |
| + |
| + /** |
| + * Map this itemStore to an Array. Items are sorted by id. |
| + * |
| + * @param {function(*)} mapper The mapping function applied to each item. |
| + * @return {Array} An array of mapped items. |
|
Evan Stade
2011/07/15 22:31:05
extra space
Scott Franklin
2011/07/15 23:18:15
Done.
|
| + */ |
| + map: function(mapper) { |
| + var items = this.items_; |
| + var ids = this.ids(); |
| + return ids.map(function(id) { return mapper(items[id]); }); |
| + } |
| + }; |
| + |
| + return { |
| + ItemStore: ItemStore |
| + }; |
| + |
|
Evan Stade
2011/07/15 22:31:05
don't need this blank line
Scott Franklin
2011/07/15 23:18:15
Done.
|
| +}); |