選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

HookMap.js 1021B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. class HookMap {
  7. constructor(factory) {
  8. this._map = new Map();
  9. this._factory = factory;
  10. this._interceptors = [];
  11. }
  12. get(key) {
  13. return this._map.get(key);
  14. }
  15. for(key) {
  16. const hook = this.get(key);
  17. if (hook !== undefined) {
  18. return hook;
  19. }
  20. let newHook = this._factory(key);
  21. const interceptors = this._interceptors;
  22. for (let i = 0; i < interceptors.length; i++) {
  23. newHook = interceptors[i].factory(key, newHook);
  24. }
  25. this._map.set(key, newHook);
  26. return newHook;
  27. }
  28. intercept(interceptor) {
  29. this._interceptors.push(
  30. Object.assign(
  31. {
  32. factory: (key, hook) => hook
  33. },
  34. interceptor
  35. )
  36. );
  37. }
  38. tap(key, options, fn) {
  39. return this.for(key).tap(options, fn);
  40. }
  41. tapAsync(key, options, fn) {
  42. return this.for(key).tapAsync(options, fn);
  43. }
  44. tapPromise(key, options, fn) {
  45. return this.for(key).tapPromise(options, fn);
  46. }
  47. }
  48. module.exports = HookMap;