allow spring/tweened values to be initially undefined - closes #3761

pull/3762/head
Rich Harris 5 years ago
parent ebf7a9024a
commit 714508ccc5

@ -65,7 +65,7 @@ interface Spring<T> extends Readable<T>{
stiffness: number;
}
export function spring<T=any>(value: T, opts: SpringOpts = {}): Spring<T> {
export function spring<T=any>(value?: T, opts: SpringOpts = {}): Spring<T> {
const store = writable(value);
const { stiffness = 0.15, damping = 0.8, precision = 0.01 } = opts;
@ -84,7 +84,7 @@ export function spring<T=any>(value: T, opts: SpringOpts = {}): Spring<T> {
target_value = new_value;
const token = current_token = {};
if (opts.hard || (spring.stiffness >= 1 && spring.damping >= 1)) {
if (value == null || opts.hard || (spring.stiffness >= 1 && spring.damping >= 1)) {
cancel_task = true; // cancel any running animation
last_time = now();
last_value = value;

@ -69,13 +69,18 @@ interface Tweened<T> extends Readable<T> {
update(updater: Updater<T>, opts: Options<T>): Promise<void>;
}
export function tweened<T>(value: T, defaults: Options<T> = {}): Tweened<T> {
export function tweened<T>(value?: T, defaults: Options<T> = {}): Tweened<T> {
const store = writable(value);
let task: Task;
let target_value = value;
function set(new_value: T, opts: Options<T>) {
if (value == null) {
store.set(value = new_value);
return Promise.resolve();
}
target_value = new_value;
let previous_task = task;

@ -0,0 +1,23 @@
import * as assert from 'assert';
import { get } from '../../store';
import { spring, tweened } from '../../motion';
describe('motion', () => {
describe('spring', () => {
it('handles initially undefined values', () => {
const size = spring();
size.set(100);
assert.equal(get(size), 100);
});
});
describe('tweened', () => {
it('handles initially undefined values', () => {
const size = tweened();
size.set(100);
assert.equal(get(size), 100);
});
});
});
Loading…
Cancel
Save