Cache.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /**
  30. * @param {number} times times
  31. * @param {function(Error=): void} callback callback
  32. * @returns {function(Error=): void} callback
  33. */
  34. const needCalls = (times, callback) => err => {
  35. if (--times === 0) {
  36. return callback(err);
  37. }
  38. if (err && times > 0) {
  39. times = 0;
  40. return callback(err);
  41. }
  42. };
  43. class Cache {
  44. constructor() {
  45. this.hooks = {
  46. /** @type {AsyncSeriesBailHook<[string, Etag | null, GotHandler[]], any>} */
  47. get: new AsyncSeriesBailHook(["identifier", "etag", "gotHandlers"]),
  48. /** @type {AsyncParallelHook<[string, Etag | null, any]>} */
  49. store: new AsyncParallelHook(["identifier", "etag", "data"]),
  50. /** @type {AsyncParallelHook<[Iterable<string>]>} */
  51. storeBuildDependencies: new AsyncParallelHook(["dependencies"]),
  52. /** @type {SyncHook<[]>} */
  53. beginIdle: new SyncHook([]),
  54. /** @type {AsyncParallelHook<[]>} */
  55. endIdle: new AsyncParallelHook([]),
  56. /** @type {AsyncParallelHook<[]>} */
  57. shutdown: new AsyncParallelHook([])
  58. };
  59. }
  60. /**
  61. * @template T
  62. * @param {string} identifier the cache identifier
  63. * @param {Etag | null} etag the etag
  64. * @param {CallbackCache<T>} callback signals when the value is retrieved
  65. * @returns {void}
  66. */
  67. get(identifier, etag, callback) {
  68. /** @type {GotHandler[]} */
  69. const gotHandlers = [];
  70. this.hooks.get.callAsync(identifier, etag, gotHandlers, (err, result) => {
  71. if (err) {
  72. callback(makeWebpackError(err, "Cache.hooks.get"));
  73. return;
  74. }
  75. if (result === null) {
  76. result = undefined;
  77. }
  78. if (gotHandlers.length > 1) {
  79. const innerCallback = needCalls(gotHandlers.length, () =>
  80. callback(null, result)
  81. );
  82. for (const gotHandler of gotHandlers) {
  83. gotHandler(result, innerCallback);
  84. }
  85. } else if (gotHandlers.length === 1) {
  86. gotHandlers[0](result, () => callback(null, result));
  87. } else {
  88. callback(null, result);
  89. }
  90. });
  91. }
  92. /**
  93. * @template T
  94. * @param {string} identifier the cache identifier
  95. * @param {Etag | null} etag the etag
  96. * @param {T} data the value to store
  97. * @param {CallbackCache<void>} callback signals when the value is stored
  98. * @returns {void}
  99. */
  100. store(identifier, etag, data, callback) {
  101. this.hooks.store.callAsync(
  102. identifier,
  103. etag,
  104. data,
  105. makeWebpackErrorCallback(callback, "Cache.hooks.store")
  106. );
  107. }
  108. /**
  109. * After this method has succeeded the cache can only be restored when build dependencies are
  110. * @param {Iterable<string>} dependencies list of all build dependencies
  111. * @param {CallbackCache<void>} callback signals when the dependencies are stored
  112. * @returns {void}
  113. */
  114. storeBuildDependencies(dependencies, callback) {
  115. this.hooks.storeBuildDependencies.callAsync(
  116. dependencies,
  117. makeWebpackErrorCallback(callback, "Cache.hooks.storeBuildDependencies")
  118. );
  119. }
  120. /**
  121. * @returns {void}
  122. */
  123. beginIdle() {
  124. this.hooks.beginIdle.call();
  125. }
  126. /**
  127. * @param {CallbackCache<void>} callback signals when the call finishes
  128. * @returns {void}
  129. */
  130. endIdle(callback) {
  131. this.hooks.endIdle.callAsync(
  132. makeWebpackErrorCallback(callback, "Cache.hooks.endIdle")
  133. );
  134. }
  135. /**
  136. * @param {CallbackCache<void>} callback signals when the call finishes
  137. * @returns {void}
  138. */
  139. shutdown(callback) {
  140. this.hooks.shutdown.callAsync(
  141. makeWebpackErrorCallback(callback, "Cache.hooks.shutdown")
  142. );
  143. }
  144. }
  145. Cache.STAGE_MEMORY = -10;
  146. Cache.STAGE_DEFAULT = 0;
  147. Cache.STAGE_DISK = 10;
  148. Cache.STAGE_NETWORK = 20;
  149. module.exports = Cache;