paint-by-numbers

Relaxing paint-by-numbers game  https://p.ce9e.org/paint-by-numbers/
git clone https://git.ce9e.org/paint-by-numbers.git

commit
7c3673c5fbe04ea2db39c4d2ec5da9d0cfdd87c3
parent
d5af7d9a66f73ebfe9fc244cd33980901cd6849c
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-02-15 04:51
refactor: simplify thottle

Diffstat

D js/utils.js 35 -----------------------------------
M js/view.js 15 +++++++++++++--

2 files changed, 13 insertions, 37 deletions


diff --git a/js/utils.js b/js/utils.js

@@ -1,35 +0,0 @@
    1    -1 var _setTimeout = function(fn, timeout) {
    2    -1     if (timeout === 'animation') {
    3    -1         requestAnimationFrame(fn);
    4    -1     } else {
    5    -1         setTimeout(fn, timeout);
    6    -1     }
    7    -1 };
    8    -1 
    9    -1 export var throttle = function(fn, timeout) {
   10    -1     var blocked = false;
   11    -1     var called = false;
   12    -1     var nextArgs = [];
   13    -1 
   14    -1     var wrapper = function(...args) {
   15    -1         if (blocked) {
   16    -1             called = true;
   17    -1             nextArgs = args;
   18    -1         } else {
   19    -1             blocked = true;
   20    -1             called = false;
   21    -1             nextArgs = [];
   22    -1 
   23    -1             fn.apply(this, args);
   24    -1 
   25    -1             _setTimeout(() => {
   26    -1                 blocked = false;
   27    -1                 if (called) {
   28    -1                     wrapper.apply(this, nextArgs);
   29    -1                 }
   30    -1             }, timeout);
   31    -1         }
   32    -1     };
   33    -1 
   34    -1     return wrapper;
   35    -1 };

diff --git a/js/view.js b/js/view.js

@@ -1,4 +1,15 @@
    1    -1 import * as utils from './utils.js';
   -1     1 export var throttle = function(fn) {
   -1     2     var called = false;
   -1     3     return function() {
   -1     4         if (!called) {
   -1     5             called = true;
   -1     6             requestAnimationFrame(() => {
   -1     7                 called = false;
   -1     8                 fn.apply(this);
   -1     9             });
   -1    10         }
   -1    11     };
   -1    12 };
    2    13 
    3    14 export class View {
    4    15     constructor(canvas, frame) {
@@ -56,4 +67,4 @@ export class View {
   56    67     }
   57    68 }
   58    69 
   59    -1 View.prototype.render = utils.throttle(View.prototype.render, 'animation');
   -1    70 View.prototype.render = throttle(View.prototype.render);