jsexecution.js 983 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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 = JSExecutionCollector;
  11. var collectEntropy_ = function() {};
  12. function JSExecutionCollector(collectEntropyCallback) {
  13. collectEntropy_ = collectEntropyCallback;
  14. }
  15. JSExecutionCollector.prototype = {
  16. _collectTimeout: null,
  17. startCollector: function() {
  18. var timer_start = (+new Date()), timer_end;
  19. this._collectTimeout = (function collect() {
  20. timer_end = (+new Date());
  21. var total_time = timer_end - timer_start;
  22. // because of browser baseline time checks we limit it
  23. if (total_time > 20) {
  24. collectEntropy_((+new Date()), 1);
  25. }
  26. timer_start = timer_end;
  27. return setTimeout(collect, 0);
  28. })();
  29. },
  30. stopCollector: function() {
  31. clearTimeout(this._collectTimeout);
  32. }
  33. };