OLD | NEW |
---|---|
1 'use strict'; | 1 'use strict'; |
2 | 2 |
3 // Wraps callback and calls reject_func if callback throws an error. | |
4 class CallbackWrapper { | |
5 constructor(callback, reject_func) { | |
6 this.wrapper_func_ = (args) => { | |
7 try { | |
8 callback(args); | |
9 } catch(e) { | |
10 reject_func(); | |
11 } | |
12 } | |
13 } | |
14 | |
15 callback() { | |
Mikhail
2016/11/08 09:04:16
Could you provide more readable name for it, for e
| |
16 return this.wrapper_func_; | |
17 } | |
18 } | |
19 | |
3 function sensor_mocks(mojo) { | 20 function sensor_mocks(mojo) { |
4 return define('Generic Sensor API mocks', [ | 21 return define('Generic Sensor API mocks', [ |
5 'mojo/public/js/core', | 22 'mojo/public/js/core', |
6 'mojo/public/js/bindings', | 23 'mojo/public/js/bindings', |
7 'mojo/public/js/connection', | 24 'mojo/public/js/connection', |
8 'device/generic_sensor/public/interfaces/sensor_provider.mojom', | 25 'device/generic_sensor/public/interfaces/sensor_provider.mojom', |
9 'device/generic_sensor/public/interfaces/sensor.mojom', | 26 'device/generic_sensor/public/interfaces/sensor.mojom', |
10 ], (core, bindings, connection, sensor_provider, sensor) => { | 27 ], (core, bindings, connection, sensor_provider, sensor) => { |
11 | 28 |
12 // Helper function that returns resolved promise with result. | 29 // Helper function that returns resolved promise with result. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
46 } | 63 } |
47 | 64 |
48 // Adds configuration for the sensor and starts reporting fake data | 65 // Adds configuration for the sensor and starts reporting fake data |
49 // through update_reading_function_ callback. | 66 // through update_reading_function_ callback. |
50 addConfiguration(configuration) { | 67 addConfiguration(configuration) { |
51 assert_not_equals(configuration, null, "Invalid sensor configuration."); | 68 assert_not_equals(configuration, null, "Invalid sensor configuration."); |
52 | 69 |
53 if (!this.start_should_fail_ && this.update_reading_function_ != null) { | 70 if (!this.start_should_fail_ && this.update_reading_function_ != null) { |
54 let timeout = (1 / configuration.frequency) * 1000; | 71 let timeout = (1 / configuration.frequency) * 1000; |
55 this.sensor_reading_timer_id_ = window.setTimeout(() => { | 72 this.sensor_reading_timer_id_ = window.setTimeout(() => { |
56 this.update_reading_function_(this.buffer_); | 73 if (this.update_reading_function_) |
74 this.update_reading_function_(this.buffer_); | |
57 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) { | 75 if (this.reporting_mode_ === sensor.ReportingMode.ON_CHANGE) { |
58 this.client_.sensorReadingChanged(); | 76 this.client_.sensorReadingChanged(); |
59 } | 77 } |
60 }, timeout); | 78 }, timeout); |
61 } | 79 } |
62 | 80 |
63 this.active_sensor_configurations_.push(configuration); | 81 this.active_sensor_configurations_.push(configuration); |
64 | 82 |
65 if (this.add_configuration_called_ != null) | 83 if (this.add_configuration_called_ != null) |
66 this.add_configuration_called_(this); | 84 this.add_configuration_called_(this); |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
315 }; | 333 }; |
316 | 334 |
317 let onFailure = () => { | 335 let onFailure = () => { |
318 sensor.mockSensorProvider.reset(); | 336 sensor.mockSensorProvider.reset(); |
319 return new Promise((resolve, reject) => { setTimeout(reject, 0); }); | 337 return new Promise((resolve, reject) => { setTimeout(reject, 0); }); |
320 }; | 338 }; |
321 | 339 |
322 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); | 340 return Promise.resolve(func(sensor)).then(onSuccess, onFailure); |
323 }), name, properties); | 341 }), name, properties); |
324 } | 342 } |
OLD | NEW |