Chromium Code Reviews| Index: third_party/WebKit/Source/devtools/front_end/common/List.js |
| diff --git a/third_party/WebKit/Source/devtools/front_end/common/List.js b/third_party/WebKit/Source/devtools/front_end/common/List.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..12257a8858ab8455b876423ddfe944755483048f |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/devtools/front_end/common/List.js |
| @@ -0,0 +1,140 @@ |
| +// Copyright 2017 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. |
| + |
| +/** |
| + * @template T |
| + */ |
| +Common.List = class extends Common.Object { |
|
pfeldman
2017/05/31 23:00:02
UI.ListModel
dgozman
2017/06/01 01:03:02
Done.
|
| + /** |
| + * @param {!Array<T>=} items |
| + */ |
| + constructor(items) { |
| + super(); |
| + this._items = items || []; |
| + } |
| + |
| + /** |
| + * @return {number} |
| + */ |
| + length() { |
| + return this._items.length; |
| + } |
| + |
| + /** |
| + * @param {number} index |
| + * @return {T} |
| + */ |
| + itemAtIndex(index) { |
| + return this._items[index]; |
| + } |
| + |
| + /** |
| + * @param {T} item |
| + */ |
| + pushItem(item) { |
| + this.replaceItemsInRange(this._items.length, this._items.length, [item]); |
| + } |
| + |
| + /** |
| + * @return {T} |
| + */ |
| + popItem() { |
| + return this.removeItemAtIndex(this._items.length - 1); |
| + } |
| + |
| + /** |
| + * @param {number} index |
| + * @param {T} item |
| + */ |
| + insertItemAtIndex(index, item) { |
|
einbinder
2017/05/31 23:23:08
I don't think this is good to have if we want a so
dgozman
2017/06/01 01:03:02
Yeah, we'll figure it out when the need arises. Fo
|
| + this.replaceItemsInRange(index, index, [item]); |
| + } |
| + |
| + /** |
| + * @param {T} item |
| + * @param {function(T, T):number} comparator |
| + */ |
| + insertItemWithComparator(item, comparator) { |
| + // TODO(dgozman): move this to sorted list. |
| + var index = this._items.lowerBound(item, comparator); |
| + this.insertItemAtIndex(index, item); |
| + } |
| + |
| + /** |
| + * @param {T} item |
| + * @return {number} |
| + */ |
| + indexOfItem(item) { |
| + return this._items.indexOf(item); |
| + } |
| + |
| + /** |
| + * @param {number} index |
| + * @return {T} |
| + */ |
| + removeItemAtIndex(index) { |
| + var result = this._items[index]; |
| + this.replaceItemsInRange(index, index + 1, []); |
| + return result; |
| + } |
| + |
| + /** |
| + * @param {T} item |
| + */ |
| + removeItem(item) { |
| + var index = this._items.indexOf(item); |
| + if (index === -1) { |
| + console.error('Attempt to remove non-existing item'); |
| + return; |
| + } |
| + this.removeItemAtIndex(index); |
| + } |
| + |
| + /** |
| + * @param {!Array<T>} items |
| + */ |
| + replaceAllItems(items) { |
| + this.replaceItemsInRange(0, this._items.length, items); |
| + } |
| + |
| + /** |
| + * @param {number} index |
| + * @param {T} item |
| + */ |
| + replaceItemAtIndex(index, item) { |
| + this.replaceItemsInRange(index, index + 1, [item]); |
| + } |
| + |
| + /** |
| + * @param {function(T):boolean} callback |
| + * @return {number} |
| + */ |
| + findIndex(callback) { |
| + return this._items.findIndex(callback); |
| + } |
| + |
| + /** |
| + * @param {number} from |
| + * @param {number} to |
| + * @param {!Array<T>} items |
| + */ |
| + replaceItemsInRange(from, to, items) { |
| + var removed = this._items.slice(from, to); |
| + if (items.length < 10000) { |
| + this._items.splice.bind(this._items, from, to - from).apply(null, items); |
| + } else { |
| + // Splice may fail with too many arguments. |
| + var before = this._items.slice(0, from); |
| + var after = this._items.slice(to); |
| + this._items = [].concat(before, items, after); |
| + } |
| + this.dispatchEventToListeners( |
| + Common.List.Events.ItemsReplaced, {index: from, removed: removed, inserted: items.length}); |
| + } |
| +}; |
| + |
| +/** @enum {symbol} */ |
| +Common.List.Events = { |
| + ItemsReplaced: Symbol('ItemsReplaced'), |
| +}; |