UNPKG

1.94 kBJavaScriptView Raw
1/**
2 * Tencent is pleased to support the open source community by making WePY available.
3 * Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
4 *
5 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
6 * http://opensource.org/licenses/MIT
7 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
8 */
9
10class 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
79exports = module.exports = ModuleSet;