Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * Class representing the results of a scan operation. | 6 * Class representing the results of a scan operation. |
| 7 * | 7 * |
| 8 * @interface | 8 * @interface |
| 9 */ | 9 */ |
| 10 importer.MediaScanner = function() {}; | 10 importer.MediaScanner = function() {}; |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 128 } | 128 } |
| 129 }; | 129 }; |
| 130 | 130 |
| 131 /** @override */ | 131 /** @override */ |
| 132 importer.DefaultMediaScanner.prototype.scan = function(entries) { | 132 importer.DefaultMediaScanner.prototype.scan = function(entries) { |
| 133 if (entries.length == 0) { | 133 if (entries.length == 0) { |
| 134 throw new Error('Cannot scan empty list of entries.'); | 134 throw new Error('Cannot scan empty list of entries.'); |
| 135 } | 135 } |
| 136 | 136 |
| 137 var scanResult = this.createScanResult_(); | 137 var scanResult = this.createScanResult_(); |
| 138 var watcher = this.watcherFactory_(function() { | 138 var watcher = this.watcherFactory_( |
| 139 scanResult.invalidateScan(); | 139 /** @this {importer.DefaultMediaScanner} */ |
| 140 this.observers_.forEach( | 140 function() { |
| 141 /** @param {!importer.ScanObserver} observer */ | 141 scanResult.invalidateScan(); |
| 142 function(observer) { | 142 this.observers_.forEach( |
| 143 observer(importer.ScanEvent.INVALIDATED, scanResult); | 143 /** @param {!importer.ScanObserver} observer */ |
| 144 }); | 144 function(observer) { |
| 145 }.bind(this)); | 145 observer(importer.ScanEvent.INVALIDATED, scanResult); |
| 146 }); | |
| 147 }.bind(this)); | |
| 146 var scanPromises = entries.map( | 148 var scanPromises = entries.map( |
| 147 this.scanEntry_.bind(this, scanResult, watcher)); | 149 this.scanEntry_.bind(this, scanResult, watcher)); |
| 148 | 150 |
| 149 Promise.all(scanPromises) | 151 Promise.all(scanPromises) |
| 150 .then(scanResult.resolveScan.bind(scanResult)) | 152 .then(scanResult.resolve) |
| 151 .catch(scanResult.rejectScan.bind(scanResult)); | 153 .catch(scanResult.reject); |
| 152 | 154 |
| 153 scanResult.whenFinal() | 155 scanResult.whenFinal() |
| 154 .then( | 156 .then( |
| 155 function() { | 157 function() { |
| 156 this.onScanFinished_(scanResult); | 158 this.onScanFinished_(scanResult); |
| 157 }.bind(this)); | 159 }.bind(this)); |
| 158 | 160 |
| 159 return scanResult; | 161 return scanResult; |
| 160 }; | 162 }; |
| 161 | 163 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 * @type {Date} | 274 * @type {Date} |
| 273 */ | 275 */ |
| 274 this.scanStarted_ = new Date(); | 276 this.scanStarted_ = new Date(); |
| 275 | 277 |
| 276 /** | 278 /** |
| 277 * The point in time when the last scan activity occured. | 279 * The point in time when the last scan activity occured. |
| 278 * @type {Date} | 280 * @type {Date} |
| 279 */ | 281 */ |
| 280 this.lastScanActivity_ = this.scanStarted_; | 282 this.lastScanActivity_ = this.scanStarted_; |
| 281 | 283 |
| 282 /** @private {boolean} */ | 284 /** @private {!importer.Resolver.<!importer.ScanResult>} */ |
| 283 this.settled_ = false; | 285 this.resolver_ = new importer.Resolver(); |
| 286 }; | |
| 284 | 287 |
| 285 /** @type {function()} */ | 288 /** @struct */ |
| 286 this.resolveScan; | 289 importer.DefaultScanResult.prototype = { |
| 287 | 290 |
| 288 /** @type {function(*)} */ | 291 /** @return {function()} */ |
|
Ben Kwa
2015/01/30 23:13:09
I think this is the reason your refactoring addres
Steve McKay
2015/01/30 23:18:02
But the issue was only when I had individually sel
| |
| 289 this.rejectScan; | 292 get resolve() { return this.resolver_.resolve.bind(null, this); }, |
| 290 | 293 |
| 291 /** @private {!Promise.<!importer.ScanResult>} */ | 294 /** @return {function(*=)} */ |
| 292 this.finishedPromise_ = new Promise( | 295 get reject() { return this.resolver_.reject; } |
| 293 function(resolve, reject) { | |
| 294 this.resolveScan = function() { | |
| 295 this.settled_ = true; | |
| 296 resolve(this); | |
| 297 }.bind(this); | |
| 298 | |
| 299 this.rejectScan = function() { | |
| 300 this.settled_ = true; | |
| 301 reject(); | |
| 302 }; | |
| 303 }.bind(this)); | |
| 304 }; | 296 }; |
| 305 | 297 |
| 306 /** @override */ | 298 /** @override */ |
| 307 importer.DefaultScanResult.prototype.isFinal = function() { | 299 importer.DefaultScanResult.prototype.isFinal = function() { |
| 308 return this.settled_; | 300 return this.resolver_.settled; |
| 309 }; | 301 }; |
| 310 | 302 |
| 311 importer.DefaultScanResult.prototype.isInvalidated = function() { | 303 importer.DefaultScanResult.prototype.isInvalidated = function() { |
| 312 return this.invalidated_; | 304 return this.invalidated_; |
| 313 }; | 305 }; |
| 314 | 306 |
| 315 /** @override */ | 307 /** @override */ |
| 316 importer.DefaultScanResult.prototype.getFileEntries = function() { | 308 importer.DefaultScanResult.prototype.getFileEntries = function() { |
| 317 return this.fileEntries_; | 309 return this.fileEntries_; |
| 318 }; | 310 }; |
| 319 | 311 |
| 320 /** @override */ | 312 /** @override */ |
| 321 importer.DefaultScanResult.prototype.getTotalBytes = function() { | 313 importer.DefaultScanResult.prototype.getTotalBytes = function() { |
| 322 return this.totalBytes_; | 314 return this.totalBytes_; |
| 323 }; | 315 }; |
| 324 | 316 |
| 325 /** @override */ | 317 /** @override */ |
| 326 importer.DefaultScanResult.prototype.getScanDurationMs = function() { | 318 importer.DefaultScanResult.prototype.getScanDurationMs = function() { |
| 327 return this.lastScanActivity_.getTime() - this.scanStarted_.getTime(); | 319 return this.lastScanActivity_.getTime() - this.scanStarted_.getTime(); |
| 328 }; | 320 }; |
| 329 | 321 |
| 330 /** @override */ | 322 /** @override */ |
| 331 importer.DefaultScanResult.prototype.whenFinal = function() { | 323 importer.DefaultScanResult.prototype.whenFinal = function() { |
| 332 return this.finishedPromise_; | 324 return this.resolver_.promise; |
| 333 }; | 325 }; |
| 334 | 326 |
| 335 /** | 327 /** |
| 336 * Invalidates this scan. | 328 * Invalidates this scan. |
| 337 */ | 329 */ |
| 338 importer.DefaultScanResult.prototype.invalidateScan = function() { | 330 importer.DefaultScanResult.prototype.invalidateScan = function() { |
| 339 this.invalidated_ = true; | 331 this.invalidated_ = true; |
| 340 }; | 332 }; |
| 341 | 333 |
| 342 /** | 334 /** |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 498 if (!this.watchedDirectories_[event.entry.toURL()]) | 490 if (!this.watchedDirectories_[event.entry.toURL()]) |
| 499 return; | 491 return; |
| 500 this.triggered = true; | 492 this.triggered = true; |
| 501 for (var url in this.watchedDirectories_) { | 493 for (var url in this.watchedDirectories_) { |
| 502 chrome.fileManagerPrivate.removeFileWatch(url, function() {}); | 494 chrome.fileManagerPrivate.removeFileWatch(url, function() {}); |
| 503 } | 495 } |
| 504 chrome.fileManagerPrivate.onDirectoryChanged.removeListener( | 496 chrome.fileManagerPrivate.onDirectoryChanged.removeListener( |
| 505 assert(this.listener_)); | 497 assert(this.listener_)); |
| 506 this.callback_(); | 498 this.callback_(); |
| 507 }; | 499 }; |
| OLD | NEW |