OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 * Javascript for Device-related features, served from | |
7 * chrome://bluetooth-internals/. | |
8 */ | |
9 | |
10 cr.define('device_collection', function() { | |
dpapad
2016/11/04 17:20:00
Shouldn't all namespaces for code under bluetooth_
mbrunson
2016/11/04 22:57:40
I mainly did this for ease of use in other files.
dpapad
2016/11/04 23:43:51
I see, there is 3 competing patterns for namespace
| |
11 /* | |
12 * Collection of devices. | |
13 * @constructor | |
14 * @param {!Array} array the starting collection of devices. | |
dpapad
2016/11/04 17:20:00
s/the/The
mbrunson
2016/11/04 22:57:41
Done.
| |
15 * @extends {cr.ui.ArrayDataModel} | |
16 */ | |
17 var DeviceCollection = function(array) { | |
18 cr.ui.ArrayDataModel.call(this, array); | |
dpapad
2016/11/04 17:20:00
Can you explain a bit why you need to extend Array
mbrunson
2016/11/04 22:57:41
ArrayDataModel provides a set of functions and eve
| |
19 }; | |
20 DeviceCollection.prototype = { | |
21 __proto__: cr.ui.ArrayDataModel.prototype, | |
22 | |
23 /** | |
24 * Finds the Device in the collection with the matching address. | |
25 * @param {!string} address | |
26 */ | |
27 getByAddress: function(address) { | |
28 for (var i = 0; i < this.length; i++) { | |
29 var device = this.item(i); | |
30 if (address == device.info.address) | |
31 return device; | |
32 } | |
33 return null; | |
34 }, | |
35 | |
36 /** | |
37 * Adds or updates a Device with new DeviceInfo. | |
38 * @param {!interfaces.BluetoothDevice.DeviceInfo} deviceInfo | |
39 */ | |
40 addOrUpdate: function(deviceInfo) { | |
41 var oldDevice = this.getByAddress(deviceInfo.address); | |
42 if (oldDevice) { | |
43 // Update rssi if it's valid | |
44 var rssi = (deviceInfo.rssi && deviceInfo.rssi.value) || | |
45 (oldDevice.info.rssi && oldDevice.info.rssi.value); | |
46 | |
47 oldDevice.info = deviceInfo; | |
48 oldDevice.info.rssi = { value: rssi }; | |
49 oldDevice.removed = false; | |
50 | |
51 this.updateIndex(this.indexOf(oldDevice)); | |
52 } else { | |
53 this.push(new Device(deviceInfo)); | |
54 } | |
55 }, | |
56 | |
57 /** | |
58 * Marks the Device as removed. | |
59 * @param {!interfaces.bluetoothDevice.DeviceInfo} deviceInfo | |
60 */ | |
61 remove: function(deviceInfo) { | |
62 var device = this.getByAddress(deviceInfo.address); | |
63 assert(device, 'Device does not exist.'); | |
64 device.removed = true; | |
65 this.updateIndex(this.indexOf(device)); | |
66 } | |
67 }; | |
68 | |
69 /* | |
70 * Data model for a cached device. | |
71 * @constructor | |
72 * @param {!interfaces.BluetoothDevice.DeviceInfo} info | |
73 */ | |
74 var Device = function(info) { | |
75 this.info = info; | |
76 this.removed = false; | |
77 }; | |
78 | |
79 return { | |
80 DeviceCollection: DeviceCollection, | |
81 }; | |
82 }); | |
OLD | NEW |