All user data for FoundryVTT. Includes worlds, systems, modules, and any asset in the "foundryuserdata" directory. Does NOT include the FoundryVTT installation itself.
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.

108 lines
3.0 KiB

1 year ago
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2021 DnD5e Helpers Team
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. import {MODULE} from './module.js'
  25. let updateQueues = new Map();
  26. /**
  27. * Safely manages concurrent updates to the provided entity type
  28. * @function warpgate.plugin.queueUpdate
  29. * @param {Function} updateFn the function that handles the actual update (can be async)
  30. */
  31. export function queueUpdate(updateFn) {
  32. /** queue the update for this entity */
  33. getQueue().queueUpdate(updateFn);
  34. }
  35. export function flush() {
  36. return getQueue().flush();
  37. }
  38. function getQueue(entity = "default"){
  39. /** if this is a new entity type, create the queue object to manage it */
  40. if(!updateQueues.has(entity)) {
  41. updateQueues.set(entity, new UpdateQueue(entity));
  42. }
  43. /** queue the update for this entity */
  44. return updateQueues.get(entity);
  45. }
  46. /**
  47. * Helper class to manage database updates that occur from
  48. * hooks that may fire back to back.
  49. * @ignore
  50. */
  51. class UpdateQueue {
  52. constructor(entityType) {
  53. /** self identification */
  54. this.entityType = entityType;
  55. /** buffer of update functions to run */
  56. this.queue = [];
  57. /** Semaphore for 'batch update in progress' */
  58. this.inFlight = false;
  59. }
  60. queueUpdate(fn) {
  61. this.queue.push(fn);
  62. /** only kick off a batch of updates if none are in flight */
  63. if (!this.inFlight) {
  64. this.runUpdate();
  65. }
  66. }
  67. flush() {
  68. return MODULE.waitFor( () => !this.inFlight )
  69. }
  70. async runUpdate(){
  71. this.inFlight = true;
  72. while(this.queue.length > 0) {
  73. /** grab the last update in the list and hold onto its index
  74. * in case another update pushes onto this array before we
  75. * are finished.
  76. */
  77. const updateIndex = this.queue.length-1;
  78. const updateFn = this.queue[updateIndex];
  79. /** wait for the update to complete */
  80. await updateFn();
  81. /** remove this entry from the queue */
  82. this.queue.splice(updateIndex,1);
  83. }
  84. this.inFlight = false;
  85. }
  86. }