1/*
2 * Copyright 2020, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17type File = {
18  blobUrl: string,
19  filename: string,
20}
21
22import JSZip from 'jszip';
23
24export default abstract class Trace implements ITrace {
25  selectedIndex: Number;
26  data: Object;
27  timeline: Array<Number>;
28  _files: File[];
29
30  constructor(data: any, timeline: Number[], files: any[]) {
31    this.selectedIndex = 0;
32    this.data = data;
33    this.timeline = timeline;
34    this._files = files;
35  }
36
37  get files(): File[] {
38    return Object.values(this._files).flat();
39  }
40
41  abstract get type(): String;
42
43  get blobUrl() {
44    if (this.files.length == 0) {
45      return null;
46    }
47
48    if (this.files.length == 1) {
49      return this.files[0].blobUrl;
50    }
51
52    const zip = new JSZip();
53
54    return (async () => {
55      for (const file of this.files) {
56        const blob = await fetch(file.blobUrl).then((r) => r.blob());
57        zip.file(file.filename, blob);
58      }
59
60      return await zip.generateAsync({ type: 'blob' });
61    })();
62
63  }
64}
65
66interface ITrace {
67  files: Array<Object>;
68  type: String,
69}