Cache.js 4.0 KB

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