123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- "use strict";
- const { AsyncParallelHook, AsyncSeriesBailHook, SyncHook } = require("tapable");
- const {
- makeWebpackError,
- makeWebpackErrorCallback
- } = require("./HookWebpackError");
- const needCalls = (times, callback) => {
- return err => {
- if (--times === 0) {
- return callback(err);
- }
- if (err && times > 0) {
- times = 0;
- return callback(err);
- }
- };
- };
- class Cache {
- constructor() {
- this.hooks = {
-
- get: new AsyncSeriesBailHook(["identifier", "etag", "gotHandlers"]),
-
- store: new AsyncParallelHook(["identifier", "etag", "data"]),
-
- storeBuildDependencies: new AsyncParallelHook(["dependencies"]),
-
- beginIdle: new SyncHook([]),
-
- endIdle: new AsyncParallelHook([]),
-
- shutdown: new AsyncParallelHook([])
- };
- }
-
- get(identifier, etag, callback) {
-
- const gotHandlers = [];
- this.hooks.get.callAsync(identifier, etag, gotHandlers, (err, result) => {
- if (err) {
- callback(makeWebpackError(err, "Cache.hooks.get"));
- return;
- }
- if (result === null) {
- result = undefined;
- }
- if (gotHandlers.length > 1) {
- const innerCallback = needCalls(gotHandlers.length, () =>
- callback(null, result)
- );
- for (const gotHandler of gotHandlers) {
- gotHandler(result, innerCallback);
- }
- } else if (gotHandlers.length === 1) {
- gotHandlers[0](result, () => callback(null, result));
- } else {
- callback(null, result);
- }
- });
- }
-
- store(identifier, etag, data, callback) {
- this.hooks.store.callAsync(
- identifier,
- etag,
- data,
- makeWebpackErrorCallback(callback, "Cache.hooks.store")
- );
- }
-
- storeBuildDependencies(dependencies, callback) {
- this.hooks.storeBuildDependencies.callAsync(
- dependencies,
- makeWebpackErrorCallback(callback, "Cache.hooks.storeBuildDependencies")
- );
- }
-
- beginIdle() {
- this.hooks.beginIdle.call();
- }
-
- endIdle(callback) {
- this.hooks.endIdle.callAsync(
- makeWebpackErrorCallback(callback, "Cache.hooks.endIdle")
- );
- }
-
- shutdown(callback) {
- this.hooks.shutdown.callAsync(
- makeWebpackErrorCallback(callback, "Cache.hooks.shutdown")
- );
- }
- }
- Cache.STAGE_MEMORY = -10;
- Cache.STAGE_DEFAULT = 0;
- Cache.STAGE_DISK = 10;
- Cache.STAGE_NETWORK = 20;
- module.exports = Cache;
|