Commit 1608dcb2 authored by François Degros's avatar François Degros Committed by Commit Bot

[Files app] VolumeManagerImpl.initialize() returns a Promise<void>

Change-Id: I739a6ed2a754a17f3899e3b5716f0d0497cb2470
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1621790Reviewed-by: default avatarLuciano Pacheco <lucmult@chromium.org>
Commit-Queue: François Degros <fdegros@chromium.org>
Auto-Submit: François Degros <fdegros@chromium.org>
Cr-Commit-Position: refs/heads/master@{#661978}
parent ed5b1b34
......@@ -25,8 +25,7 @@ var volumeManagerFactory = (() => {
async function getInstance() {
if (!instance) {
instance = new VolumeManagerImpl();
instanceInitialized =
new Promise(fulfill => instance.initialize(fulfill));
instanceInitialized = instance.initialize();
}
await instanceInitialized;
return instance;
......
......@@ -23,8 +23,7 @@ class VolumeManagerImpl extends cr.EventTarget {
/**
* Queue for mounting.
* @type {AsyncUtil.Queue}
* @private
* @private @const {AsyncUtil.Queue}
*/
this.mountQueue_ = new AsyncUtil.Queue();
......@@ -119,36 +118,38 @@ class VolumeManagerImpl extends cr.EventTarget {
/**
* Initializes mount points.
* @param {function()} callback Called upon the completion of the
* initialization.
* @return {!Promise<void>}
*/
initialize(callback) {
chrome.fileManagerPrivate.onMountCompleted.addListener(
this.onMountCompleted_.bind(this));
console.warn('Getting volume list');
chrome.fileManagerPrivate.getVolumeMetadataList(volumeMetadataList => {
console.warn(`There are ${volumeMetadataList.length} volumes`);
// We must subscribe to the mount completed event in the callback of
// getVolumeMetadataList. crbug.com/330061.
// But volumes reported by onMountCompleted events must be added after the
// volumes in the volumeMetadataList are mounted. crbug.com/135477.
this.mountQueue_.run(async (inCallback) => {
try {
// Create VolumeInfo for each volume.
await Promise.all(volumeMetadataList.map(async (volumeMetadata) => {
console.warn(`Initializing volume ${volumeMetadata.volumeId}`);
const volumeInfo = await this.addVolumeMetadata_(volumeMetadata);
console.warn(`Initialized volume ${volumeInfo.volumeId}`);
}));
console.warn('Initialized all volumes');
} finally {
// Call the callback of the initialize function.
callback();
// Call the callback of AsyncQueue. Maybe it invokes callbacks
// registered by mountCompleted events.
inCallback();
}
initialize() {
return new Promise((resolve, reject) => {
chrome.fileManagerPrivate.onMountCompleted.addListener(
this.onMountCompleted_.bind(this));
console.warn('Getting volume list');
chrome.fileManagerPrivate.getVolumeMetadataList(volumeMetadataList => {
console.warn(`There are ${volumeMetadataList.length} volumes`);
// We must subscribe to the mount completed event in the callback of
// getVolumeMetadataList (crbug.com/330061). But volumes reported by
// onMountCompleted events must be added after the volumes in the
// volumeMetadataList are mounted (crbug.com/135477).
this.mountQueue_.run(async (done) => {
try {
// Create VolumeInfo for each volume.
await Promise.all(volumeMetadataList.map(async (volumeMetadata) => {
console.warn(`Initializing volume ${volumeMetadata.volumeId}`);
const volumeInfo = await this.addVolumeMetadata_(volumeMetadata);
console.warn(`Initialized volume ${volumeInfo.volumeId}`);
}));
console.warn('Initialized all volumes');
resolve();
} catch (e) {
reject(e);
} finally {
// Call the callback of AsyncQueue. Maybe it invokes callbacks
// registered by mountCompleted events.
done();
}
});
});
});
}
......@@ -160,7 +161,7 @@ class VolumeManagerImpl extends cr.EventTarget {
* @private
*/
onMountCompleted_(event) {
this.mountQueue_.run(async (callback) => {
this.mountQueue_.run(async (done) => {
try {
switch (event.eventType) {
case 'mount':
......@@ -212,7 +213,7 @@ class VolumeManagerImpl extends cr.EventTarget {
break;
}
} finally {
callback();
done();
}
});
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment