Usage.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2021-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Keeps track of usage of font characters and wrappers
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. /**
  23. * Class used for tracking usage of font characters or wrappers
  24. */
  25. export class Usage<T> {
  26. /**
  27. * The used items.
  28. */
  29. protected used: Set<string> = new Set<string>();
  30. /**
  31. * The items marked as used since last update.
  32. */
  33. protected needsUpdate: T[] = [];
  34. /**
  35. * @param {T} item The item that has been used
  36. */
  37. public add(item: T) {
  38. const name = JSON.stringify(item);
  39. if (!this.used.has(name)) {
  40. this.needsUpdate.push(item);
  41. }
  42. this.used.add(name);
  43. }
  44. /**
  45. * @param {T} item The item to check for being used
  46. * @return {boolean} True if the item has been used
  47. */
  48. public has(item: T): boolean {
  49. return this.used.has(JSON.stringify(item));
  50. }
  51. /**
  52. * Clear the usage information
  53. */
  54. public clear() {
  55. this.used.clear();
  56. this.needsUpdate = [];
  57. }
  58. /**
  59. * Get the items marked as used since the last update.
  60. */
  61. public update() {
  62. const update = this.needsUpdate;
  63. this.needsUpdate = [];
  64. return update;
  65. }
  66. }