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