Merge pull request #2856 from sveltejs/raf

treat requestAnimationFrame as a noop on the server
pull/2876/head
Rich Harris 6 years ago committed by GitHub
commit bebed18a93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

2
package-lock.json generated

@ -1,6 +1,6 @@
{ {
"name": "svelte", "name": "svelte",
"version": "3.4.0", "version": "3.4.1",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

@ -461,7 +461,7 @@ export default class ElementWrapper extends Wrapper {
function ${handler}() { function ${handler}() {
${animation_frame && deindent` ${animation_frame && deindent`
cancelAnimationFrame(${animation_frame}); cancelAnimationFrame(${animation_frame});
if (!${this.var}.paused) ${animation_frame} = requestAnimationFrame(${handler});`} if (!${this.var}.paused) ${animation_frame} = @raf(${handler});`}
${needs_lock && `${lock} = true;`} ${needs_lock && `${lock} = true;`}
ctx.${handler}.call(${this.var}${contextual_dependencies.size > 0 ? ', ctx' : ''}); ctx.${handler}.call(${this.var}${contextual_dependencies.size > 0 ? ', ctx' : ''});
} }

@ -1,4 +1,4 @@
import { now } from './utils.js'; import { now, raf } from './utils.js';
const tasks = new Set(); const tasks = new Set();
let running = false; let running = false;
@ -12,7 +12,7 @@ function run_tasks() {
}); });
running = tasks.size > 0; running = tasks.size > 0;
if (running) requestAnimationFrame(run_tasks); if (running) raf(run_tasks);
} }
export function clear_loops() { export function clear_loops() {
@ -26,7 +26,7 @@ export function loop(fn) {
if (!running) { if (!running) {
running = true; running = true;
requestAnimationFrame(run_tasks); raf(run_tasks);
} }
return { return {

@ -1,4 +1,5 @@
import { element } from './dom.js'; import { element } from './dom.js';
import { raf } from './utils.js';
let stylesheet; let stylesheet;
let active = 0; let active = 0;
@ -56,7 +57,7 @@ export function delete_rule(node, name) {
} }
export function clear_rules() { export function clear_rules() {
requestAnimationFrame(() => { raf(() => {
if (active) return; if (active) return;
let i = stylesheet.cssRules.length; let i = stylesheet.cssRules.length;
while (i--) stylesheet.deleteRule(i); while (i--) stylesheet.deleteRule(i);

@ -80,11 +80,19 @@ export function exclude_internal_props(props) {
return result; return result;
} }
export let now = typeof window !== 'undefined' const is_client = typeof window !== 'undefined';
export let now = is_client
? () => window.performance.now() ? () => window.performance.now()
: () => Date.now(); : () => Date.now();
export let raf = is_client ? requestAnimationFrame : noop;
// used internally for testing // used internally for testing
export function set_now(fn) { export function set_now(fn) {
now = fn; now = fn;
}
export function set_raf(fn) {
raf = fn;
} }

@ -53,4 +53,4 @@ class Component extends SvelteComponentDev {
} }
} }
export default Component; export default Component;

@ -8,6 +8,7 @@ import {
insert, insert,
listen, listen,
noop, noop,
raf,
run_all, run_all,
safe_not_equal, safe_not_equal,
time_ranges_to_array time_ranges_to_array
@ -18,7 +19,7 @@ function create_fragment(ctx) {
function audio_timeupdate_handler() { function audio_timeupdate_handler() {
cancelAnimationFrame(audio_animationframe); cancelAnimationFrame(audio_animationframe);
if (!audio.paused) audio_animationframe = requestAnimationFrame(audio_timeupdate_handler); if (!audio.paused) audio_animationframe = raf(audio_timeupdate_handler);
audio_updating = true; audio_updating = true;
ctx.audio_timeupdate_handler.call(audio); ctx.audio_timeupdate_handler.call(audio);
} }

@ -3,7 +3,7 @@ import * as path from "path";
import * as fs from "fs"; import * as fs from "fs";
import { rollup } from 'rollup'; import { rollup } from 'rollup';
import * as virtual from 'rollup-plugin-virtual'; import * as virtual from 'rollup-plugin-virtual';
import { clear_loops, set_now } from "../../internal.js"; import { clear_loops, set_now, set_raf } from "../../internal.js";
import { import {
showOutput, showOutput,
@ -101,7 +101,7 @@ describe("runtime", () => {
} }
}; };
set_now(() => raf.time); set_now(() => raf.time);
global.requestAnimationFrame = cb => { set_raf(cb => {
let called = false; let called = false;
raf.callback = () => { raf.callback = () => {
if (!called) { if (!called) {
@ -109,7 +109,7 @@ describe("runtime", () => {
cb(); cb();
} }
}; };
}; });
try { try {
mod = require(`./samples/${dir}/main.svelte`); mod = require(`./samples/${dir}/main.svelte`);

Loading…
Cancel
Save