File size: 981 Bytes
7aec436
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import ScratchStorage from 'scratch-storage';

class StorageWithProgress extends ScratchStorage {
  constructor () {
    super();

    this._totalAssets = 0;
    this._loadedAssets = 0;
  }

  _updateProgress () {
    if (this.onprogress) {
      this.onprogress(this._totalAssets, this._loadedAssets);
    }
  }

  load (assetType, asset, assetFormat) {
    const isAsset = (
      assetType === this.AssetType.ImageBitmap ||
      assetType === this.AssetType.ImageVector ||
      assetType === this.AssetType.Sound ||
      assetType === this.AssetType.Font
    );
    if (isAsset) {
      this._totalAssets++;
      this._updateProgress();
      return super.load(assetType, asset, assetFormat)
        .then((loadedAsset) => {
          this._loadedAssets++;
          this._updateProgress();
          return loadedAsset;
        });
    }
    return super.load(assetType, asset, assetFormat);
  }
}

export default StorageWithProgress;