1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | class ModuleSet {
|
11 |
|
12 | constructor () {
|
13 | this._index = -1;
|
14 | this._map = {};
|
15 | this._set = {};
|
16 | this._array = {};
|
17 | this._type = {};
|
18 | }
|
19 |
|
20 | add (file, type) {
|
21 | let id = this.get(file);
|
22 |
|
23 | if (id === undefined) {
|
24 | this._index++;
|
25 | this.length = this._index + 1;
|
26 | id = this._index;
|
27 | this._map[file] = id;
|
28 | this._array[id] = file;
|
29 | this._type[file] = type;
|
30 | }
|
31 |
|
32 | return id;
|
33 | }
|
34 |
|
35 | get (file) {
|
36 | return this._map[file];
|
37 | }
|
38 |
|
39 | pending (file) {
|
40 | return this.get(file) !== undefined && this._set[file] === undefined;
|
41 | }
|
42 |
|
43 | update (file, data, type) {
|
44 | if (!this.get(file)) {
|
45 | this.add(file, type);
|
46 | }
|
47 | this._set[file] = data;
|
48 | this._type[file] = type;
|
49 | }
|
50 |
|
51 | data (v) {
|
52 | if (typeof v === 'number') {
|
53 | return this._set[this._array[v]];
|
54 | } else {
|
55 | return this._set[v];
|
56 | }
|
57 | }
|
58 |
|
59 | array (type) {
|
60 | if (!type) {
|
61 | this._array.length = this.length;
|
62 | return Array.prototype.slice.apply(this._array);
|
63 | } else {
|
64 | return this.array().filter(file => this._type[file] === type);
|
65 | }
|
66 | }
|
67 |
|
68 | type (v) {
|
69 | if (typeof v === 'number') {
|
70 | return this._type[this._array[v]];
|
71 | } else {
|
72 | return this._type[v];
|
73 | }
|
74 | }
|
75 |
|
76 | }
|
77 |
|
78 |
|
79 | exports = module.exports = ModuleSet;
|