Skip to content

Commit bebed18

Browse files
authored
Merge pull request #2856 from sveltejs/raf
treat requestAnimationFrame as a noop on the server
2 parents cce9f14 + 9d53f56 commit bebed18

File tree

8 files changed

+22
-12
lines changed

8 files changed

+22
-12
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/compile/render-dom/wrappers/Element/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ export default class ElementWrapper extends Wrapper {
461461
function ${handler}() {
462462
${animation_frame && deindent`
463463
cancelAnimationFrame(${animation_frame});
464-
if (!${this.var}.paused) ${animation_frame} = requestAnimationFrame(${handler});`}
464+
if (!${this.var}.paused) ${animation_frame} = @raf(${handler});`}
465465
${needs_lock && `${lock} = true;`}
466466
ctx.${handler}.call(${this.var}${contextual_dependencies.size > 0 ? ', ctx' : ''});
467467
}

src/internal/loop.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { now } from './utils.js';
1+
import { now, raf } from './utils.js';
22

33
const tasks = new Set();
44
let running = false;
@@ -12,7 +12,7 @@ function run_tasks() {
1212
});
1313

1414
running = tasks.size > 0;
15-
if (running) requestAnimationFrame(run_tasks);
15+
if (running) raf(run_tasks);
1616
}
1717

1818
export function clear_loops() {
@@ -26,7 +26,7 @@ export function loop(fn) {
2626

2727
if (!running) {
2828
running = true;
29-
requestAnimationFrame(run_tasks);
29+
raf(run_tasks);
3030
}
3131

3232
return {

src/internal/style_manager.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { element } from './dom.js';
2+
import { raf } from './utils.js';
23

34
let stylesheet;
45
let active = 0;
@@ -56,7 +57,7 @@ export function delete_rule(node, name) {
5657
}
5758

5859
export function clear_rules() {
59-
requestAnimationFrame(() => {
60+
raf(() => {
6061
if (active) return;
6162
let i = stylesheet.cssRules.length;
6263
while (i--) stylesheet.deleteRule(i);

src/internal/utils.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,19 @@ export function exclude_internal_props(props) {
8080
return result;
8181
}
8282

83-
export let now = typeof window !== 'undefined'
83+
const is_client = typeof window !== 'undefined';
84+
85+
export let now = is_client
8486
? () => window.performance.now()
8587
: () => Date.now();
8688

89+
export let raf = is_client ? requestAnimationFrame : noop;
90+
8791
// used internally for testing
8892
export function set_now(fn) {
8993
now = fn;
94+
}
95+
96+
export function set_raf(fn) {
97+
raf = fn;
9098
}

test/js/samples/debug-hoisted/expected.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ class Component extends SvelteComponentDev {
5353
}
5454
}
5555

56-
export default Component;
56+
export default Component;

test/js/samples/media-bindings/expected.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
insert,
99
listen,
1010
noop,
11+
raf,
1112
run_all,
1213
safe_not_equal,
1314
time_ranges_to_array
@@ -18,7 +19,7 @@ function create_fragment(ctx) {
1819

1920
function audio_timeupdate_handler() {
2021
cancelAnimationFrame(audio_animationframe);
21-
if (!audio.paused) audio_animationframe = requestAnimationFrame(audio_timeupdate_handler);
22+
if (!audio.paused) audio_animationframe = raf(audio_timeupdate_handler);
2223
audio_updating = true;
2324
ctx.audio_timeupdate_handler.call(audio);
2425
}

test/runtime/index.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as path from "path";
33
import * as fs from "fs";
44
import { rollup } from 'rollup';
55
import * as virtual from 'rollup-plugin-virtual';
6-
import { clear_loops, set_now } from "../../internal.js";
6+
import { clear_loops, set_now, set_raf } from "../../internal.js";
77

88
import {
99
showOutput,
@@ -101,15 +101,15 @@ describe("runtime", () => {
101101
}
102102
};
103103
set_now(() => raf.time);
104-
global.requestAnimationFrame = cb => {
104+
set_raf(cb => {
105105
let called = false;
106106
raf.callback = () => {
107107
if (!called) {
108108
called = true;
109109
cb();
110110
}
111111
};
112-
};
112+
});
113113

114114
try {
115115
mod = require(`./samples/${dir}/main.svelte`);

0 commit comments

Comments
 (0)