Skip to content

Commit

Permalink
Merge pull request #2782 from sveltejs/gh-2773
Browse files Browse the repository at this point in the history
ensure spring works server-side
  • Loading branch information
Rich-Harris authored May 16, 2019
2 parents 5121a3c + a0e46ff commit 5d690f8
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/internal/animations.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { identity as linear, noop } from './utils.js';
import { identity as linear, noop, now } from './utils.js';
import { loop } from './loop.js';
import { create_rule, delete_rule } from './style_manager.js';

Expand All @@ -12,7 +12,7 @@ export function create_animation(node, from, fn, params) {
delay = 0,
duration = 300,
easing = linear,
start: start_time = window.performance.now() + delay,
start: start_time = now() + delay,
end = start_time + duration,
tick = noop,
css
Expand Down
4 changes: 3 additions & 1 deletion src/internal/loop.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { now } from './utils.js';

const tasks = new Set();
let running = false;

function run_tasks() {
tasks.forEach(task => {
if (!task[0](window.performance.now())) {
if (!task[0](now())) {
tasks.delete(task);
task[1]();
}
Expand Down
8 changes: 4 additions & 4 deletions src/internal/transitions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { identity as linear, noop, run_all } from './utils.js';
import { identity as linear, noop, now, run_all } from './utils.js';
import { loop } from './loop.js';
import { create_rule, delete_rule } from './style_manager.js';
import { custom_event } from './dom.js';
Expand Down Expand Up @@ -63,7 +63,7 @@ export function create_in_transition(node, fn, params) {
if (css) animation_name = create_rule(node, 0, 1, duration, delay, easing, css, uid++);
tick(0, 1);

const start_time = window.performance.now() + delay;
const start_time = now() + delay;
const end_time = start_time + duration;

if (task) task.abort();
Expand Down Expand Up @@ -136,7 +136,7 @@ export function create_out_transition(node, fn, params) {

if (css) animation_name = create_rule(node, 1, 0, duration, delay, easing, css);

const start_time = window.performance.now() + delay;
const start_time = now() + delay;
const end_time = start_time + duration;

loop(now => {
Expand Down Expand Up @@ -224,7 +224,7 @@ export function create_bidirectional_transition(node, fn, params, intro) {
} = config;

const program = {
start: window.performance.now() + delay,
start: now() + delay,
b
};

Expand Down
9 changes: 9 additions & 0 deletions src/internal/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,12 @@ export function exclude_internal_props(props) {
for (const k in props) if (k[0] !== '$') result[k] = props[k];
return result;
}

export let now = typeof window !== 'undefined'
? () => window.performance.now()
: () => Date.now();

// used internally for testing
export function set_now(fn) {
now = fn;
}
6 changes: 3 additions & 3 deletions src/motion/spring.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writable } from 'svelte/store'; // eslint-disable-line import/no-unresolved
import { loop } from 'svelte/internal'; // eslint-disable-line import/no-unresolved
import { loop, now } from 'svelte/internal'; // eslint-disable-line import/no-unresolved
import { is_date } from './utils.js';

function tick_spring(ctx, last_value, current_value, target_value) {
Expand Down Expand Up @@ -51,7 +51,7 @@ export function spring(value, opts = {}) {

if (opts.hard || (spring.stiffness >= 1 && spring.damping >= 1)) {
cancel_task = true; // cancel any running animation
last_time = window.performance.now();
last_time = now();
last_value = value;
store.set(value = target_value);
return new Promise(f => f()); // fulfil immediately
Expand All @@ -62,7 +62,7 @@ export function spring(value, opts = {}) {
}

if (!task) {
last_time = window.performance.now();
last_time = now();
cancel_task = false;

task = loop(now => {
Expand Down
4 changes: 2 additions & 2 deletions src/motion/tweened.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { writable } from 'svelte/store'; // eslint-disable-line import/no-unresolved
import { assign, loop } from 'svelte/internal'; // eslint-disable-line import/no-unresolved
import { assign, loop, now } from 'svelte/internal'; // eslint-disable-line import/no-unresolved
import { linear } from 'svelte/easing'; // eslint-disable-line import/no-unresolved
import { is_date } from './utils.js';

Expand Down Expand Up @@ -73,7 +73,7 @@ export function tweened(value, defaults = {}) {
interpolate = get_interpolator
} = assign(assign({}, defaults), opts);

const start = window.performance.now() + delay;
const start = now() + delay;
let fn;

task = loop(now => {
Expand Down
4 changes: 2 additions & 2 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 } from "../../internal.js";
import { clear_loops, set_now } from "../../internal.js";

import {
showOutput,
Expand Down Expand Up @@ -100,7 +100,7 @@ describe("runtime", () => {
if (raf.callback) raf.callback();
}
};
window.performance.now = () => raf.time;
set_now(() => raf.time);
global.requestAnimationFrame = cb => {
let called = false;
raf.callback = () => {
Expand Down
3 changes: 3 additions & 0 deletions test/runtime/samples/spring/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
html: `<p>0</p>`
}
8 changes: 8 additions & 0 deletions test/runtime/samples/spring/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
import { spring } from 'svelte/motion';

const x = spring(0);
x.set(1);
</script>

<p>{$x}</p>
2 changes: 2 additions & 0 deletions test/server-side-rendering/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ describe("ssr", () => {
delete require.cache[file];
});

delete global.window;

const compileOptions = Object.assign({ sveltePath }, config.compileOptions, {
generate: 'ssr'
});
Expand Down

0 comments on commit 5d690f8

Please sign in to comment.