Vedavyas1235 commited on
Commit
227501d
·
1 Parent(s): 3309c33

Sync TIME_SHIM to support CSS animations

Browse files
Files changed (1) hide show
  1. index.js +55 -46
index.js CHANGED
@@ -40,64 +40,73 @@ const requireAuth = (req, res, next) => {
40
  const TIME_SHIM = `
41
  window.__VTIME_INSTALLED__ = true;
42
  var vtime = 0;
43
- function now(){ return vtime; }
44
- try { Object.defineProperty(performance, 'now', { value: now, configurable: true }); } catch(e){}
45
- var _Date = Date;
46
- function FakeDate(){
47
- if (!(this instanceof FakeDate)) return new _Date(Math.floor(vtime)).toString();
48
- if (arguments.length === 0) return new _Date(Math.floor(vtime));
49
- return new (Function.prototype.bind.apply(_Date, [null].concat([].slice.call(arguments))))();
 
 
 
 
 
 
 
 
 
 
 
 
50
  }
51
- FakeDate.now = function(){ return Math.floor(vtime); };
52
- FakeDate.parse = _Date.parse; FakeDate.UTC = _Date.UTC;
53
- FakeDate.prototype = _Date.prototype;
54
- try { window.Date = FakeDate; } catch(e){}
55
- var _origRaf = window.requestAnimationFrame;
56
  var rafCallbacks = []; var rafId = 0;
 
 
57
  window.requestAnimationFrame = function(cb){ rafId++; rafCallbacks.push({id: rafId, cb: cb}); return rafId; };
58
  window.cancelAnimationFrame = function(id){ rafCallbacks = rafCallbacks.filter(function(x){ return x.id !== id; }); };
59
- var timers = []; var timerId = 0;
60
- var _origSetTimeout = window.setTimeout;
61
- window.setTimeout = function(fn, delay){
62
- timerId++; var args = [].slice.call(arguments, 2);
63
- timers.push({id: timerId, fn: fn, due: vtime + (delay||0), args: args, interval: 0}); return timerId;
 
64
  };
65
- window.setInterval = function(fn, delay){
66
- timerId++; var args = [].slice.call(arguments, 2); var d = delay || 16;
67
- timers.push({id: timerId, fn: fn, due: vtime + d, args: args, interval: d}); return timerId;
 
 
 
 
68
  };
69
- window.clearTimeout = window.clearInterval = function(id){ timers = timers.filter(function(t){ return t.id !== id; }); };
 
70
  window.__advanceVTime = function(target){
71
  var STEP = 8;
72
  while (vtime < target) {
73
- var next = Math.min(vtime + STEP, target); vtime = next;
74
- var due = timers.filter(function(t){ return t.due <= vtime; });
75
- due.sort(function(a,b){ return a.due - b.due; });
76
- for (var i = 0; i < due.length; i++) {
77
- var t = due[i];
78
- try { if (typeof t.fn === 'function') t.fn.apply(window, t.args || []); else (0, eval)(String(t.fn)); } catch(e){}
79
- if (t.interval > 0) t.due = vtime + t.interval;
80
- else timers = timers.filter(function(x){ return x.id !== t.id; });
 
81
  }
82
- var cbs = rafCallbacks; rafCallbacks = [];
83
- for (var j = 0; j < cbs.length; j++) {
84
- try {
85
- cbs[j].cb(vtime);
86
- } catch(e) {
87
- try {
88
- var c = document.querySelector('canvas');
89
- if (c) {
90
- var ctx = c.getContext('2d');
91
- ctx.save();
92
- ctx.resetTransform();
93
- ctx.fillStyle = 'red';
94
- ctx.font = '14px Arial';
95
- ctx.fillText('RAF Error: ' + e.message, 10, 20);
96
- ctx.restore();
97
- }
98
- } catch(e2){}
99
  }
100
  }
 
 
 
 
 
101
  }
102
  };
103
  `;
 
40
  const TIME_SHIM = `
41
  window.__VTIME_INSTALLED__ = true;
42
  var vtime = 0;
43
+
44
+ window.__nativeSetTimeout = window.setTimeout;
45
+ window.__nativeClearTimeout = window.clearTimeout;
46
+ window.__nativeSetInterval = window.setInterval;
47
+ window.__nativeClearInterval = window.clearInterval;
48
+ window.__nativeDate = window.Date;
49
+ window.__nativePerformance = window.performance;
50
+
51
+ var _origDate = Date;
52
+ window.Date = function() {
53
+ if (arguments.length === 0) return new _origDate(vtime);
54
+ return new _origDate(...arguments);
55
+ };
56
+ window.Date.now = function() { return vtime; };
57
+ window.Date.parse = _origDate.parse;
58
+ window.Date.UTC = _origDate.UTC;
59
+
60
+ if (window.performance) {
61
+ window.performance.now = function() { return vtime; };
62
  }
63
+
 
 
 
 
64
  var rafCallbacks = []; var rafId = 0;
65
+ window.__origRaf = window.requestAnimationFrame;
66
+ window.__origCancelRaf = window.cancelAnimationFrame;
67
  window.requestAnimationFrame = function(cb){ rafId++; rafCallbacks.push({id: rafId, cb: cb}); return rafId; };
68
  window.cancelAnimationFrame = function(id){ rafCallbacks = rafCallbacks.filter(function(x){ return x.id !== id; }); };
69
+
70
+ var timeoutCallbacks = []; var timeoutId = 0;
71
+ window.setTimeout = function(cb, delay){
72
+ timeoutId++;
73
+ timeoutCallbacks.push({id: timeoutId, cb: typeof cb === 'function' ? cb : function(){}, triggerTime: vtime + (delay || 0)});
74
+ return timeoutId;
75
  };
76
+ window.clearTimeout = function(id){ timeoutCallbacks = timeoutCallbacks.filter(function(x){ return x.id !== id; }); };
77
+
78
+ var intervalCallbacks = []; var intervalId = 0;
79
+ window.setInterval = function(cb, delay){
80
+ intervalId++;
81
+ intervalCallbacks.push({id: intervalId, cb: typeof cb === 'function' ? cb : function(){}, interval: delay || 0, nextTime: vtime + (delay || 0)});
82
+ return intervalId;
83
  };
84
+ window.clearInterval = function(id){ intervalCallbacks = intervalCallbacks.filter(function(x){ return x.id !== id; }); };
85
+
86
  window.__advanceVTime = function(target){
87
  var STEP = 8;
88
  while (vtime < target) {
89
+ vtime = Math.min(vtime + STEP, target);
90
+ var pendingTimeouts = timeoutCallbacks;
91
+ timeoutCallbacks = [];
92
+ for (var k = 0; k < pendingTimeouts.length; k++) {
93
+ if (pendingTimeouts[k].triggerTime <= vtime) {
94
+ try { pendingTimeouts[k].cb(); } catch(e){}
95
+ } else {
96
+ timeoutCallbacks.push(pendingTimeouts[k]);
97
+ }
98
  }
99
+ for (var l = 0; l < intervalCallbacks.length; l++) {
100
+ if (intervalCallbacks[l].nextTime <= vtime) {
101
+ try { intervalCallbacks[l].cb(); } catch(e){}
102
+ intervalCallbacks[l].nextTime = vtime + intervalCallbacks[l].interval;
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  }
104
  }
105
+ var cbs = rafCallbacks; rafCallbacks = [];
106
+ for (var j = 0; j < cbs.length; j++) { try { cbs[j].cb(vtime); } catch(e){} }
107
+ if (document.getAnimations) {
108
+ document.getAnimations().forEach(function(a){ a.currentTime = vtime; });
109
+ }
110
  }
111
  };
112
  `;