forked from fusionjs/fusion-plugin-i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchunk-translation-map.js
81 lines (66 loc) · 2.25 KB
/
chunk-translation-map.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
/** Copyright (c) 2018 Uber Technologies, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
/* eslint-env node */
class ChunkTranslationMap {
/*:: translations: Map<number | string, Map<string, Set<string>>>; */
constructor() {
this.translations = new Map();
}
add(
filename /*: string */,
chunkIds /*: Array<number | string> */,
translations /*: Array<string> */
) /*: void */ {
for (const chunkId of chunkIds) {
if (!this.translations.has(chunkId)) {
this.translations.set(chunkId, new Map());
}
const translationsForChunk = this.translations.get(chunkId);
if (!translationsForChunk)
throw new Error('translations missing chunkId');
for (const key of translations) {
if (!translationsForChunk.has(key)) {
translationsForChunk.set(key, new Set());
}
const filesForTranslation = translationsForChunk.get(key);
if (!filesForTranslation)
throw new Error('unable to get Set() in translations for chunk');
filesForTranslation.add(filename);
}
}
}
dispose(
filename /*: string */,
chunkIds /*: Array<number> */,
translations /*: Array<string> */
) /*: void */ {
for (const chunkId of chunkIds) {
const translationsForChunk = this.translations.get(chunkId);
if (!translationsForChunk)
throw new Error('translations missing chunkId');
for (const key of translations) {
const filesForTranslation = translationsForChunk.get(key);
if (!filesForTranslation)
throw new Error('unable to get Set() in translations for chunk');
filesForTranslation.delete(filename);
if (filesForTranslation.size === 0) {
translationsForChunk.delete(key);
}
}
}
}
translationsForChunk(chunkId /*: number | string */) /*: Iterator<string> */ {
if (!this.translations.has(chunkId)) {
return new Set().keys();
}
const translationsForChunk = this.translations.get(chunkId);
if (!translationsForChunk) throw new Error('translations missing chunkId');
return translationsForChunk.keys();
}
}
module.exports = new ChunkTranslationMap();