Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

treat requestAnimationFrame as a noop on the server #2856

Merged
merged 2 commits into from
May 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/compile/render-dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ export default class ElementWrapper extends Wrapper {
function ${handler}() {
${animation_frame && deindent`
cancelAnimationFrame(${animation_frame});
if (!${this.var}.paused) ${animation_frame} = requestAnimationFrame(${handler});`}
if (!${this.var}.paused) ${animation_frame} = @raf(${handler});`}
${needs_lock && `${lock} = true;`}
ctx.${handler}.call(${this.var}${contextual_dependencies.size > 0 ? ', ctx' : ''});
}
Expand Down
6 changes: 3 additions & 3 deletions src/internal/loop.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { now } from './utils.js';
import { now, raf } from './utils.js';

const tasks = new Set();
let running = false;
Expand All @@ -12,7 +12,7 @@ function run_tasks() {
});

running = tasks.size > 0;
if (running) requestAnimationFrame(run_tasks);
if (running) raf(run_tasks);
}

export function clear_loops() {
Expand All @@ -26,7 +26,7 @@ export function loop(fn) {

if (!running) {
running = true;
requestAnimationFrame(run_tasks);
raf(run_tasks);
}

return {
Expand Down
3 changes: 2 additions & 1 deletion src/internal/style_manager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { element } from './dom.js';
import { raf } from './utils.js';

let stylesheet;
let active = 0;
Expand Down Expand Up @@ -56,7 +57,7 @@ export function delete_rule(node, name) {
}

export function clear_rules() {
requestAnimationFrame(() => {
raf(() => {
if (active) return;
let i = stylesheet.cssRules.length;
while (i--) stylesheet.deleteRule(i);
Expand Down
10 changes: 9 additions & 1 deletion src/internal/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,19 @@ export function exclude_internal_props(props) {
return result;
}

export let now = typeof window !== 'undefined'
const is_client = typeof window !== 'undefined';

export let now = is_client
? () => window.performance.now()
: () => Date.now();

export let raf = is_client ? requestAnimationFrame : noop;

// used internally for testing
export function set_now(fn) {
now = fn;
}

export function set_raf(fn) {
raf = fn;
}
2 changes: 1 addition & 1 deletion test/js/samples/debug-hoisted/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ class Component extends SvelteComponentDev {
}
}

export default Component;
export default Component;
3 changes: 2 additions & 1 deletion test/js/samples/media-bindings/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
insert,
listen,
noop,
raf,
run_all,
safe_not_equal,
time_ranges_to_array
Expand All @@ -18,7 +19,7 @@ function create_fragment(ctx) {

function audio_timeupdate_handler() {
cancelAnimationFrame(audio_animationframe);
if (!audio.paused) audio_animationframe = requestAnimationFrame(audio_timeupdate_handler);
if (!audio.paused) audio_animationframe = raf(audio_timeupdate_handler);
audio_updating = true;
ctx.audio_timeupdate_handler.call(audio);
}
Expand Down
6 changes: 3 additions & 3 deletions test/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from "path";
import * as fs from "fs";
import { rollup } from 'rollup';
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 {
showOutput,
Expand Down Expand Up @@ -101,15 +101,15 @@ describe("runtime", () => {
}
};
set_now(() => raf.time);
global.requestAnimationFrame = cb => {
set_raf(cb => {
let called = false;
raf.callback = () => {
if (!called) {
called = true;
cb();
}
};
};
});

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