FunctionList.ts 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2017-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 Implement FunctionList object
  19. *
  20. * @author dpvc@mathjax.org (Davide Cervone)
  21. */
  22. import {PrioritizedList, PrioritizedListItem} from './PrioritizedList.js';
  23. /*****************************************************************/
  24. /**
  25. * The FunctionListItem interface (extends PrioritizedListItem<Function>)
  26. */
  27. export interface FunctionListItem extends PrioritizedListItem<Function> {}
  28. /*****************************************************************/
  29. /**
  30. * Implements the FunctionList class (extends PrioritizedList<Function>)
  31. */
  32. export class FunctionList extends PrioritizedList<Function> {
  33. /**
  34. * Executes the functions in the list (in prioritized order),
  35. * passing the given data to the functions. If any return
  36. * false, the list is terminated.
  37. *
  38. * @param {any[]} data The array of arguments to pass to the functions
  39. * @return {boolean} False if any function stopped the list by
  40. * returning false, true otherwise
  41. */
  42. public execute(...data: any[]): boolean {
  43. for (const item of this) {
  44. let result = item.item(...data);
  45. if (result === false) {
  46. return false;
  47. }
  48. }
  49. return true;
  50. }
  51. /**
  52. * Executes the functions in the list (in prioritized order) asynchronously,
  53. * passing the given data to the functions, and doing the next function
  54. * only when the previous one completes. If the function returns a
  55. * Promise, then use that to control the flow. Otherwise, if the
  56. * function returns false, the list is terminated.
  57. * This function returns a Promise. If any function in the list fails,
  58. * the promise fails. If any function returns false, the promise
  59. * succeeds, but passes false as its argument. Otherwise it succeeds
  60. * and passes true.
  61. *
  62. * @param {any[]} data The array of arguments to pass to the functions
  63. * @return {Promise} The promise that is satisfied when the function
  64. * list completes (with argument true or false
  65. * depending on whether some function returned
  66. * false or not).
  67. */
  68. public asyncExecute(...data: any[]): Promise<void> {
  69. let i = -1;
  70. let items = this.items;
  71. return new Promise((ok: Function, fail: Function) => {
  72. (function execute() {
  73. while (++i < items.length) {
  74. let result = items[i].item(...data);
  75. if (result instanceof Promise) {
  76. result.then(execute).catch(err => fail(err));
  77. return;
  78. }
  79. if (result === false) {
  80. ok(false);
  81. return;
  82. }
  83. }
  84. ok(true);
  85. })();
  86. });
  87. }
  88. }