replay-utils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Replay utils
  2. /**
  3. * Partitions array into two arrays.
  4. *
  5. * Partitions array into two array by pushing elements left or right given some decision function, which is
  6. * evaluated on each element. Successful validations lead to placement on the left side, others on the right.
  7. *
  8. * @param {Array} array Array to be partitioned.
  9. * @param {function} decisionFunction Function that assigns elements to the left or right arrays.
  10. * @return {Array} Array containing the left and right arrays.
  11. */
  12. export function partition(array, decisionFunction){
  13. return array.reduce(function(result, element, i) {
  14. decisionFunction(element, i, array)
  15. ? result[0].push(element)
  16. : result[1].push(element);
  17. return result;
  18. }, [[],[]]
  19. );
  20. };
  21. /**
  22. * Updates beliefs.
  23. *
  24. * Updates oldBeliefs with the most recent newBeliefs about the same event for the same sensor by the same source.
  25. *
  26. * @param {Array} oldBeliefs Array containing old beliefs.
  27. * @param {Array} newBeliefs Array containing new beliefs.
  28. * @return {Array} Array containing updated beliefs.
  29. */
  30. export function updateBeliefs(oldBeliefs, newBeliefs) {
  31. // Group by sensor, event start and source
  32. var oldBeliefsByEventBySource = Object.fromEntries(new Map(oldBeliefs.map(belief => [belief.sensor.id + '_' + belief.event_start + '_' + belief.source.id, belief]))); // array -> dict (already had one belief per event)
  33. // Group by sensor, event start and source, and select only the most recent new beliefs
  34. var mostRecentNewBeliefsByEventBySource = Object.fromEntries(new Map(newBeliefs.map(belief => [belief.sensor.id + '_' + belief.event_start + '_' + belief.source.id, belief]))); // array -> dict (assumes beliefs are ordered by ascending belief time, with the last belief used as dict value)
  35. // Return old beliefs updated with most recent new beliefs
  36. return Object.values({...oldBeliefsByEventBySource, ...mostRecentNewBeliefsByEventBySource}) // dict -> array
  37. }
  38. // Define the step duration for the replay (value in ms)
  39. export var beliefTimedelta = 3600000
  40. /**
  41. * Timer that can be canceled using the optional AbortSignal.
  42. * Adapted from https://www.bennadel.com/blog/4195-using-abortcontroller-to-debounce-settimeout-calls-in-javascript.htm
  43. * MIT License: https://www.bennadel.com/blog/license.htm
  44. */
  45. export function setAbortableTimeout(callback, delayInMilliseconds, signal) {
  46. signal?.addEventListener( "abort", handleAbort );
  47. var internalTimer = setTimeout(internalCallback, delayInMilliseconds);
  48. function internalCallback() {
  49. signal?.removeEventListener( "abort", handleAbort );
  50. callback();
  51. }
  52. function handleAbort() {
  53. clearTimeout( internalTimer );
  54. }
  55. }