1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | "use strict";
|
7 |
|
8 | const { AsyncParallelHook, AsyncSeriesBailHook, SyncHook } = require("tapable");
|
9 | const {
|
10 | makeWebpackError,
|
11 | makeWebpackErrorCallback
|
12 | } = require("./HookWebpackError");
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | const needCalls = (times, callback) => {
|
37 | return err => {
|
38 | if (--times === 0) {
|
39 | return callback(err);
|
40 | }
|
41 | if (err && times > 0) {
|
42 | times = 0;
|
43 | return callback(err);
|
44 | }
|
45 | };
|
46 | };
|
47 |
|
48 | class Cache {
|
49 | constructor() {
|
50 | this.hooks = {
|
51 |
|
52 | get: new AsyncSeriesBailHook(["identifier", "etag", "gotHandlers"]),
|
53 | /** @type {AsyncParallelHook<[string, Etag | null, any]>} */
|
54 | store: new AsyncParallelHook(["identifier", "etag", "data"]),
|
55 |
|
56 | storeBuildDependencies: new AsyncParallelHook(["dependencies"]),
|
57 |
|
58 | beginIdle: new SyncHook([]),
|
59 |
|
60 | endIdle: new AsyncParallelHook([]),
|
61 |
|
62 | shutdown: new AsyncParallelHook([])
|
63 | };
|
64 | }
|
65 |
|
66 | |
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | get(identifier, etag, callback) {
|
74 | const gotHandlers = [];
|
75 | this.hooks.get.callAsync(identifier, etag, gotHandlers, (err, result) => {
|
76 | if (err) {
|
77 | callback(makeWebpackError(err, "Cache.hooks.get"));
|
78 | return;
|
79 | }
|
80 | if (result === null) {
|
81 | result = undefined;
|
82 | }
|
83 | if (gotHandlers.length > 1) {
|
84 | const innerCallback = needCalls(gotHandlers.length, () =>
|
85 | callback(null, result)
|
86 | );
|
87 | for (const gotHandler of gotHandlers) {
|
88 | gotHandler(result, innerCallback);
|
89 | }
|
90 | } else if (gotHandlers.length === 1) {
|
91 | gotHandlers[0](result, () => callback(null, result));
|
92 | } else {
|
93 | callback(null, result);
|
94 | }
|
95 | });
|
96 | }
|
97 |
|
98 | |
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | store(identifier, etag, data, callback) {
|
107 | this.hooks.store.callAsync(
|
108 | identifier,
|
109 | etag,
|
110 | data,
|
111 | makeWebpackErrorCallback(callback, "Cache.hooks.store")
|
112 | );
|
113 | }
|
114 |
|
115 | |
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | storeBuildDependencies(dependencies, callback) {
|
122 | this.hooks.storeBuildDependencies.callAsync(
|
123 | dependencies,
|
124 | makeWebpackErrorCallback(callback, "Cache.hooks.storeBuildDependencies")
|
125 | );
|
126 | }
|
127 |
|
128 | |
129 |
|
130 |
|
131 | beginIdle() {
|
132 | this.hooks.beginIdle.call();
|
133 | }
|
134 |
|
135 | |
136 |
|
137 |
|
138 |
|
139 | endIdle(callback) {
|
140 | this.hooks.endIdle.callAsync(
|
141 | makeWebpackErrorCallback(callback, "Cache.hooks.endIdle")
|
142 | );
|
143 | }
|
144 |
|
145 | |
146 |
|
147 |
|
148 |
|
149 | shutdown(callback) {
|
150 | this.hooks.shutdown.callAsync(
|
151 | makeWebpackErrorCallback(callback, "Cache.hooks.shutdown")
|
152 | );
|
153 | }
|
154 | }
|
155 |
|
156 | Cache.STAGE_MEMORY = -10;
|
157 | Cache.STAGE_DEFAULT = 0;
|
158 | Cache.STAGE_DISK = 10;
|
159 | Cache.STAGE_NETWORK = 20;
|
160 |
|
161 | module.exports = Cache;
|