IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.6KB

  1. //
  2. // Copyright (C) Stanislaw Adaszewski, 2020
  3. // Contact: s.adaszewski@gmail.com
  4. // Website: https://adared.ch/wba
  5. // License: GNU Affero General Public License, Version 3
  6. //
  7. const defaultOrderRegistry = {};
  8. /* function notify(orderRegistry) {
  9. if (!('listeners' in orderRegistry))
  10. return;
  11. for (let k in orderRegistry.listeners) {
  12. orderRegistry.listeners[k](orderRegistry);
  13. }
  14. } */
  15. function cursorDecor() {
  16. let d = $('#cursor-decor');
  17. if (d.length === 1)
  18. return $(d[0]);
  19. d = $('<div id="cursor-decor" style="z-index: 10000; position: absolute; left: 10px; top: 10px;"> \
  20. <div class="progress" style="height: 8px;"> \
  21. <div class="progress-bar progress-bar-striped progress-bar-animated" \
  22. role="progressbar" aria-valuenow="100" aria-valuemin="0" \
  23. aria-valuemax="100" style="width: 32px;"></div> \
  24. </div> \
  25. </div>');
  26. $(document.body).append(d);
  27. let pageX = 0, pageY = 0, scrollX = 0, scrollY = 0;
  28. document.addEventListener('mousemove', e => {
  29. pageX = e.pageX;
  30. pageY = e.pageY;
  31. d.css({ left: (e.pageX + 16) + 'px', top: (e.pageY + 16) + 'px' })
  32. scrollX = window.scrollX;
  33. scrollY = window.scrollY;
  34. });
  35. document.addEventListener('scroll', e => {
  36. d.css({ left: (pageX + window.scrollX - scrollX + 16) + 'px',
  37. top: (pageY + window.scrollY - scrollY + 16) + 'px' });
  38. });
  39. return d;
  40. }
  41. function updateCursorDecor(orderRegistry) {
  42. const d = cursorDecor();
  43. if (Object.keys(orderRegistry.pendingCompletion).length === 0)
  44. d.hide();
  45. else
  46. d.show();
  47. }
  48. function wbApplyPromiseOrdering(prom, orderRegistry) {
  49. let orderId;
  50. if (!orderRegistry)
  51. orderRegistry = defaultOrderRegistry;
  52. //if (Object.keys(orderRegistry).length === 0) {
  53. if (!('started' in orderRegistry)) {
  54. orderRegistry.started = 0;
  55. orderRegistry.pendingCompletion = {};
  56. orderRegistry.completed = { 0: true };
  57. }
  58. orderRegistry.started += 1;
  59. orderId = orderRegistry.started;
  60. // console.log('New orderId: ' + orderId);
  61. // notify(orderRegistry);
  62. cursorDecor().show();
  63. const orderCallback = ((isCatch, payload) => {
  64. // console.log('orderId: ' + orderId +
  65. // ', pendingCompletion: ' + Object.keys(orderRegistry.pendingCompletion) +
  66. // ', completed: ' + Object.keys(orderRegistry.completed));
  67. if ((orderId - 1) in orderRegistry.completed) {
  68. // console.log('Running: ' + orderId);
  69. orderRegistry.completed[orderId] = true;
  70. delete orderRegistry.pendingCompletion[orderId];
  71. const keys = Object.keys(orderRegistry.pendingCompletion);
  72. keys.sort((a, b) => (a - b));
  73. keys.map(k => {
  74. if ((k - 1) in orderRegistry.completed) {
  75. // console.log('Running: ' + k);
  76. orderRegistry.pendingCompletion[k]();
  77. orderRegistry.completed[k] = true;
  78. delete orderRegistry.pendingCompletion[k];
  79. }
  80. });
  81. if (orderRegistry.started in orderRegistry.completed) {
  82. // console.log('Garbage collect');
  83. orderRegistry.started = 0;
  84. orderRegistry.completed = { 0: true };
  85. cursorDecor().hide();
  86. }
  87. if (isCatch)
  88. throw payload;
  89. else
  90. return payload;
  91. }
  92. const prom_1 = new Promise((accept, reject) => {
  93. orderRegistry.pendingCompletion[orderId] = (() =>
  94. (isCatch ? reject(payload) : accept(payload)));
  95. });
  96. return prom_1;
  97. });
  98. prom = prom.then(xhr => orderCallback(false, xhr));
  99. prom = prom.catch(e => orderCallback(true, e));
  100. return prom;
  101. }
  102. export default wbApplyPromiseOrdering;