|
1 | 1 |
|
2 |
| -function FilesRegister () { |
3 |
| - this.register = {}; |
| 2 | +function FilesRegister (dataFactory) { |
| 3 | + this.files = {}; |
| 4 | + this.dataFactory = dataFactory; |
4 | 5 | }
|
5 | 6 | module.exports = FilesRegister;
|
6 | 7 |
|
7 |
| -FilesRegister.prototype.forEach = function (callback) { |
8 |
| - Object.keys(this.register).forEach(function (key) { |
9 |
| - callback(key, this.register[key]); |
10 |
| - }.bind(this)); |
| 8 | +FilesRegister.prototype.keys = function () { |
| 9 | + return Object.keys(this.files); |
11 | 10 | };
|
12 | 11 |
|
13 |
| -FilesRegister.prototype.keys = function() { |
14 |
| - return Object.keys(this.register); |
15 |
| -}; |
16 |
| - |
17 |
| -FilesRegister.prototype.addFile = function (fileName) { |
18 |
| - this.register[fileName] = { |
| 12 | +FilesRegister.prototype.add = function (filePath) { |
| 13 | + this.files[filePath] = { |
19 | 14 | mtime: undefined,
|
20 |
| - source: undefined, |
21 |
| - linted: false, |
22 |
| - lints: [] |
| 15 | + data: this.dataFactory(undefined) |
23 | 16 | };
|
24 | 17 | };
|
25 | 18 |
|
26 |
| -FilesRegister.prototype.removeFile = function (fileName) { |
27 |
| - if (this.hasFile(fileName)) { |
28 |
| - delete this.register[fileName]; |
| 19 | +FilesRegister.prototype.remove = function (filePath) { |
| 20 | + if (this.has(filePath)) { |
| 21 | + delete this.files[filePath]; |
29 | 22 | }
|
30 | 23 | };
|
31 | 24 |
|
32 |
| -FilesRegister.prototype.hasFile = function (fileName) { |
33 |
| - return this.register.hasOwnProperty(fileName); |
| 25 | +FilesRegister.prototype.has = function (filePath) { |
| 26 | + return this.files.hasOwnProperty(filePath); |
34 | 27 | };
|
35 | 28 |
|
36 |
| -FilesRegister.prototype.getFile = function (fileName) { |
37 |
| - if (!this.hasFile(fileName)) { |
38 |
| - throw new Error('File "' + fileName + '" not found in register.'); |
| 29 | +FilesRegister.prototype.get = function (filePath) { |
| 30 | + if (!this.has(filePath)) { |
| 31 | + throw new Error('File "' + filePath + '" not found in register.'); |
39 | 32 | }
|
40 | 33 |
|
41 |
| - return this.register[fileName]; |
| 34 | + return this.files[filePath]; |
42 | 35 | };
|
43 | 36 |
|
44 |
| -FilesRegister.prototype.ensureFile = function (fileName) { |
45 |
| - if (!this.hasFile(fileName)) { |
46 |
| - this.addFile(fileName); |
| 37 | +FilesRegister.prototype.ensure = function (filePath) { |
| 38 | + if (!this.has(filePath)) { |
| 39 | + this.add(filePath); |
47 | 40 | }
|
48 | 41 | };
|
49 | 42 |
|
50 |
| -FilesRegister.prototype.setSource = function (fileName, source) { |
51 |
| - this.ensureFile(fileName); |
52 |
| - |
53 |
| - this.register[fileName].source = source; |
54 |
| -}; |
55 |
| - |
56 |
| -FilesRegister.prototype.hasSource = function (fileName) { |
57 |
| - return this.hasFile(fileName) && !!this.getFile(fileName).source; |
| 43 | +FilesRegister.prototype.getData = function (filePath) { |
| 44 | + return this.get(filePath).data; |
58 | 45 | };
|
59 | 46 |
|
60 |
| -FilesRegister.prototype.getSource = function (fileName) { |
61 |
| - if (!this.hasSource(fileName)) { |
62 |
| - throw new Error('Cannot get source of "' + fileName + '" file.'); |
63 |
| - } |
| 47 | +FilesRegister.prototype.mutateData = function (filePath, mutator) { |
| 48 | + this.ensure(filePath); |
64 | 49 |
|
65 |
| - return this.getFile(fileName).source; |
| 50 | + mutator(this.files[filePath].data); |
66 | 51 | };
|
67 | 52 |
|
68 |
| -FilesRegister.prototype.setMtime = function (fileName, mtime) { |
69 |
| - this.ensureFile(fileName); |
70 |
| - |
71 |
| - if (this.register[fileName].mtime !== mtime) { |
72 |
| - this.register[fileName].linted = false; |
73 |
| - this.register[fileName].lints = []; |
74 |
| - this.register[fileName].mtime = mtime; |
75 |
| - |
76 |
| - // file has been changed - we are not sure about it's current source |
77 |
| - this.register[fileName].source = undefined; |
78 |
| - } |
| 53 | +FilesRegister.prototype.getMtime = function (filePath) { |
| 54 | + return this.get(filePath).mtime; |
79 | 55 | };
|
80 | 56 |
|
81 |
| -FilesRegister.prototype.getLints = function () { |
82 |
| - var lints = []; |
83 |
| - |
84 |
| - this.forEach(function (fileName, fileEntry) { |
85 |
| - lints.push.apply(lints, fileEntry.lints); |
86 |
| - }); |
| 57 | +FilesRegister.prototype.setMtime = function (filePath, mtime) { |
| 58 | + this.ensure(filePath); |
87 | 59 |
|
88 |
| - return lints; |
89 |
| -}; |
90 |
| - |
91 |
| -FilesRegister.prototype.consumeLint = function (lint) { |
92 |
| - var fileName = lint.getFileName(); |
93 |
| - |
94 |
| - this.ensureFile(fileName); |
95 |
| - |
96 |
| - this.register[fileName].linted = true; |
97 |
| - this.register[fileName].lints.push(lint); |
98 |
| -}; |
99 |
| - |
100 |
| -FilesRegister.prototype.consumeLints = function (lints) { |
101 |
| - lints.forEach(function (lint) { |
102 |
| - this.consumeLint(lint); |
103 |
| - }.bind(this)); |
104 |
| -}; |
105 |
| - |
106 |
| -FilesRegister.prototype.setAllLinted = function () { |
107 |
| - this.keys().forEach(function (fileName) { |
108 |
| - this.register[fileName].linted = true; |
109 |
| - }.bind(this)) |
| 60 | + if (this.files[filePath].mtime !== mtime) { |
| 61 | + this.files[filePath].mtime = mtime; |
| 62 | + // file has been changed - we have to reset data |
| 63 | + this.files[filePath].data = this.dataFactory(this.files[filePath].data); |
| 64 | + } |
110 | 65 | };
|
0 commit comments