-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshared.js
141 lines (125 loc) · 4.42 KB
/
shared.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @ts-check
'use strict';
/**
* @fileoverview
* Constants used by both the UI and Web Worker scripts.
*/
/**
* @typedef {object} TreeNode Node object used to represent the file tree. Can
* represent either a container or a symbol.
* @prop {TreeNode[] | null} children Child tree nodes. Null values indicate
* that there are children, but that they haven't been loaded in yet. Empty
* arrays indicate this is a leaf node.
* @prop {TreeNode | null} parent Parent tree node. null if this is a root node.
* @prop {string} idPath Full path to this node.
* @prop {number} shortNameIndex The name of the node is include in the idPath.
* This index indicates where to start to slice the idPath to read the name.
* @prop {number} size Byte size of this node and its children.
* @prop {string} type Type of this node. If this node has children, the string
* may have a second character to denote the most common child.
* @prop {{[type: string]: {size:number,count:number}}} childStats Stats about
* this node's descendants, organized by symbol type.
*/
/**
* @typedef {object} TreeProgress
* @prop {TreeNode} root Root node and its direct children.
* @prop {number} percent Number from (0-1] to represent percentage.
* @prop {boolean} diffMode True if we are currently showing the diff of two
* different size files.
* @prop {string} [error] Error message, if an error occured in the worker.
* If unset, then there was no error.
*/
/**
* @typedef {object} GetSizeResult
* @prop {string} description Description of the size, shown as hover text
* @prop {Node} element Abbreviated representation of the size, which can
* include DOM elements for styling.
* @prop {number} value The size number used to create the other strings.
*/
/**
* @typedef {(node: TreeNode, unit: string) => GetSizeResult} GetSize
*/
/** Abberivated keys used by FileEntrys in the JSON data file. */
const _KEYS = Object.freeze({
SOURCE_PATH: /** @type {'p'} */ ('p'),
COMPONENT_INDEX: /** @type {'c'} */ ('c'),
FILE_SYMBOLS: /** @type {'s'} */ ('s'),
SYMBOL_NAME: /** @type {'n'} */ ('n'),
SIZE: /** @type {'b'} */ ('b'),
TYPE: /** @type {'t'} */ ('t'),
COUNT: /** @type {'u'} */ ('u'),
});
/**
* @enum {number} Various byte units and the corresponding amount of bytes
* that one unit represents.
*/
const _BYTE_UNITS = Object.freeze({
GiB: 1024 ** 3,
MiB: 1024 ** 2,
KiB: 1024 ** 1,
B: 1024 ** 0,
});
/** Set of all byte units */
const _BYTE_UNITS_SET = new Set(Object.keys(_BYTE_UNITS));
/**
* Special types used by containers, such as folders and files.
*/
const _CONTAINER_TYPES = {
DIRECTORY: 'D',
COMPONENT: 'C',
FILE: 'F',
JAVA_CLASS: 'J',
};
const _CONTAINER_TYPE_SET = new Set(Object.values(_CONTAINER_TYPES));
/** Type for a dex method symbol */
const _DEX_METHOD_SYMBOL_TYPE = 'm';
/** Type for an 'other' symbol */
const _OTHER_SYMBOL_TYPE = 'o';
/** Set of all known symbol types. Container types are not included. */
const _SYMBOL_TYPE_SET = new Set('bdrtv*xmpP' + _OTHER_SYMBOL_TYPE);
/** Name used by a directory created to hold symbols with no name. */
const _NO_NAME = '(No path)';
/** Key where type is stored in the query string state. */
const _TYPE_STATE_KEY = 'type';
/** @type {string | string[]} */
const _LOCALE = navigator.languages || navigator.language;
/**
* Returns shortName for a tree node.
* @param {TreeNode} node
*/
function shortName(node) {
return node.idPath.slice(node.shortNameIndex);
}
/**
* Iterate through each type in the query string. Types can be expressed as
* repeats of the same key in the query string ("type=b&type=p") or as a long
* string with multiple characters ("type=bp").
* @param {string[]} typesList All values associated with the "type" key in the
* query string.
*/
function* types(typesList) {
for (const typeOrTypes of typesList) {
for (const typeChar of typeOrTypes) {
yield typeChar;
}
}
}
/**
* Limit how frequently `func` is called.
* @template {T}
* @param {T & Function} func
* @param {number} wait Time to wait before func can be called again (ms).
* @returns {T}
*/
function debounce(func, wait) {
/** @type {number} */
let timeoutId;
function debounced (...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => func(...args), wait);
};
return /** @type {any} */ (debounced);
}