progress-meter.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2018 Scytl Secure Electronic Voting SA
  3. *
  4. * All rights reserved
  5. *
  6. * See our extended copyright notice in *file 'Copyright.txt' which is part of this source code package
  7. */
  8. /* jshint node:true */
  9. 'use strict';
  10. module.exports = ProgressMeter;
  11. /**
  12. * @class ProgressMeter
  13. * @classdesc Encapsulates a progress meter for zero-knowledge proof of
  14. * knowledge generation, pre-computation or verification. it
  15. * is instantiated internally by the proof handlers.
  16. * @private
  17. * @param {number}
  18. * progressMax The maximum amount of progress to be attained.
  19. * @param {function}
  20. * callback The progress meter callback function.
  21. * @param {number}
  22. * [minCheckInterval=10] The minimum check interval of the progress,
  23. * as a percentage of the expected final progress value.
  24. */
  25. function ProgressMeter(progressMax, callback, minCheckInterval) {
  26. var minCheckInterval_ = minCheckInterval || 10;
  27. var lastProgressPercent_ = 0;
  28. /**
  29. * Calculates the progress as a percentage of the expected final value and
  30. * provides it as input to the provided callback function.
  31. *
  32. * @function update
  33. * @memberof ProgressMeter
  34. * @param {number}
  35. * progress The present amount of progress.
  36. */
  37. this.update = function(progress) {
  38. if (callback === undefined) {
  39. return;
  40. }
  41. var progressPercent = Math.floor((progress / progressMax) * 100);
  42. progressPercent = Math.min(progressPercent, 100);
  43. var progressPercentChange = progressPercent - lastProgressPercent_;
  44. if (progressPercentChange > 0) {
  45. var checkProgress = (progressPercentChange >= minCheckInterval_) ||
  46. (progressPercent === 100);
  47. if (checkProgress) {
  48. lastProgressPercent_ = progressPercent;
  49. callback(progressPercent);
  50. }
  51. }
  52. };
  53. /**
  54. * Resest the progress meter.
  55. *
  56. * @function reset
  57. * @memberof ProgressMeter
  58. */
  59. this.reset = function() {
  60. lastProgressPercent_ = 0;
  61. };
  62. }