Cache.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { AsyncParallelHook, AsyncSeriesBailHook, SyncHook } = require("tapable");
  7. const {
  8. makeWebpackError,
  9. makeWebpackErrorCallback
  10. } = require("./HookWebpackError");
  11. /** @typedef {import("./WebpackError")} WebpackError */
  12. /**
  13. * @typedef {Object} Etag
  14. * @property {function(): string} toString
  15. */
  16. /**
  17. * @template T
  18. * @callback CallbackCache
  19. * @param {(WebpackError | null)=} err
  20. * @param {T=} result
  21. * @returns {void}
  22. */
  23. /**
  24. * @callback GotHandler
  25. * @param {any} result
  26. * @param {function(Error=): void} callback
  27. * @returns {void}
  28. */
  29. const needCalls = (times, callback) => {
  30. return err => {
  31. if (--times === 0) {
  32. return callback(err);
  33. }
  34. if (err && times > 0) {
  35. times = 0;
  36. return callback(err);
  37. }
  38. };
  39. };
  40. class Cache {
  41. constructor() {
  42. this.hooks = {
  43. /** @type {AsyncSeriesBailHook<[string, Etag | null, GotHandler[]], any>} */
  44. get: new AsyncSeriesBailHook(["identifier", "etag", "gotHandlers"]),
  45. /** @type {AsyncParallelHook<[string, Etag | null, any]>} */
  46. store: new AsyncParallelHook(["identifier", "etag", "data"]),
  47. /** @type {AsyncParallelHook<[Iterable<string>]>} */
  48. storeBuildDependencies: new AsyncParallelHook(["dependencies"]),
  49. /** @type {SyncHook<[]>} */
  50. beginIdle: new SyncHook([]),
  51. /** @type {AsyncParallelHook<[]>} */
  52. endIdle: new AsyncParallelHook([]),
  53. /** @type {AsyncParallelHook<[]>} */
  54. shutdown: new AsyncParallelHook([])
  55. };
  56. }
  57. /**
  58. * @template T
  59. * @param {string} identifier the cache identifier
  60. * @param {Etag | null} etag the etag
  61. * @param {CallbackCache<T>} callback signals when the value is retrieved
  62. * @returns {void}
  63. */
  64. get(identifier, etag, callback) {
  65. const gotHandlers = [];
  66. this.hooks.get.callAsync(identifier, etag, gotHandlers, (err, result) => {
  67. if (err) {
  68. callback(makeWebpackError(err, "Cache.hooks.get"));
  69. return;
  70. }
  71. if (result === null) {
  72. result = undefined;
  73. }
  74. if (gotHandlers.length > 1) {
  75. const innerCallback = needCalls(gotHandlers.length, () =>
  76. callback(null, result)
  77. );
  78. for (const gotHandler of gotHandlers) {
  79. gotHandler(result, innerCallback);
  80. }
  81. } else if (gotHandlers.length === 1) {
  82. gotHandlers[0](result, () => callback(null, result));
  83. } else {
  84. callback(null, result);
  85. }
  86. });
  87. }
  88. /**
  89. * @template T
  90. * @param {string} identifier the cache identifier
  91. * @param {Etag | null} etag the etag
  92. * @param {T} data the value to store
  93. * @param {CallbackCache<void>} callback signals when the value is stored
  94. * @returns {void}
  95. */
  96. store(identifier, etag, data, callback) {
  97. this.hooks.store.callAsync(
  98. identifier,
  99. etag,
  100. data,
  101. makeWebpackErrorCallback(callback, "Cache.hooks.store")
  102. );
  103. }
  104. /**
  105. * After this method has succeeded the cache can only be restored when build dependencies are
  106. * @param {Iterable<string>} dependencies list of all build dependencies
  107. * @param {CallbackCache<void>} callback signals when the dependencies are stored
  108. * @returns {void}
  109. */
  110. storeBuildDependencies(dependencies, callback) {
  111. this.hooks.storeBuildDependencies.callAsync(
  112. dependencies,
  113. makeWebpackErrorCallback(callback, "Cache.hooks.storeBuildDependencies")
  114. );
  115. }
  116. /**
  117. * @returns {void}
  118. */
  119. beginIdle() {
  120. this.hooks.beginIdle.call();
  121. }
  122. /**
  123. * @param {CallbackCache<void>} callback signals when the call finishes
  124. * @returns {void}
  125. */
  126. endIdle(callback) {
  127. this.hooks.endIdle.callAsync(
  128. makeWebpackErrorCallback(callback, "Cache.hooks.endIdle")
  129. );
  130. }
  131. /**
  132. * @param {CallbackCache<void>} callback signals when the call finishes
  133. * @returns {void}
  134. */
  135. shutdown(callback) {
  136. this.hooks.shutdown.callAsync(
  137. makeWebpackErrorCallback(callback, "Cache.hooks.shutdown")
  138. );
  139. }
  140. }
  141. Cache.STAGE_MEMORY = -10;
  142. Cache.STAGE_DEFAULT = 0;
  143. Cache.STAGE_DISK = 10;
  144. Cache.STAGE_NETWORK = 20;
  145. module.exports = Cache;