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

add typescript def for transitions #5625

Merged
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
5 changes: 4 additions & 1 deletion src/runtime/internal/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { blank_object, is_empty, is_function, run, run_all, noop } from './utils
import { children, detach } from './dom';
import { transition_in } from './transitions';

interface Fragment {
/**
* INTERNAL, DO NOT USE. Code may change at any time.
*/
export interface Fragment {
key: string|null;
first: null;
/* create */ c: () => void;
Expand Down
57 changes: 46 additions & 11 deletions src/runtime/internal/transitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,25 @@ import { create_rule, delete_rule } from './style_manager';
import { custom_event } from './dom';
import { add_render_callback } from './scheduler';
import { TransitionConfig } from '../transition';
import { Fragment } from './Component';

let promise: Promise<void>|null;
type INTRO = 1;
type OUTRO = 0;
interface Outro {
/**
* remaining outros
*/
r: number;
/**
* callbacks
*/
c: Function[];
/**
* parent outro
*/
p: Outro;
}

function wait() {
if (!promise) {
Expand All @@ -19,12 +36,12 @@ function wait() {
return promise;
}

function dispatch(node: Element, direction: boolean, kind: 'start' | 'end') {
function dispatch(node: Element, direction: INTRO | OUTRO | boolean, kind: 'start' | 'end') {
node.dispatchEvent(custom_event(`${direction ? 'intro' : 'outro'}${kind}`));
}

const outroing = new Set();
let outros;
let outros: Outro;

export function group_outros() {
outros = {
Expand All @@ -41,14 +58,14 @@ export function check_outros() {
outros = outros.p;
}

export function transition_in(block, local?: 0 | 1) {
export function transition_in(block: Fragment, local?: 0 | 1) {
if (block && block.i) {
outroing.delete(block);
block.i(local);
}
}

export function transition_out(block, local: 0 | 1, detach?: 0 | 1, callback?) {
export function transition_out(block: Fragment, local: 0 | 1, detach?: 0 | 1, callback?) {
if (block && block.o) {
if (outroing.has(block)) return;
outroing.add(block);
Expand Down Expand Up @@ -225,21 +242,39 @@ export function create_out_transition(node: Element & ElementCSSInlineStyle, fn:
};
}

interface PendingProgram {
start: number;
b: INTRO | OUTRO;
group?: Outro;
}
interface Program {
a: number;
b: INTRO | OUTRO;
/**
* direction
*/
d: 1 | -1;
duration: number;
start: number;
end: number;
group?: Outro;
}
tanhauhau marked this conversation as resolved.
Show resolved Hide resolved

export function create_bidirectional_transition(node: Element & ElementCSSInlineStyle, fn: TransitionFn, params: any, intro: boolean) {
let config = fn(node, params);

let t = intro ? 0 : 1;

let running_program = null;
let pending_program = null;
let running_program: Program | null = null;
let pending_program: PendingProgram | null = null;
let animation_name = null;

function clear_animation() {
if (animation_name) delete_rule(node, animation_name);
}

function init(program, duration) {
const d = program.b - t;
function init(program: PendingProgram, duration: number): Program {
const d = (program.b - t) as Program['d'];
duration *= Math.abs(d);

return {
Expand All @@ -253,7 +288,7 @@ export function create_bidirectional_transition(node: Element & ElementCSSInline
};
}

function go(b) {
function go(b: INTRO | OUTRO) {
const {
delay = 0,
duration = 300,
Expand All @@ -262,7 +297,7 @@ export function create_bidirectional_transition(node: Element & ElementCSSInline
css
} = config || null_transition;

const program = {
const program: PendingProgram = {
start: now() + delay,
b
};
Expand Down Expand Up @@ -331,7 +366,7 @@ export function create_bidirectional_transition(node: Element & ElementCSSInline
}

return {
run(b) {
run(b: INTRO | OUTRO) {
if (is_function(config)) {
wait().then(() => {
// @ts-ignore
Expand Down
2 changes: 1 addition & 1 deletion src/runtime/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function blank_object() {
return Object.create(null);
}

export function run_all(fns) {
export function run_all(fns: Function[]) {
fns.forEach(run);
}

Expand Down