Skip to content

Commit 5d82496

Browse files
authored
internal: add typescript def for transitions (#5625)
1 parent 92fba76 commit 5d82496

File tree

3 files changed

+51
-13
lines changed

3 files changed

+51
-13
lines changed

src/runtime/internal/Component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { blank_object, is_empty, is_function, run, run_all, noop } from './utils
44
import { children, detach, start_hydrating, end_hydrating } from './dom';
55
import { transition_in } from './transitions';
66

7-
interface Fragment {
7+
/**
8+
* INTERNAL, DO NOT USE. Code may change at any time.
9+
*/
10+
export interface Fragment {
811
key: string|null;
912
first: null;
1013
/* create */ c: () => void;

src/runtime/internal/transitions.ts

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,25 @@ import { create_rule, delete_rule } from './style_manager';
55
import { custom_event } from './dom';
66
import { add_render_callback } from './scheduler';
77
import { TransitionConfig } from '../transition';
8+
import { Fragment } from './Component';
89

910
let promise: Promise<void>|null;
11+
type INTRO = 1;
12+
type OUTRO = 0;
13+
interface Outro {
14+
/**
15+
* remaining outros
16+
*/
17+
r: number;
18+
/**
19+
* callbacks
20+
*/
21+
c: Function[];
22+
/**
23+
* parent outro
24+
*/
25+
p: Outro;
26+
}
1027

1128
function wait() {
1229
if (!promise) {
@@ -19,12 +36,12 @@ function wait() {
1936
return promise;
2037
}
2138

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

2643
const outroing = new Set();
27-
let outros;
44+
let outros: Outro;
2845

2946
export function group_outros() {
3047
outros = {
@@ -41,14 +58,14 @@ export function check_outros() {
4158
outros = outros.p;
4259
}
4360

44-
export function transition_in(block, local?: 0 | 1) {
61+
export function transition_in(block: Fragment, local?: 0 | 1) {
4562
if (block && block.i) {
4663
outroing.delete(block);
4764
block.i(local);
4865
}
4966
}
5067

51-
export function transition_out(block, local: 0 | 1, detach?: 0 | 1, callback?) {
68+
export function transition_out(block: Fragment, local: 0 | 1, detach?: 0 | 1, callback?) {
5269
if (block && block.o) {
5370
if (outroing.has(block)) return;
5471
outroing.add(block);
@@ -225,21 +242,39 @@ export function create_out_transition(node: Element & ElementCSSInlineStyle, fn:
225242
};
226243
}
227244

245+
interface PendingProgram {
246+
start: number;
247+
b: INTRO | OUTRO;
248+
group?: Outro;
249+
}
250+
interface Program {
251+
a: number;
252+
b: INTRO | OUTRO;
253+
/**
254+
* direction
255+
*/
256+
d: 1 | -1;
257+
duration: number;
258+
start: number;
259+
end: number;
260+
group?: Outro;
261+
}
262+
228263
export function create_bidirectional_transition(node: Element & ElementCSSInlineStyle, fn: TransitionFn, params: any, intro: boolean) {
229264
let config = fn(node, params);
230265

231266
let t = intro ? 0 : 1;
232267

233-
let running_program = null;
234-
let pending_program = null;
268+
let running_program: Program | null = null;
269+
let pending_program: PendingProgram | null = null;
235270
let animation_name = null;
236271

237272
function clear_animation() {
238273
if (animation_name) delete_rule(node, animation_name);
239274
}
240275

241-
function init(program, duration) {
242-
const d = program.b - t;
276+
function init(program: PendingProgram, duration: number): Program {
277+
const d = (program.b - t) as Program['d'];
243278
duration *= Math.abs(d);
244279

245280
return {
@@ -253,7 +288,7 @@ export function create_bidirectional_transition(node: Element & ElementCSSInline
253288
};
254289
}
255290

256-
function go(b) {
291+
function go(b: INTRO | OUTRO) {
257292
const {
258293
delay = 0,
259294
duration = 300,
@@ -262,7 +297,7 @@ export function create_bidirectional_transition(node: Element & ElementCSSInline
262297
css
263298
} = config || null_transition;
264299

265-
const program = {
300+
const program: PendingProgram = {
266301
start: now() + delay,
267302
b
268303
};
@@ -331,7 +366,7 @@ export function create_bidirectional_transition(node: Element & ElementCSSInline
331366
}
332367

333368
return {
334-
run(b) {
369+
run(b: INTRO | OUTRO) {
335370
if (is_function(config)) {
336371
wait().then(() => {
337372
// @ts-ignore

src/runtime/internal/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function blank_object() {
2828
return Object.create(null);
2929
}
3030

31-
export function run_all(fns) {
31+
export function run_all(fns: Function[]) {
3232
fns.forEach(run);
3333
}
3434

0 commit comments

Comments
 (0)