Merge pull request #2782 from sveltejs/gh-2773

ensure spring works server-side
pull/2785/head
Rich Harris 6 years ago committed by GitHub
commit 5d690f83e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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 { loop } from './loop.js';
import { create_rule, delete_rule } from './style_manager.js'; import { create_rule, delete_rule } from './style_manager.js';
@ -12,7 +12,7 @@ export function create_animation(node, from, fn, params) {
delay = 0, delay = 0,
duration = 300, duration = 300,
easing = linear, easing = linear,
start: start_time = window.performance.now() + delay, start: start_time = now() + delay,
end = start_time + duration, end = start_time + duration,
tick = noop, tick = noop,
css css

@ -1,9 +1,11 @@
import { now } from './utils.js';
const tasks = new Set(); const tasks = new Set();
let running = false; let running = false;
function run_tasks() { function run_tasks() {
tasks.forEach(task => { tasks.forEach(task => {
if (!task[0](window.performance.now())) { if (!task[0](now())) {
tasks.delete(task); tasks.delete(task);
task[1](); task[1]();
} }

@ -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 { loop } from './loop.js';
import { create_rule, delete_rule } from './style_manager.js'; import { create_rule, delete_rule } from './style_manager.js';
import { custom_event } from './dom.js'; import { custom_event } from './dom.js';
@ -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++); if (css) animation_name = create_rule(node, 0, 1, duration, delay, easing, css, uid++);
tick(0, 1); tick(0, 1);
const start_time = window.performance.now() + delay; const start_time = now() + delay;
const end_time = start_time + duration; const end_time = start_time + duration;
if (task) task.abort(); if (task) task.abort();
@ -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); 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; const end_time = start_time + duration;
loop(now => { loop(now => {
@ -224,7 +224,7 @@ export function create_bidirectional_transition(node, fn, params, intro) {
} = config; } = config;
const program = { const program = {
start: window.performance.now() + delay, start: now() + delay,
b b
}; };

@ -79,3 +79,12 @@ export function exclude_internal_props(props) {
for (const k in props) if (k[0] !== '$') result[k] = props[k]; for (const k in props) if (k[0] !== '$') result[k] = props[k];
return result; return result;
} }
export let now = typeof window !== 'undefined'
? () => window.performance.now()
: () => Date.now();
// used internally for testing
export function set_now(fn) {
now = fn;
}

@ -1,5 +1,5 @@
import { writable } from 'svelte/store'; // eslint-disable-line import/no-unresolved 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'; import { is_date } from './utils.js';
function tick_spring(ctx, last_value, current_value, target_value) { function tick_spring(ctx, last_value, current_value, target_value) {
@ -51,7 +51,7 @@ export function spring(value, opts = {}) {
if (opts.hard || (spring.stiffness >= 1 && spring.damping >= 1)) { if (opts.hard || (spring.stiffness >= 1 && spring.damping >= 1)) {
cancel_task = true; // cancel any running animation cancel_task = true; // cancel any running animation
last_time = window.performance.now(); last_time = now();
last_value = value; last_value = value;
store.set(value = target_value); store.set(value = target_value);
return new Promise(f => f()); // fulfil immediately return new Promise(f => f()); // fulfil immediately
@ -62,7 +62,7 @@ export function spring(value, opts = {}) {
} }
if (!task) { if (!task) {
last_time = window.performance.now(); last_time = now();
cancel_task = false; cancel_task = false;
task = loop(now => { task = loop(now => {

@ -1,5 +1,5 @@
import { writable } from 'svelte/store'; // eslint-disable-line import/no-unresolved 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 { linear } from 'svelte/easing'; // eslint-disable-line import/no-unresolved
import { is_date } from './utils.js'; import { is_date } from './utils.js';
@ -73,7 +73,7 @@ export function tweened(value, defaults = {}) {
interpolate = get_interpolator interpolate = get_interpolator
} = assign(assign({}, defaults), opts); } = assign(assign({}, defaults), opts);
const start = window.performance.now() + delay; const start = now() + delay;
let fn; let fn;
task = loop(now => { task = loop(now => {

@ -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 } from "../../internal.js"; import { clear_loops, set_now } from "../../internal.js";
import { import {
showOutput, showOutput,
@ -100,7 +100,7 @@ describe("runtime", () => {
if (raf.callback) raf.callback(); if (raf.callback) raf.callback();
} }
}; };
window.performance.now = () => raf.time; set_now(() => raf.time);
global.requestAnimationFrame = cb => { global.requestAnimationFrame = cb => {
let called = false; let called = false;
raf.callback = () => { raf.callback = () => {

@ -0,0 +1,3 @@
export default {
html: `<p>0</p>`
}

@ -0,0 +1,8 @@
<script>
import { spring } from 'svelte/motion';
const x = spring(0);
x.set(1);
</script>
<p>{$x}</p>

@ -102,6 +102,8 @@ describe("ssr", () => {
delete require.cache[file]; delete require.cache[file];
}); });
delete global.window;
const compileOptions = Object.assign({ sveltePath }, config.compileOptions, { const compileOptions = Object.assign({ sveltePath }, config.compileOptions, {
generate: 'ssr' generate: 'ssr'
}); });

Loading…
Cancel
Save