chore: simplify transitions (#10798)

* replace transition code

* get rid of some stuff

* simplify

* remove some junk

* not sure how we solved this before, but i guess this makes sense

* oh hey i don't think we need this

* make elseif transparent for transition purposes

* oops

* edge case

* fix

* do not want

* rename

* transition out ecah blocks when emptying

* baby steps

* hydration fix

* tidy up

* tidy up

* tidy up

* fallbacks

* man i love deleting code

* tidy up

* note to self

* why was this an effect

* tidy up

* tidy up

* key blocks

* temporary

* fix

* WIP

* fix

* simplify

* emit events

* delete delete delete

* destroy child effects

* simplify helper

* simplify helper

* fix

* remove commented out code

* fix wonky test

* fix test

* fix test

* fix test

* dynamic components

* fix test

* await

* tidy up

* fix

* fix

* fix test

* tidy up

* we dont need to reconcile during hydration

* simplify

* tidy up

* fix

* reduce indentation

* simplify

* remove some junk

* remove some junk

* simplify

* tidy up

* prefer while over do-while (this appears to have the same behaviour)

* group fast paths

* rename

* unused import

* unused exports

* try this

* simplify

* simplify

* simplify

* simplify

* tidy up

* simplify

* simplify

* tidy up

* simplify

* simplify

* more compact names

* simplify

* better comments

* simplify

* tidy up

* we don't actually gain anything from this

* fix binding group order bug (revealed by previous commit, but exists separately from it)

* tidy up

* simplify

* tidy up

* remove some junk

* simplify

* note to self

* tidy up

* revert this bit

* tidy up

* simplify

* simplify

* simplify

* symmetry

* tidy up

* var

* rename some stuff

* tidy up

* simplify

* keyed each transitions

* make elements inert

* deferred transitions

* fix

* fix test

* fix some tests

* simplify

* fix

* fix test

* fix

* eh that'll do for now

* fix

* revert all these random changes

* fix

* fix

* simplify

* tidy up

* simplify

* simplify

* tidy up

* tidy up

* tidy up

* WIP

* WIP

* working

* tidy up

* fix

* tidy up

* tidy up

* lerp

* tidy up

* rename

* rename

* almost everything working

* tidy up

* ALL TESTS PASSING

* fix treeshaking

* Apply suggestions from code review

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>

* comment

* explain elseif locality

* explain flushSync

* comments

* this is accounted for

* add some comments

* remove outdated comment

* add comment

* add comments

* rename

* a few naming tweaks

* explain each_item_block stuff

* remove unused arg

* optimise

* add some comments

* fix test post-optimisation

* explicit comparisons

* some docs

* fix intro events

* move effect.ran into the bitmask

* docs

* rename run_transitions to should_intro, add explanatory jsdoc

* add some more docs

* remove animation before measuring

* only animate blocks that persist

* note to self

---------

Co-authored-by: Simon H <5968653+dummdidumm@users.noreply.github.com>
pull/10870/head
Rich Harris 4 months ago committed by GitHub
parent f6eca83b7c
commit e8ce41815a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -11,3 +11,5 @@
/motion.d.ts
/store.d.ts
/transition.d.ts
/scripts/_bundle.js

@ -74,7 +74,7 @@ for (const key in pkg.exports) {
}
const client_main = path.resolve(pkg.exports['.'].browser);
const without_hydration = await bundle_code(
const bundle = await bundle_code(
// Use all features which contain hydration code to ensure it's treeshakeable
compile(
`
@ -109,15 +109,17 @@ const without_hydration = await bundle_code(
).js.code
);
if (!without_hydration.includes('current_hydration_fragment')) {
if (!bundle.includes('current_hydration_fragment')) {
// eslint-disable-next-line no-console
console.error(`✅ Hydration code treeshakeable`);
} else {
// eslint-disable-next-line no-console
console.error(without_hydration);
console.error(bundle);
// eslint-disable-next-line no-console
console.error(`❌ Hydration code not treeshakeable`);
failed = true;
fs.writeFileSync('scripts/_bundle.js', bundle);
}
// eslint-disable-next-line no-console

@ -28,10 +28,14 @@ import {
AttributeAliases,
DOMBooleanAttributes,
EACH_INDEX_REACTIVE,
EACH_IS_ANIMATED,
EACH_IS_CONTROLLED,
EACH_IS_STRICT_EQUALS,
EACH_ITEM_REACTIVE,
EACH_KEYED
EACH_KEYED,
TRANSITION_GLOBAL,
TRANSITION_IN,
TRANSITION_OUT
} from '../../../../../constants.js';
import { regex_is_valid_identifier } from '../../../patterns.js';
import { javascript_visitors_runes } from './javascript-runes.js';
@ -1922,7 +1926,7 @@ export const template_visitors = {
state.init.push(
b.stmt(
b.call(
'$.animate',
'$.animation',
state.node,
b.thunk(
/** @type {import('estree').Expression} */ (visit(parse_directive_name(node.name)))
@ -1939,25 +1943,21 @@ export const template_visitors = {
error(node, 'INTERNAL', 'Node should have been handled elsewhere');
},
TransitionDirective(node, { state, visit }) {
const type = node.intro && node.outro ? '$.transition' : node.intro ? '$.in' : '$.out';
const expression =
node.expression === null
? b.literal(null)
: b.thunk(/** @type {import('estree').Expression} */ (visit(node.expression)));
let flags = node.modifiers.includes('global') ? TRANSITION_GLOBAL : 0;
if (node.intro) flags |= TRANSITION_IN;
if (node.outro) flags |= TRANSITION_OUT;
state.init.push(
b.stmt(
b.call(
type,
state.node,
b.thunk(
/** @type {import('estree').Expression} */ (visit(parse_directive_name(node.name)))
),
expression,
node.modifiers.includes('global') ? b.true : b.false
)
)
);
const args = [
b.literal(flags),
state.node,
b.thunk(/** @type {import('estree').Expression} */ (visit(parse_directive_name(node.name))))
];
if (node.expression) {
args.push(b.thunk(/** @type {import('estree').Expression} */ (visit(node.expression))));
}
state.init.push(b.stmt(b.call('$.transition', ...args)));
},
RegularElement(node, context) {
if (node.name === 'noscript') {
@ -2345,6 +2345,19 @@ export const template_visitors = {
each_type |= EACH_ITEM_REACTIVE;
}
// Since `animate:` can only appear on elements that are the sole child of a keyed each block,
// we can determine at compile time whether the each block is animated or not (in which
// case it should measure animated elements before and after reconciliation).
if (
node.key &&
node.body.nodes.some((child) => {
if (child.type !== 'RegularElement' && child.type !== 'SvelteElement') return false;
return child.attributes.some((attr) => attr.type === 'AnimateDirective');
})
) {
each_type |= EACH_IS_ANIMATED;
}
if (each_node_meta.is_controlled) {
each_type |= EACH_IS_CONTROLLED;
}
@ -2557,22 +2570,44 @@ export const template_visitors = {
context.visit(node.consequent)
);
context.state.after_update.push(
b.stmt(
b.call(
'$.if',
context.state.node,
b.thunk(/** @type {import('estree').Expression} */ (context.visit(node.test))),
b.arrow([b.id('$$anchor')], consequent),
node.alternate
? b.arrow(
[b.id('$$anchor')],
/** @type {import('estree').BlockStatement} */ (context.visit(node.alternate))
)
: b.literal(null)
)
)
);
const args = [
context.state.node,
b.thunk(/** @type {import('estree').Expression} */ (context.visit(node.test))),
b.arrow([b.id('$$anchor')], consequent),
node.alternate
? b.arrow(
[b.id('$$anchor')],
/** @type {import('estree').BlockStatement} */ (context.visit(node.alternate))
)
: b.literal(null)
];
if (node.elseif) {
// We treat this...
//
// {#if x}
// ...
// {:else}
// {#if y}
// <div transition:foo>...</div>
// {/if}
// {/if}
//
// ...slightly differently to this...
//
// {#if x}
// ...
// {:else if y}
// <div transition:foo>...</div>
// {/if}
//
// ...even though they're logically equivalent. In the first case, the
// transition will only play when `y` changes, but in the second it
// should play when `x` or `y` change — both are considered 'local'
args.push(b.literal(true));
}
context.state.after_update.push(b.stmt(b.call('$.if', ...args)));
},
AwaitBlock(node, context) {
context.state.template.push('<!>');

@ -12,6 +12,10 @@ export const PROPS_IS_RUNES = 1 << 1;
export const PROPS_IS_UPDATED = 1 << 2;
export const PROPS_IS_LAZY_INITIAL = 1 << 3;
export const TRANSITION_IN = 1;
export const TRANSITION_OUT = 1 << 1;
export const TRANSITION_GLOBAL = 1 << 2;
/** List of Element events that will be delegated */
export const DelegatedEvents = [
'beforeinput',

@ -9,17 +9,8 @@ export const DIRTY = 1 << 9;
export const MAYBE_DIRTY = 1 << 10;
export const INERT = 1 << 11;
export const DESTROYED = 1 << 12;
export const ROOT_BLOCK = 0;
export const IF_BLOCK = 1;
export const EACH_BLOCK = 2;
export const EACH_ITEM_BLOCK = 3;
export const AWAIT_BLOCK = 4;
export const KEY_BLOCK = 5;
export const HEAD_BLOCK = 6;
export const DYNAMIC_COMPONENT_BLOCK = 7;
export const DYNAMIC_ELEMENT_BLOCK = 8;
export const SNIPPET_BLOCK = 9;
export const IS_ELSEIF = 1 << 13;
export const EFFECT_RAN = 1 << 14;
export const UNINITIALIZED = Symbol();
export const STATE_SYMBOL = Symbol('$state');

@ -1,10 +1,16 @@
import { is_promise } from '../../../common.js';
import { hydrate_block_anchor } from '../hydration.js';
import { remove } from '../reconciler.js';
import { current_block, execute_effect, flushSync } from '../../runtime.js';
import { destroy_effect, render_effect } from '../../reactivity/effects.js';
import { trigger_transitions } from '../elements/transitions.js';
import { AWAIT_BLOCK, UNINITIALIZED } from '../../constants.js';
import {
current_block,
current_component_context,
flushSync,
set_current_component_context,
set_current_effect,
set_current_reaction
} from '../../runtime.js';
import { destroy_effect, pause_effect, render_effect } from '../../reactivity/effects.js';
import { DESTROYED, INERT } from '../../constants.js';
/** @returns {import('../../types.js').AwaitBlock} */
export function create_await_block() {
@ -16,179 +22,125 @@ export function create_await_block() {
// parent
p: /** @type {import('../../types.js').Block} */ (current_block),
// pending
n: true,
// transition
r: null,
// type
t: AWAIT_BLOCK
n: true
};
}
/**
* @template V
* @param {Comment} anchor_node
* @param {(() => Promise<V>)} input
* @param {Comment} anchor
* @param {(() => Promise<V>)} get_input
* @param {null | ((anchor: Node) => void)} pending_fn
* @param {null | ((anchor: Node, value: V) => void)} then_fn
* @param {null | ((anchor: Node, error: unknown) => void)} catch_fn
* @returns {void}
*/
export function await_block(anchor_node, input, pending_fn, then_fn, catch_fn) {
export function await_block(anchor, get_input, pending_fn, then_fn, catch_fn) {
const block = create_await_block();
/** @type {null | import('../../types.js').Render} */
let current_render = null;
hydrate_block_anchor(anchor_node);
/** @type {{}} */
let latest_token;
/** @type {typeof UNINITIALIZED | V} */
let resolved_value = UNINITIALIZED;
/** @type {unknown} */
let error = UNINITIALIZED;
let pending = false;
block.r =
/**
* @param {import('../../types.js').Transition} transition
* @returns {void}
*/
(transition) => {
const render = /** @type {import('../../types.js').Render} */ (current_render);
const transitions = render.s;
transitions.add(transition);
transition.f(() => {
transitions.delete(transition);
if (transitions.size === 0) {
// If the current render has changed since, then we can remove the old render
// effect as it's stale.
if (current_render !== render && render.e !== null) {
if (render.d !== null) {
remove(render.d);
render.d = null;
}
destroy_effect(render.e);
render.e = null;
}
}
});
};
const create_render_effect = () => {
/** @type {import('../../types.js').Render} */
const render = {
d: null,
e: null,
s: new Set(),
p: current_render
};
const effect = render_effect(
() => {
if (error === UNINITIALIZED) {
if (resolved_value === UNINITIALIZED) {
// pending = true
block.n = true;
if (pending_fn !== null) {
pending_fn(anchor_node);
}
} else if (then_fn !== null) {
// pending = false
block.n = false;
then_fn(anchor_node, resolved_value);
}
} else if (catch_fn !== null) {
// pending = false
block.n = false;
catch_fn(anchor_node, error);
const component_context = current_component_context;
hydrate_block_anchor(anchor);
/** @type {any} */
let input;
/** @type {import('#client').Effect | null} */
let pending_effect;
/** @type {import('#client').Effect | null} */
let then_effect;
/** @type {import('#client').Effect | null} */
let catch_effect;
/**
* @param {(anchor: Comment, value: any) => void} fn
* @param {any} value
*/
function create_effect(fn, value) {
set_current_effect(branch);
set_current_reaction(branch); // TODO do we need both?
set_current_component_context(component_context);
var effect = render_effect(() => fn(anchor, value), {}, true);
set_current_component_context(null);
set_current_reaction(null);
set_current_effect(null);
// without this, the DOM does not update until two ticks after the promise,
// resolves which is unexpected behaviour (and somewhat irksome to test)
flushSync();
return effect;
}
/** @param {import('#client').Effect} effect */
function pause(effect) {
if ((effect.f & DESTROYED) !== 0) return;
const block = effect.block;
pause_effect(effect, () => {
// TODO make this unnecessary
const dom = block?.d;
if (dom) remove(dom);
});
}
const branch = render_effect(() => {
if (input === (input = get_input())) return;
if (is_promise(input)) {
const promise = /** @type {Promise<any>} */ (input);
if (pending_fn) {
if (pending_effect && (pending_effect.f & INERT) === 0) {
if (pending_effect.block?.d) remove(pending_effect.block.d);
destroy_effect(pending_effect);
}
render.d = block.d;
block.d = null;
},
block,
true,
true
);
render.e = effect;
current_render = render;
};
const render = () => {
const render = current_render;
if (render === null) {
create_render_effect();
return;
}
const transitions = render.s;
if (transitions.size === 0) {
if (render.d !== null) {
remove(render.d);
render.d = null;
}
if (render.e) {
execute_effect(render.e);
} else {
create_render_effect();
pending_effect = render_effect(() => pending_fn(anchor), {}, true);
}
} else {
create_render_effect();
trigger_transitions(transitions, 'out');
}
};
const await_effect = render_effect(
() => {
const token = {};
latest_token = token;
const promise = input();
if (is_promise(promise)) {
promise.then(
/** @param {V} v */
(v) => {
if (latest_token === token) {
// Ensure UI is in sync before resolving value.
flushSync();
resolved_value = v;
pending = false;
render();
}
},
/** @param {unknown} _error */
(_error) => {
error = _error;
pending = false;
render();
if (then_effect) pause(then_effect);
if (catch_effect) pause(catch_effect);
promise.then(
(value) => {
if (promise !== input) return;
if (pending_effect) pause(pending_effect);
if (then_fn) {
then_effect = create_effect(then_fn, value);
}
},
(error) => {
if (promise !== input) return;
if (pending_effect) pause(pending_effect);
if (catch_fn) {
catch_effect = create_effect(catch_fn, error);
}
);
if (resolved_value !== UNINITIALIZED || error !== UNINITIALIZED) {
error = UNINITIALIZED;
resolved_value = UNINITIALIZED;
}
if (!pending) {
pending = true;
render();
);
} else {
if (pending_effect) pause(pending_effect);
if (catch_effect) pause(catch_effect);
if (then_fn) {
if (then_effect) {
if (then_effect.block?.d) remove(then_effect.block.d);
destroy_effect(then_effect);
}
} else {
error = UNINITIALIZED;
resolved_value = promise;
pending = false;
render();
}
},
block,
false
);
await_effect.ondestroy = () => {
let render = current_render;
latest_token = {};
while (render !== null) {
const dom = render.d;
if (dom !== null) {
remove(dom);
}
const effect = render.e;
if (effect !== null) {
destroy_effect(effect);
then_effect = render_effect(() => then_fn(anchor, input), {}, true);
}
render = render.p;
}
}, block);
branch.ondestroy = () => {
// TODO this sucks, tidy it up
if (pending_effect?.block?.d) remove(pending_effect.block.d);
if (then_effect?.block?.d) remove(then_effect.block.d);
if (catch_effect?.block?.d) remove(catch_effect.block.d);
};
block.e = await_effect;
}

@ -2,7 +2,7 @@ import { namespace_svg } from '../../../../constants.js';
import { current_hydration_fragment, hydrate_block_anchor, hydrating } from '../hydration.js';
import { empty } from '../operations.js';
import { render_effect } from '../../reactivity/effects.js';
import { insert, remove } from '../reconciler.js';
import { remove } from '../reconciler.js';
/**
* @param {Element | Text | Comment} anchor
@ -33,7 +33,7 @@ export function css_props(anchor, is_html, props, component) {
tag = document.createElementNS(namespace_svg, 'g');
}
insert(tag, null, anchor);
anchor.before(tag);
component_anchor = empty();
tag.appendChild(component_anchor);
}

File diff suppressed because it is too large Load Diff

@ -1,4 +1,4 @@
import { IF_BLOCK } from '../../constants.js';
import { IS_ELSEIF } from '../../constants.js';
import {
current_hydration_fragment,
hydrate_block_anchor,
@ -6,50 +6,40 @@ import {
set_current_hydration_fragment
} from '../hydration.js';
import { remove } from '../reconciler.js';
import { current_block, execute_effect } from '../../runtime.js';
import { destroy_effect, render_effect } from '../../reactivity/effects.js';
import { trigger_transitions } from '../elements/transitions.js';
import { current_block } from '../../runtime.js';
import {
destroy_effect,
pause_effect,
render_effect,
resume_effect
} from '../../reactivity/effects.js';
/** @returns {import('#client').IfBlock} */
function create_if_block() {
return {
// alternate transitions
a: null,
// alternate effect
ae: null,
// consequent transitions
c: null,
// consequent effect
ce: null,
// dom
d: null,
// effect
e: null,
// parent
p: /** @type {import('#client').Block} */ (current_block),
// transition
r: null,
// type
t: IF_BLOCK,
// value
v: false
};
}
/**
* @param {Comment} anchor_node
* @param {() => boolean} condition_fn
* @param {Comment} anchor
* @param {() => boolean} get_condition
* @param {(anchor: Node) => void} consequent_fn
* @param {null | ((anchor: Node) => void)} alternate_fn
* @param {boolean} [elseif] True if this is an `{:else if ...}` block rather than an `{#if ...}`, as that affects which transitions are considered 'local'
* @returns {void}
*/
export function if_block(anchor_node, condition_fn, consequent_fn, alternate_fn) {
export function if_block(anchor, get_condition, consequent_fn, alternate_fn, elseif = false) {
const block = create_if_block();
hydrate_block_anchor(anchor_node);
/** Whether or not there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
let mismatch = false;
hydrate_block_anchor(anchor);
/** @type {null | import('#client').TemplateNode | Array<import('#client').TemplateNode>} */
let consequent_dom = null;
@ -57,134 +47,118 @@ export function if_block(anchor_node, condition_fn, consequent_fn, alternate_fn)
/** @type {null | import('#client').TemplateNode | Array<import('#client').TemplateNode>} */
let alternate_dom = null;
let has_mounted = false;
/** @type {import('#client').Effect | null} */
let consequent_effect = null;
/**
* @type {import('#client').Effect | null}
*/
let current_branch_effect = null;
/** @type {import('#client').Effect | null} */
let alternate_effect = null;
/** @type {import('#client').Effect} */
let consequent_effect;
/** @type {import('#client').Effect} */
let alternate_effect;
/** @type {boolean | null} */
let condition = null;
const if_effect = render_effect(() => {
const result = !!condition_fn();
if (block.v !== result || !has_mounted) {
block.v = result;
if (has_mounted) {
const consequent_transitions = block.c;
const alternate_transitions = block.a;
if (result) {
if (alternate_transitions === null || alternate_transitions.size === 0) {
execute_effect(alternate_effect);
} else {
trigger_transitions(alternate_transitions, 'out');
}
if (consequent_transitions === null || consequent_transitions.size === 0) {
execute_effect(consequent_effect);
} else {
trigger_transitions(consequent_transitions, 'in');
}
} else {
if (consequent_transitions === null || consequent_transitions.size === 0) {
execute_effect(consequent_effect);
} else {
trigger_transitions(consequent_transitions, 'out');
}
if (alternate_transitions === null || alternate_transitions.size === 0) {
execute_effect(alternate_effect);
} else {
trigger_transitions(alternate_transitions, 'in');
}
}
} else if (hydrating) {
const comment_text = /** @type {Comment} */ (current_hydration_fragment?.[0])?.data;
if (
!comment_text ||
(comment_text === 'ssr:if:true' && !result) ||
(comment_text === 'ssr:if:false' && result)
) {
// Hydration mismatch: remove everything inside the anchor and start fresh.
// This could happen using when `{#if browser} .. {/if}` in SvelteKit.
remove(current_hydration_fragment);
set_current_hydration_fragment(null);
mismatch = true;
} else {
// Remove the ssr:if comment node or else it will confuse the subsequent hydration algorithm
current_hydration_fragment.shift();
}
if (condition === (condition = !!get_condition())) return;
/** Whether or not there was a hydration mismatch. Needs to be a `let` or else it isn't treeshaken out */
let mismatch = false;
if (hydrating) {
const comment_text = /** @type {Comment} */ (current_hydration_fragment?.[0])?.data;
if (
!comment_text ||
(comment_text === 'ssr:if:true' && !condition) ||
(comment_text === 'ssr:if:false' && condition)
) {
// Hydration mismatch: remove everything inside the anchor and start fresh.
// This could happen using when `{#if browser} .. {/if}` in SvelteKit.
remove(current_hydration_fragment);
set_current_hydration_fragment(null);
mismatch = true;
} else {
// Remove the ssr:if comment node or else it will confuse the subsequent hydration algorithm
current_hydration_fragment.shift();
}
has_mounted = true;
}
// create these here so they have the correct parent/child relationship
consequent_effect ??= render_effect(
(/** @type {any} */ _, /** @type {import('#client').Effect | null} */ consequent_effect) => {
const result = block.v;
if (!result && consequent_dom !== null) {
remove(consequent_dom);
consequent_dom = null;
}
if (result && current_branch_effect !== consequent_effect) {
consequent_fn(anchor_node);
if (mismatch && current_branch_effect === null) {
// Set fragment so that Svelte continues to operate in hydration mode
set_current_hydration_fragment([]);
}
current_branch_effect = consequent_effect;
consequent_dom = block.d;
}
block.d = null;
},
block,
true
);
block.ce = consequent_effect;
alternate_effect ??= render_effect(
(/** @type {any} */ _, /** @type {import('#client').Effect | null} */ alternate_effect) => {
const result = block.v;
if (result && alternate_dom !== null) {
remove(alternate_dom);
alternate_dom = null;
}
if (!result && current_branch_effect !== alternate_effect) {
if (alternate_fn !== null) {
alternate_fn(anchor_node);
}
if (mismatch && current_branch_effect === null) {
// Set fragment so that Svelte continues to operate in hydration mode
set_current_hydration_fragment([]);
}
current_branch_effect = alternate_effect;
alternate_dom = block.d;
}
block.d = null;
},
block,
true
);
block.ae = alternate_effect;
if (condition) {
if (consequent_effect) {
resume_effect(consequent_effect);
} else {
consequent_effect = render_effect(
() => {
consequent_fn(anchor);
consequent_dom = block.d;
if (mismatch) {
// Set fragment so that Svelte continues to operate in hydration mode
set_current_hydration_fragment([]);
}
return () => {
// TODO make this unnecessary by linking the dom to the effect,
// and removing automatically on teardown
if (consequent_dom !== null) {
remove(consequent_dom);
consequent_dom = null;
}
};
},
block,
true
);
}
if (alternate_effect) {
pause_effect(alternate_effect, () => {
alternate_effect = null;
if (alternate_dom) remove(alternate_dom);
});
}
} else {
if (alternate_effect) {
resume_effect(alternate_effect);
} else if (alternate_fn) {
alternate_effect = render_effect(
() => {
alternate_fn(anchor);
alternate_dom = block.d;
if (mismatch) {
// Set fragment so that Svelte continues to operate in hydration mode
set_current_hydration_fragment([]);
}
return () => {
// TODO make this unnecessary by linking the dom to the effect,
// and removing automatically on teardown
if (alternate_dom !== null) {
remove(alternate_dom);
alternate_dom = null;
}
};
},
block,
true
);
}
if (consequent_effect) {
pause_effect(consequent_effect, () => {
consequent_effect = null;
if (consequent_dom) remove(consequent_dom);
});
}
}
}, block);
if (elseif) {
if_effect.f |= IS_ELSEIF;
}
if_effect.ondestroy = () => {
// TODO make this unnecessary by linking the dom to the effect,
// and removing automatically on teardown
if (consequent_dom !== null) {
remove(consequent_dom);
}
@ -193,8 +167,12 @@ export function if_block(anchor_node, condition_fn, consequent_fn, alternate_fn)
remove(alternate_dom);
}
destroy_effect(consequent_effect);
destroy_effect(alternate_effect);
if (consequent_effect) {
destroy_effect(consequent_effect);
}
if (alternate_effect) {
destroy_effect(alternate_effect);
}
};
block.e = if_effect;

@ -1,140 +1,76 @@
import { UNINITIALIZED, KEY_BLOCK } from '../../constants.js';
import { UNINITIALIZED } from '../../constants.js';
import { hydrate_block_anchor } from '../hydration.js';
import { remove } from '../reconciler.js';
import { current_block, execute_effect } from '../../runtime.js';
import { destroy_effect, render_effect } from '../../reactivity/effects.js';
import { trigger_transitions } from '../elements/transitions.js';
import { pause_effect, render_effect } from '../../reactivity/effects.js';
import { safe_not_equal } from '../../reactivity/equality.js';
/** @returns {import('../../types.js').KeyBlock} */
function create_key_block() {
return {
// dom
d: null,
// effect
e: null,
// parent
p: /** @type {import('../../types.js').Block} */ (current_block),
// transition
r: null,
// type
t: KEY_BLOCK
};
}
/**
* @template V
* @param {Comment} anchor_node
* @param {() => V} key
* @param {Comment} anchor
* @param {() => V} get_key
* @param {(anchor: Node) => void} render_fn
* @returns {void}
*/
export function key_block(anchor_node, key, render_fn) {
const block = create_key_block();
export function key_block(anchor, get_key, render_fn) {
const block = {};
/** @type {null | import('../../types.js').Render} */
let current_render = null;
hydrate_block_anchor(anchor_node);
hydrate_block_anchor(anchor);
/** @type {V | typeof UNINITIALIZED} */
let key_value = UNINITIALIZED;
let mounted = false;
block.r =
/**
* @param {import('../../types.js').Transition} transition
* @returns {void}
*/
(transition) => {
const render = /** @type {import('../../types.js').Render} */ (current_render);
const transitions = render.s;
transitions.add(transition);
transition.f(() => {
transitions.delete(transition);
if (transitions.size === 0) {
// If the current render has changed since, then we can remove the old render
// effect as it's stale.
if (current_render !== render && render.e !== null) {
if (render.d !== null) {
remove(render.d);
render.d = null;
}
destroy_effect(render.e);
render.e = null;
}
}
});
};
const create_render_effect = () => {
/** @type {import('../../types.js').Render} */
const render = {
d: null,
e: null,
s: new Set(),
p: current_render
};
const effect = render_effect(
() => {
render_fn(anchor_node);
render.d = block.d;
block.d = null;
},
block,
true,
true
);
render.e = effect;
current_render = render;
};
const render = () => {
const render = current_render;
if (render === null) {
create_render_effect();
return;
}
const transitions = render.s;
if (transitions.size === 0) {
if (render.d !== null) {
remove(render.d);
render.d = null;
}
if (render.e) {
execute_effect(render.e);
} else {
create_render_effect();
}
} else {
trigger_transitions(transitions, 'out');
create_render_effect();
}
};
let key = UNINITIALIZED;
/** @type {import('#client').Effect} */
let effect;
/**
* Every time `key` changes, we create a new effect. Old effects are
* removed from this set when they have fully transitioned out
* @type {Set<import('#client').Effect>}
*/
let effects = new Set();
const key_effect = render_effect(
() => {
const prev_key_value = key_value;
key_value = key();
if (mounted && safe_not_equal(prev_key_value, key_value)) {
render();
if (safe_not_equal(key, (key = get_key()))) {
if (effect) {
var e = effect;
pause_effect(e, () => {
effects.delete(e);
});
}
effect = render_effect(
() => {
render_fn(anchor);
// @ts-expect-error TODO this should be unnecessary
const dom = block.d;
return () => {
if (dom !== null) {
remove(dom);
}
};
},
block,
true,
true
);
// @ts-expect-error TODO tidy up
effect.d = block.d;
effects.add(effect);
}
},
block,
false
);
// To ensure topological ordering of the key effect to the render effect,
// we trigger the effect after.
render();
mounted = true;
key_effect.ondestroy = () => {
let render = current_render;
while (render !== null) {
const dom = render.d;
if (dom !== null) {
remove(dom);
}
const effect = render.e;
if (effect !== null) {
destroy_effect(effect);
}
render = render.p;
for (const e of effects) {
// @ts-expect-error TODO tidy up. ondestroy should be totally unnecessary
if (e.d) remove(e.d);
}
};
block.e = key_effect;
}

@ -1,4 +1,3 @@
import { SNIPPET_BLOCK } from '../../constants.js';
import { render_effect } from '../../reactivity/effects.js';
import { remove } from '../reconciler.js';
import { current_block, untrack } from '../../runtime.js';
@ -17,11 +16,7 @@ export function snippet(get_snippet, node, ...args) {
// parent
p: /** @type {import('#client').Block} */ (current_block),
// effect
e: null,
// transition
r: null,
// type
t: SNIPPET_BLOCK
e: null
};
render_effect(() => {

@ -1,127 +1,69 @@
import { DYNAMIC_COMPONENT_BLOCK } from '../../constants.js';
import { hydrate_block_anchor } from '../hydration.js';
import { destroy_effect, render_effect } from '../../reactivity/effects.js';
import { pause_effect, render_effect } from '../../reactivity/effects.js';
import { remove } from '../reconciler.js';
import { current_block, execute_effect } from '../../runtime.js';
import { trigger_transitions } from '../elements/transitions.js';
// TODO this is very similar to `key`, can we deduplicate?
/**
* @template P
* @param {Comment} anchor_node
* @param {() => (props: P) => void} component_fn
* @param {(component: (props: P) => void) => void} render_fn
* @template {(props: P) => void} C
* @param {Comment} anchor
* @param {() => C} get_component
* @param {(component: C) => void} render_fn
* @returns {void}
*/
export function component(anchor_node, component_fn, render_fn) {
/** @type {import('#client').DynamicComponentBlock} */
const block = {
// dom
d: null,
// effect
e: null,
// parent
p: /** @type {import('#client').Block} */ (current_block),
// transition
r: null,
// type
t: DYNAMIC_COMPONENT_BLOCK
};
export function component(anchor, get_component, render_fn) {
const block = {};
/** @type {null | import('#client').Render} */
let current_render = null;
hydrate_block_anchor(anchor_node);
hydrate_block_anchor(anchor);
/** @type {null | ((props: P) => void)} */
let component = null;
/** @type {C} */
let component;
block.r =
/**
* @param {import('#client').Transition} transition
* @returns {void}
*/
(transition) => {
const render = /** @type {import('#client').Render} */ (current_render);
const transitions = render.s;
transitions.add(transition);
transition.f(() => {
transitions.delete(transition);
if (transitions.size === 0) {
// If the current render has changed since, then we can remove the old render
// effect as it's stale.
if (current_render !== render && render.e !== null) {
if (render.d !== null) {
remove(render.d);
render.d = null;
}
destroy_effect(render.e);
render.e = null;
}
}
});
};
/** @type {import('#client').Effect} */
let effect;
const create_render_effect = () => {
/** @type {import('#client').Render} */
const render = {
d: null,
e: null,
s: new Set(),
p: current_render
};
/**
* Every time `component` changes, we create a new effect. Old effects are
* removed from this set when they have fully transitioned out
* @type {Set<import('#client').Effect>}
*/
let effects = new Set();
// Managed effect
render.e = render_effect(
() => {
const current = block.d;
if (current !== null) {
remove(current);
block.d = null;
}
if (component) {
render_fn(component);
}
render.d = block.d;
block.d = null;
},
block,
true
);
const component_effect = render_effect(
() => {
if (component === (component = get_component())) return;
current_render = render;
};
if (effect) {
var e = effect;
pause_effect(e, () => {
effects.delete(e);
});
}
const render = () => {
const render = current_render;
if (component) {
effect = render_effect(
() => {
render_fn(component);
if (render === null) {
create_render_effect();
return;
}
// @ts-expect-error TODO this should be unnecessary
const dom = block.d;
const transitions = render.s;
return () => {
if (dom !== null) {
remove(dom);
}
};
},
block,
true,
true
);
if (transitions.size === 0) {
if (render.d !== null) {
remove(render.d);
render.d = null;
}
if (render.e) {
execute_effect(render.e);
} else {
create_render_effect();
}
} else {
create_render_effect();
trigger_transitions(transitions, 'out');
}
};
// @ts-expect-error TODO tidy up
effect.d = block.d;
const component_effect = render_effect(
() => {
const next_component = component_fn();
if (component !== next_component) {
component = next_component;
render();
effects.add(effect);
}
},
block,
@ -129,19 +71,9 @@ export function component(anchor_node, component_fn, render_fn) {
);
component_effect.ondestroy = () => {
let render = current_render;
while (render !== null) {
const dom = render.d;
if (dom !== null) {
remove(dom);
}
const effect = render.e;
if (effect !== null) {
destroy_effect(effect);
}
render = render.p;
for (const e of effects) {
// @ts-expect-error TODO tidy up. ondestroy should be totally unnecessary
if (e.d) remove(e.d);
}
};
block.e = component_effect;
}

@ -1,11 +1,17 @@
import { namespace_svg } from '../../../../constants.js';
import { DYNAMIC_ELEMENT_BLOCK } from '../../constants.js';
import { current_hydration_fragment, hydrate_block_anchor, hydrating } from '../hydration.js';
import { empty } from '../operations.js';
import { destroy_effect, render_effect } from '../../reactivity/effects.js';
import { insert, remove } from '../reconciler.js';
import { current_block, execute_effect } from '../../runtime.js';
import {
destroy_effect,
pause_effect,
render_effect,
resume_effect
} from '../../reactivity/effects.js';
import { remove } from '../reconciler.js';
import { current_block } from '../../runtime.js';
import { is_array } from '../../utils.js';
import { set_should_intro } from '../../render.js';
import { current_each_item_block, set_current_each_item_block } from './each.js';
/**
* @param {import('#client').Block} block
@ -28,13 +34,13 @@ function swap_block_dom(block, from, to) {
}
/**
* @param {Comment} anchor_node
* @param {() => string} tag_fn
* @param {Comment} anchor
* @param {() => string} get_tag
* @param {boolean | null} is_svg `null` == not statically known
* @param {undefined | ((element: Element, anchor: Node) => void)} render_fn
* @returns {void}
*/
export function element(anchor_node, tag_fn, is_svg, render_fn) {
export function element(anchor, get_tag, is_svg, render_fn) {
/** @type {import('#client').DynamicElementBlock} */
const block = {
// dom
@ -42,98 +48,119 @@ export function element(anchor_node, tag_fn, is_svg, render_fn) {
// effect
e: null,
// parent
p: /** @type {import('#client').Block} */ (current_block),
// transition
r: null,
// type
t: DYNAMIC_ELEMENT_BLOCK
p: /** @type {import('#client').Block} */ (current_block)
};
hydrate_block_anchor(anchor_node);
let has_mounted = false;
hydrate_block_anchor(anchor);
/** @type {string} */
/** @type {string | null} */
let tag;
/** @type {string | null} */
let current_tag;
/** @type {null | Element} */
let element = null;
const element_effect = render_effect(
() => {
tag = tag_fn();
if (has_mounted) {
execute_effect(render_effect_signal);
}
has_mounted = true;
},
block,
false
);
// Managed effect
const render_effect_signal = render_effect(
() => {
// We try our best infering the namespace in case it's not possible to determine statically,
// but on the first render on the client (without hydration) the parent will be undefined,
// since the anchor is not attached to its parent / the dom yet.
const ns =
is_svg || tag === 'svg'
? namespace_svg
: is_svg === false || anchor_node.parentElement?.tagName === 'foreignObject'
? null
: anchor_node.parentElement?.namespaceURI ?? null;
const next_element = tag
? hydrating
? /** @type {Element} */ (current_hydration_fragment[0])
: ns
? document.createElementNS(ns, tag)
: document.createElement(tag)
: null;
const prev_element = element;
if (prev_element !== null) {
block.d = null;
}
/** @type {import('#client').Effect | null} */
let effect;
element = next_element;
if (element !== null && render_fn !== undefined) {
let anchor;
if (hydrating) {
// Use the existing ssr comment as the anchor so that the inner open and close
// methods can pick up the existing nodes correctly
anchor = /** @type {Comment} */ (element.firstChild);
} else {
anchor = empty();
element.appendChild(anchor);
}
render_fn(element, anchor);
}
/**
* The keyed `{#each ...}` item block, if any, that this element is inside.
* We track this so we can set it when changing the element, allowing any
* `animate:` directive to bind itself to the correct block
*/
let each_item_block = current_each_item_block;
const has_prev_element = prev_element !== null;
if (has_prev_element) {
remove(prev_element);
}
if (element !== null) {
insert(element, null, anchor_node);
if (has_prev_element) {
const parent_block = block.p;
swap_block_dom(parent_block, prev_element, element);
}
const wrapper = render_effect(() => {
const next_tag = get_tag() || null;
if (next_tag === tag) return;
// See explanation of `each_item_block` above
var previous_each_item_block = current_each_item_block;
set_current_each_item_block(each_item_block);
// We try our best infering the namespace in case it's not possible to determine statically,
// but on the first render on the client (without hydration) the parent will be undefined,
// since the anchor is not attached to its parent / the dom yet.
const ns =
is_svg || next_tag === 'svg'
? namespace_svg
: is_svg === false || anchor.parentElement?.tagName === 'foreignObject'
? null
: anchor.parentElement?.namespaceURI ?? null;
if (effect) {
if (next_tag === null) {
// start outro
pause_effect(effect, () => {
effect = null;
current_tag = null;
element?.remove(); // TODO this should be unnecessary
});
} else if (next_tag === current_tag) {
// same tag as is currently rendered — abort outro
resume_effect(effect);
} else {
// tag is changing — destroy immediately, render contents without intro transitions
destroy_effect(effect);
set_should_intro(false);
}
},
block,
true
);
}
element_effect.ondestroy = () => {
if (next_tag && next_tag !== current_tag) {
effect = render_effect(
() => {
const prev_element = element;
element = hydrating
? /** @type {Element} */ (current_hydration_fragment[0])
: ns
? document.createElementNS(ns, next_tag)
: document.createElement(next_tag);
if (render_fn) {
let anchor;
if (hydrating) {
// Use the existing ssr comment as the anchor so that the inner open and close
// methods can pick up the existing nodes correctly
anchor = /** @type {Comment} */ (element.firstChild);
} else {
anchor = empty();
element.appendChild(anchor);
}
render_fn(element, anchor);
}
anchor.before(element);
if (prev_element) {
swap_block_dom(block.p, prev_element, element);
prev_element.remove();
}
},
block,
true
);
}
tag = next_tag;
if (tag) current_tag = tag;
set_should_intro(true);
set_current_each_item_block(previous_each_item_block);
}, block);
wrapper.ondestroy = () => {
if (element !== null) {
remove(element);
block.d = null;
element = null;
}
destroy_effect(render_effect_signal);
if (effect) {
destroy_effect(effect);
}
};
block.e = element_effect;
block.e = wrapper;
}

@ -1,4 +1,3 @@
import { HEAD_BLOCK } from '../../constants.js';
import {
current_hydration_fragment,
get_hydration_fragment,
@ -22,11 +21,7 @@ export function head(render_fn) {
// effect
e: null,
// parent
p: /** @type {import('#client').Block} */ (current_block),
// transition
r: null,
// type
t: HEAD_BLOCK
p: /** @type {import('#client').Block} */ (current_block)
};
// The head function may be called after the first hydration pass and ssr comment nodes may still be present,

@ -1,5 +1,5 @@
import { DEV } from 'esm-env';
import { render_effect } from '../../../reactivity/effects.js';
import { render_effect, user_effect } from '../../../reactivity/effects.js';
import { stringify } from '../../../render.js';
/**
@ -96,6 +96,11 @@ export function bind_group(inputs, group_index, input, get_value, update) {
}
});
user_effect(() => {
// necessary to maintain binding group order in all insertion scenarios. TODO optimise
binding_group.sort((a, b) => (a.compareDocumentPosition(b) === 4 ? -1 : 1));
});
render_effect(() => {
return () => {
var index = binding_group.indexOf(input);

@ -73,33 +73,26 @@ export function get_hydration_fragment(node, insert_text = false) {
}
/**
* @param {Text | Comment | Element} anchor_node
* @param {boolean} [is_controlled]
* @param {Node} node
* @returns {void}
*/
export function hydrate_block_anchor(anchor_node, is_controlled) {
if (hydrating) {
/** @type {Node} */
let target_node = anchor_node;
export function hydrate_block_anchor(node) {
if (!hydrating) return;
if (is_controlled) {
target_node = /** @type {Node} */ (target_node.firstChild);
}
if (target_node.nodeType === 8) {
// @ts-ignore
let fragment = target_node.$$fragment;
if (fragment === undefined) {
fragment = get_hydration_fragment(target_node);
} else {
schedule_task(() => {
// @ts-expect-error clean up memory
target_node.$$fragment = undefined;
});
}
set_current_hydration_fragment(fragment);
if (node.nodeType === 8) {
// @ts-ignore
let fragment = node.$$fragment;
if (fragment === undefined) {
fragment = get_hydration_fragment(node);
} else {
const first_child = /** @type {Element | null} */ (target_node.firstChild);
set_current_hydration_fragment(first_child === null ? [] : [first_child]);
schedule_task(() => {
// @ts-expect-error clean up memory
node.$$fragment = undefined;
});
}
set_current_hydration_fragment(fragment);
} else {
const first_child = /** @type {Element | null} */ (node.firstChild);
set_current_hydration_fragment(first_child === null ? [] : [first_child]);
}
}

@ -30,30 +30,21 @@ export function create_fragment_with_script_from_html(html) {
/**
* @param {Array<import('../types.js').TemplateNode> | import('../types.js').TemplateNode} current
* @param {null | Element} parent_element
* @param {null | Text | Element | Comment} sibling
* @param {Text | Element | Comment} sibling
* @returns {Text | Element | Comment}
*/
export function insert(current, parent_element, sibling) {
export function insert(current, sibling) {
if (!current) return sibling;
if (is_array(current)) {
var i = 0;
var node;
for (; i < current.length; i++) {
node = current[i];
if (sibling === null) {
append_child(/** @type {Element} */ (parent_element), /** @type {Node} */ (node));
} else {
sibling.before(/** @type {Node} */ (node));
}
for (var i = 0; i < current.length; i++) {
sibling.before(/** @type {Node} */ (current[i]));
}
return current[0];
} else if (current !== null) {
if (sibling === null) {
append_child(/** @type {Element} */ (parent_element), /** @type {Node} */ (current));
} else {
sibling.before(/** @type {Node} */ (current));
}
}
sibling.before(/** @type {Node} */ (current));
return /** @type {Text | Element | Comment} */ (current);
}

@ -90,7 +90,7 @@ export function svg_template_with_script(svg, return_fragment) {
function open_template(is_fragment, use_clone_node, anchor, template_element_fn) {
if (hydrating) {
if (anchor !== null) {
hydrate_block_anchor(anchor, false);
hydrate_block_anchor(anchor);
}
// In ssr+hydration optimization mode, we might remove the template_element,
// so we need to is_fragment flag to properly handle hydrated content accordingly.
@ -181,18 +181,18 @@ export function comment(anchor) {
* @returns {void}
*/
function close_template(dom, is_fragment, anchor) {
const block = /** @type {import('#client').Block} */ (current_block);
/** @type {import('#client').TemplateNode | Array<import('#client').TemplateNode>} */
const current = is_fragment
var current = is_fragment
? is_array(dom)
? dom
: /** @type {import('#client').TemplateNode[]} */ (Array.from(dom.childNodes))
: dom;
if (!hydrating && anchor !== null) {
insert(current, null, anchor);
insert(current, anchor);
}
block.d = current;
/** @type {import('#client').Block} */ (current_block).d = current;
}
/**

@ -1,19 +1,22 @@
import { raf } from './timing.js';
const tasks = new Set();
// TODO move this into timing.js where it probably belongs
/**
* @param {number} now
* @returns {void}
*/
function run_tasks(now) {
tasks.forEach((task) => {
raf.tasks.forEach((task) => {
if (!task.c(now)) {
tasks.delete(task);
raf.tasks.delete(task);
task.f();
}
});
if (tasks.size !== 0) raf.tick(run_tasks);
if (raf.tasks.size !== 0) {
raf.tick(run_tasks);
}
}
/**
@ -25,13 +28,17 @@ function run_tasks(now) {
export function loop(callback) {
/** @type {import('./types.js').TaskEntry} */
let task;
if (tasks.size === 0) raf.tick(run_tasks);
if (raf.tasks.size === 0) {
raf.tick(run_tasks);
}
return {
promise: new Promise((fulfill) => {
tasks.add((task = { c: callback, f: fulfill }));
raf.tasks.add((task = { c: callback, f: fulfill }));
}),
abort() {
tasks.delete(task);
raf.tasks.delete(task);
}
};
}

@ -1,22 +1,34 @@
import { DEV } from 'esm-env';
import {
check_dirtiness,
current_block,
current_component_context,
current_effect,
current_reaction,
destroy_children,
execute_effect,
get,
remove_reactions,
schedule_effect,
set_signal_status,
untrack
} from '../runtime.js';
import { DIRTY, MANAGED, RENDER_EFFECT, EFFECT, PRE_EFFECT, DESTROYED } from '../constants.js';
import {
DIRTY,
MANAGED,
RENDER_EFFECT,
EFFECT,
PRE_EFFECT,
DESTROYED,
INERT,
IS_ELSEIF
} from '../constants.js';
import { set } from './sources.js';
import { noop } from '../../common.js';
/**
* @param {import('./types.js').EffectType} type
* @param {(() => void | (() => void)) | ((b: import('#client').Block) => void | (() => void))} fn
* @param {(() => void | (() => void))} fn
* @param {boolean} sync
* @param {null | import('#client').Block} block
* @param {boolean} init
@ -25,6 +37,7 @@ import { set } from './sources.js';
function create_effect(type, fn, sync, block = current_block, init = true) {
/** @type {import('#client').Effect} */
const signal = {
parent: current_effect,
block,
deps: null,
f: type | DIRTY,
@ -34,7 +47,8 @@ function create_effect(type, fn, sync, block = current_block, init = true) {
deriveds: null,
teardown: null,
ctx: current_component_context,
ondestroy: null
ondestroy: null,
transitions: null
};
if (current_effect !== null) {
@ -201,8 +215,7 @@ export function invalidate_effect(fn) {
}
/**
* @template {import('#client').Block} B
* @param {(block: B) => void | (() => void)} fn
* @param {(() => void)} fn
* @param {any} block
* @param {any} managed
* @param {any} sync
@ -217,22 +230,128 @@ export function render_effect(fn, block = current_block, managed = false, sync =
}
/**
* @param {import('#client').Effect} signal
* @param {import('#client').Effect} effect
* @returns {void}
*/
export function destroy_effect(signal) {
destroy_children(signal);
remove_reactions(signal, 0);
set_signal_status(signal, DESTROYED);
signal.teardown?.();
signal.ondestroy?.();
signal.fn =
signal.effects =
signal.teardown =
signal.ondestroy =
signal.ctx =
signal.block =
signal.deps =
export function destroy_effect(effect) {
destroy_children(effect);
remove_reactions(effect, 0);
set_signal_status(effect, DESTROYED);
if (effect.transitions) {
for (const transition of effect.transitions) {
transition.stop();
}
}
effect.teardown?.();
effect.ondestroy?.();
// @ts-expect-error
effect.fn =
effect.effects =
effect.teardown =
effect.ondestroy =
effect.ctx =
effect.block =
effect.deps =
null;
}
/**
* When a block effect is removed, we don't immediately destroy it or yank it
* out of the DOM, because it might have transitions. Instead, we 'pause' it.
* It stays around (in memory, and in the DOM) until outro transitions have
* completed, and if the state change is reversed then we _resume_ it.
* A paused effect does not update, and the DOM subtree becomes inert.
* @param {import('#client').Effect} effect
* @param {() => void} callback
*/
export function pause_effect(effect, callback = noop) {
/** @type {import('#client').TransitionManager[]} */
const transitions = [];
pause_children(effect, transitions, true);
let remaining = transitions.length;
if (remaining > 0) {
const check = () => {
if (!--remaining) {
destroy_effect(effect);
callback();
}
};
for (const transition of transitions) {
transition.out(check);
}
} else {
destroy_effect(effect);
callback();
}
}
/**
* @param {import('#client').Effect} effect
* @param {import('#client').TransitionManager[]} transitions
* @param {boolean} local
*/
function pause_children(effect, transitions, local) {
if ((effect.f & INERT) !== 0) return;
effect.f ^= INERT;
if (effect.transitions !== null) {
for (const transition of effect.transitions) {
if (transition.is_global || local) {
transitions.push(transition);
}
}
}
if (effect.effects !== null) {
for (const child of effect.effects) {
var transparent = (child.f & IS_ELSEIF) !== 0 || (child.f & MANAGED) !== 0;
pause_children(child, transitions, transparent ? local : false);
}
}
}
/**
* The opposite of `pause_effect`. We call this if (for example)
* `x` becomes falsy then truthy: `{#if x}...{/if}`
* @param {import('#client').Effect} effect
*/
export function resume_effect(effect) {
resume_children(effect, true);
}
/**
* @param {import('#client').Effect} effect
* @param {boolean} local
*/
function resume_children(effect, local) {
if ((effect.f & INERT) === 0) return;
effect.f ^= INERT;
// If a dependency of this effect changed while it was paused,
// apply the change now
if (check_dirtiness(effect)) {
execute_effect(effect);
}
if (effect.effects !== null) {
for (const child of effect.effects) {
var transparent = (child.f & IS_ELSEIF) !== 0 || (child.f & MANAGED) !== 0;
resume_children(child, transparent ? local : false);
}
}
if (effect.transitions !== null) {
for (const transition of effect.transitions) {
if (transition.is_global || local) {
transition.in();
}
}
}
}

@ -1,4 +1,4 @@
import type { Block, ComponentContext, Equals } from '#client';
import type { Block, ComponentContext, Equals, TransitionManager } from '#client';
import type { EFFECT, PRE_EFFECT, RENDER_EFFECT } from '../constants';
export type EffectType = typeof EFFECT | typeof PRE_EFFECT | typeof RENDER_EFFECT;
@ -21,7 +21,7 @@ export interface Value<V = unknown> extends Signal {
export interface Reaction extends Signal {
/** The reaction function */
fn: null | Function;
fn: Function;
/** Signals that this signal reads from */
deps: null | Value[];
/** Effects created inside this signal */
@ -36,6 +36,7 @@ export interface Derived<V = unknown> extends Value<V>, Reaction {
}
export interface Effect extends Reaction {
parent: Effect | null;
/** The block associated with this effect */
block: null | Block;
/** The associated component context */
@ -43,11 +44,13 @@ export interface Effect extends Reaction {
/** Stuff to do when the effect is destroyed */
ondestroy: null | (() => void);
/** The effect function */
fn: null | (() => void | (() => void)) | ((b: Block, s: Signal) => void | (() => void));
fn: () => void | (() => void);
/** The teardown function returned from the effect function */
teardown: null | (() => void);
/** The depth from the root signal, used for ordering render/pre-effects topologically **/
l: number;
/** Transition managers created with `$.transition` */
transitions: null | TransitionManager[];
}
export interface ValueDebug<V = unknown> extends Value<V> {

@ -12,7 +12,6 @@ import {
set_current_hydration_fragment
} from './dom/hydration.js';
import { array_from } from './utils.js';
import { ROOT_BLOCK } from './constants.js';
import { handle_event_propagation } from './dom/elements/events.js';
/** @type {Set<string>} */
@ -21,6 +20,18 @@ export const all_registered_events = new Set();
/** @type {Set<(events: Array<string>) => void>} */
export const root_event_handles = new Set();
/**
* This is normally true block effects should run their intro transitions
* but is false during hydration and mounting (unless `options.intro` is `true`)
* and when creating the children of a `<svelte:element>` that just changed tag
*/
export let should_intro = true;
/** @param {boolean} value */
export function set_should_intro(value) {
should_intro = value;
}
/**
* @param {Element} dom
* @param {() => string} value
@ -51,19 +62,19 @@ export function text(dom, value) {
}
/**
* @param {Comment} anchor_node
* @param {Comment} anchor
* @param {void | ((anchor: Comment, slot_props: Record<string, unknown>) => void)} slot_fn
* @param {Record<string, unknown>} slot_props
* @param {null | ((anchor: Comment) => void)} fallback_fn
*/
export function slot(anchor_node, slot_fn, slot_props, fallback_fn) {
hydrate_block_anchor(anchor_node);
export function slot(anchor, slot_fn, slot_props, fallback_fn) {
hydrate_block_anchor(anchor);
if (slot_fn === undefined) {
if (fallback_fn !== null) {
fallback_fn(anchor_node);
fallback_fn(anchor);
}
} else {
slot_fn(anchor_node, slot_props);
slot_fn(anchor, slot_props);
}
}
@ -198,6 +209,8 @@ function _mount(Component, options) {
const registered_events = new Set();
const container = options.target;
should_intro = options.intro ?? false;
/** @type {import('#client').RootBlock} */
const block = {
// dom
@ -207,11 +220,7 @@ function _mount(Component, options) {
// intro
i: options.intro || false,
// parent
p: null,
// transition
r: null,
// type
t: ROOT_BLOCK
p: null
};
/** @type {Exports} */
@ -246,6 +255,8 @@ function _mount(Component, options) {
const bound_event_listener = handle_event_propagation.bind(null, container);
const bound_document_event_listener = handle_event_propagation.bind(null, document);
should_intro = true;
/** @param {Array<string>} events */
const event_handle = (events) => {
for (let i = 0; i < events.length; i++) {

@ -21,7 +21,8 @@ import {
DESTROYED,
INERT,
MANAGED,
STATE_SYMBOL
STATE_SYMBOL,
EFFECT_RAN
} from './constants.js';
import { flush_tasks } from './dom/task.js';
import { add_owner } from './dev/ownership.js';
@ -54,9 +55,19 @@ let flush_count = 0;
/** @type {null | import('./types.js').Reaction} */
export let current_reaction = null;
/** @param {null | import('./types.js').Reaction} reaction */
export function set_current_reaction(reaction) {
current_reaction = reaction;
}
/** @type {null | import('./types.js').Effect} */
export let current_effect = null;
/** @param {null | import('./types.js').Effect} effect */
export function set_current_effect(effect) {
current_effect = effect;
}
/** @type {null | import('./types.js').Value[]} */
export let current_dependencies = null;
let current_dependencies_index = 0;
@ -104,6 +115,11 @@ export let current_block = null;
/** @type {import('./types.js').ComponentContext | null} */
export let current_component_context = null;
/** @param {import('./types.js').ComponentContext | null} context */
export function set_current_component_context(context) {
current_component_context = context;
}
/** @returns {boolean} */
export function is_runes() {
return current_component_context !== null && current_component_context.r;
@ -147,7 +163,7 @@ export function batch_inspect(target, prop, receiver) {
* @param {import('./types.js').Reaction} reaction
* @returns {boolean}
*/
function check_dirtiness(reaction) {
export function check_dirtiness(reaction) {
var flags = reaction.f;
if ((flags & DIRTY) !== 0) {
@ -200,7 +216,6 @@ function check_dirtiness(reaction) {
export function execute_reaction_fn(signal) {
const fn = signal.fn;
const flags = signal.f;
const is_render_effect = (flags & RENDER_EFFECT) !== 0;
const previous_dependencies = current_dependencies;
const previous_dependencies_index = current_dependencies_index;
@ -217,19 +232,7 @@ export function execute_reaction_fn(signal) {
current_untracking = false;
try {
let res;
if (is_render_effect) {
res = /** @type {(block: import('#client').Block, signal: import('#client').Signal) => V} */ (
fn
)(
/** @type {import('#client').Block} */ (
/** @type {import('#client').Effect} */ (signal).block
),
/** @type {import('#client').Signal} */ (signal)
);
} else {
res = /** @type {() => V} */ (fn)();
}
let res = fn();
let dependencies = /** @type {import('./types.js').Value<unknown>[]} **/ (signal.deps);
if (current_dependencies !== null) {
let i;
@ -548,6 +551,8 @@ export function schedule_effect(signal, sync) {
}
}
}
signal.f |= EFFECT_RAN;
}
/**

@ -9,5 +9,6 @@ const now = is_client ? () => performance.now() : () => Date.now();
/** @type {import('./types.js').Raf} */
export const raf = {
tick: /** @param {any} _ */ (_) => request_animation_frame(_),
now: () => now()
now: () => now(),
tasks: new Set()
};

@ -1,16 +1,4 @@
import {
ROOT_BLOCK,
EACH_BLOCK,
EACH_ITEM_BLOCK,
IF_BLOCK,
AWAIT_BLOCK,
KEY_BLOCK,
HEAD_BLOCK,
DYNAMIC_COMPONENT_BLOCK,
DYNAMIC_ELEMENT_BLOCK,
SNIPPET_BLOCK,
STATE_SYMBOL
} from './constants.js';
import { STATE_SYMBOL } from './constants.js';
import type { Effect, Source, Value } from './reactivity/types.js';
type EventCallback = (event: Event) => boolean;
@ -59,42 +47,8 @@ export type ComponentContext = {
export type Equals = (this: Value, value: unknown) => boolean;
export type BlockType =
| typeof ROOT_BLOCK
| typeof EACH_BLOCK
| typeof EACH_ITEM_BLOCK
| typeof IF_BLOCK
| typeof AWAIT_BLOCK
| typeof KEY_BLOCK
| typeof SNIPPET_BLOCK
| typeof HEAD_BLOCK
| typeof DYNAMIC_COMPONENT_BLOCK
| typeof DYNAMIC_ELEMENT_BLOCK;
export type TemplateNode = Text | Element | Comment;
export type Transition = {
/** effect */
e: Effect;
/** payload */
p: null | TransitionPayload;
/** init */
i: (from?: DOMRect) => TransitionPayload;
/** finished */
f: (fn: () => void) => void;
in: () => void;
/** out */
o: () => void;
/** cancel */
c: () => void;
/** cleanup */
x: () => void;
/** direction */
r: 'in' | 'out' | 'both' | 'key';
/** dom */
d: HTMLElement;
};
export type RootBlock = {
/** dom */
d: null | TemplateNode | Array<TemplateNode>;
@ -104,10 +58,6 @@ export type RootBlock = {
i: boolean;
/** parent */
p: null;
/** transition */
r: null | ((transition: Transition) => void);
/** type */
t: typeof ROOT_BLOCK;
};
export type IfBlock = {
@ -119,31 +69,6 @@ export type IfBlock = {
e: null | Effect;
/** parent */
p: Block;
/** transition */
r: null | ((transition: Transition) => void);
/** consequent transitions */
c: null | Set<Transition>;
/** alternate transitions */
a: null | Set<Transition>;
/** effect */
ce: null | Effect;
/** effect */
ae: null | Effect;
/** type */
t: typeof IF_BLOCK;
};
export type KeyBlock = {
/** dom */
d: null | TemplateNode | Array<TemplateNode>;
/** effect */
e: null | Effect;
/** parent */
p: Block;
/** transition */
r: null | ((transition: Transition) => void);
/** type */
t: typeof KEY_BLOCK;
};
export type HeadBlock = {
@ -153,10 +78,6 @@ export type HeadBlock = {
e: null | Effect;
/** parent */
p: Block;
/** transition */
r: null | ((transition: Transition) => void);
/** type */
t: typeof HEAD_BLOCK;
};
export type DynamicElementBlock = {
@ -166,10 +87,6 @@ export type DynamicElementBlock = {
e: null | Effect;
/** parent */
p: Block;
/** transition */
r: null | ((transition: Transition) => void);
/** type */
t: typeof DYNAMIC_ELEMENT_BLOCK;
};
export type DynamicComponentBlock = {
@ -179,10 +96,6 @@ export type DynamicComponentBlock = {
e: null | Effect;
/** parent */
p: Block;
/** transition */
r: null | ((transition: Transition) => void);
/** type */
t: typeof DYNAMIC_COMPONENT_BLOCK;
};
export type AwaitBlock = {
@ -194,15 +107,9 @@ export type AwaitBlock = {
p: Block;
/** pending */
n: boolean;
/** transition */
r: null | ((transition: Transition) => void);
/** type */
t: typeof AWAIT_BLOCK;
};
export type EachBlock = {
/** anchor */
a: Element | Comment;
/** flags */
f: number;
/** dom */
@ -213,35 +120,21 @@ export type EachBlock = {
e: null | Effect;
/** parent */
p: Block;
/** transition */
r: null | ((transition: Transition) => void);
/** transitions */
s: Array<EachItemBlock>;
/** type */
t: typeof EACH_BLOCK;
};
export type EachItemBlock = {
/** transition */
a: null | ((block: EachItemBlock, transitions: Set<Transition>) => void);
/** animation manager */
a: AnimationManager | null;
/** dom */
d: null | TemplateNode | Array<TemplateNode>;
/** effect */
e: null | Effect;
e: Effect;
/** item */
v: any | Source<any>;
/** index */
i: number | Source<number>;
/** key */
k: unknown;
/** parent */
p: EachBlock;
/** transition */
r: null | ((transition: Transition) => void);
/** transitions */
s: null | Set<Transition>;
/** type */
t: typeof EACH_ITEM_BLOCK;
};
export type SnippetBlock = {
@ -251,10 +144,6 @@ export type SnippetBlock = {
p: Block;
/** effect */
e: null | Effect;
/** transition */
r: null;
/** type */
t: typeof SNIPPET_BLOCK;
};
export type Block =
@ -264,25 +153,54 @@ export type Block =
| DynamicElementBlock
| DynamicComponentBlock
| HeadBlock
| KeyBlock
| EachBlock
| EachItemBlock
| SnippetBlock;
export interface TransitionManager {
/** Whether the `global` modifier was used (i.e. `transition:fade|global`) */
is_global: boolean;
/** Called inside `resume_effect` */
in: () => void;
/** Called inside `pause_effect` */
out: (callback?: () => void) => void;
/** Called inside `destroy_effect` */
stop: () => void;
}
export interface AnimationManager {
/** An element with an `animate:` directive */
element: Element;
/** Called during keyed each block reconciliation, before updates */
measure: () => void;
/** Called during keyed each block reconciliation, after updates — this triggers the animation */
apply: () => void;
}
export interface Animation {
/** Abort the animation */
abort: () => void;
/** Allow the animation to continue running, but remove any callback. This prevents the removal of an outroing block if the corresponding intro has a `delay` */
deactivate: () => void;
/** Resets an animation to its starting state, if it uses `tick`. Exposed as a separate method so that an aborted `out:` can still reset even if the `outro` had already completed */
reset: () => void;
/** Get the `t` value (between `0` and `1`) of the animation, so that its counterpart can start from the right place */
t: (now: number) => number;
}
export type TransitionFn<P> = (
element: Element,
props: P,
options: { direction?: 'in' | 'out' | 'both' }
) => TransitionPayload;
) => AnimationConfig | ((options: { direction?: 'in' | 'out' }) => AnimationConfig);
export type AnimateFn<P> = (
element: Element,
rects: { from: DOMRect; to: DOMRect },
props: P,
options: {}
) => TransitionPayload;
props: P
) => AnimationConfig;
export type TransitionPayload = {
export type AnimationConfig = {
delay?: number;
duration?: number;
easing?: (t: number) => number;
@ -307,15 +225,17 @@ export type Render = {
d: null | TemplateNode | Array<TemplateNode>;
/** effect */
e: null | Effect;
/** transitions */
s: Set<Transition>;
/** prev */
p: Render | null;
};
export type Raf = {
/** Alias for `requestAnimationFrame`, exposed in such a way that we can override in tests */
tick: (callback: (time: DOMHighResTimeStamp) => void) => any;
/** Alias for `performance.now()`, exposed in such a way that we can override in tests */
now: () => number;
/** A set of tasks that will run to completion, unless aborted */
tasks: Set<TaskEntry>;
};
export interface Task {

@ -14,6 +14,7 @@ export const raf = {
raf.ticks.add(f);
};
svelte_raf.now = () => raf.time;
svelte_raf.tasks.clear();
}
};
@ -31,125 +32,144 @@ function tick(time) {
}
class Animation {
#target;
#keyframes;
#duration;
#timeline_offset;
#reversed;
#target;
#paused;
#offset = raf.time;
#finished = () => {};
#cancelled = () => {};
currentTime = 0;
/**
* @param {HTMLElement} target
* @param {Keyframe[]} keyframes
* @param {{duration?: number}} options
* @param {{ duration: number }} options // TODO add delay
*/
constructor(target, keyframes, options = {}) {
constructor(target, keyframes, { duration }) {
this.#target = target;
this.#keyframes = keyframes;
this.#duration = options.duration || 0;
this.#timeline_offset = 0;
this.#reversed = false;
this.#paused = false;
this.onfinish = () => {};
this.pending = true;
this.currentTime = 0;
this.playState = 'running';
this.effect = {
setKeyframes: (/** @type {Keyframe[]} */ keyframes) => {
this.#keyframes = keyframes;
this.#duration = duration;
// Promise-like semantics, but call callbacks immediately on raf.tick
this.finished = {
/** @param {() => void} callback */
then: (callback) => {
this.#finished = callback;
return {
/** @param {() => void} callback */
catch: (callback) => {
this.#cancelled = callback;
}
};
}
};
}
play() {
this.#paused = false;
raf.animations.add(this);
this.playState = 'running';
this._update();
}
_update() {
if (this.#reversed) {
if (this.#timeline_offset === 0) {
this.currentTime = this.#duration - raf.time;
} else {
this.currentTime = this.#timeline_offset + (this.#timeline_offset - raf.time);
}
} else {
this.currentTime = raf.time - this.#timeline_offset;
}
this.currentTime = raf.time - this.#offset;
const target_frame = this.currentTime / this.#duration;
this._applyKeyFrame(target_frame);
this.#apply_keyframe(target_frame);
if (this.currentTime >= this.#duration) {
this.#finished();
raf.animations.delete(this);
}
}
/**
* @param {number} target_frame
* @param {number} t
*/
_applyKeyFrame(target_frame) {
const keyframes = this.#keyframes;
const keyframes_size = keyframes.length - 1;
const frame = keyframes[Math.min(keyframes_size, Math.floor(keyframes.length * target_frame))];
#apply_keyframe(t) {
const n = Math.min(1, Math.max(0, t)) * (this.#keyframes.length - 1);
const lower = this.#keyframes[Math.floor(n)];
const upper = this.#keyframes[Math.ceil(n)];
let frame = lower;
if (lower !== upper) {
frame = {};
for (const key in lower) {
frame[key] = interpolate(
/** @type {string} */ (lower[key]),
/** @type {string} */ (upper[key]),
n % 1
);
}
}
for (let prop in frame) {
// @ts-ignore
this.#target.style[prop] = frame[prop];
}
if (this.#reversed) {
if (this.currentTime <= 0) {
this.finish();
for (let prop in frame) {
// @ts-ignore
this.#target.style[prop] = null;
}
}
} else {
if (this.currentTime >= this.#duration) {
this.finish();
for (let prop in frame) {
// @ts-ignore
this.#target.style[prop] = null;
}
}
}
}
finish() {
this.onfinish();
this.currentTime = this.#reversed ? 0 : this.#duration;
if (this.#reversed) {
raf.animations.delete(this);
if (this.currentTime >= this.#duration) {
this.currentTime = this.#duration;
for (let prop in frame) {
// @ts-ignore
this.#target.style[prop] = null;
}
}
this.playState = 'idle';
}
cancel() {
this.#paused = true;
if (this.currentTime > 0 && this.currentTime < this.#duration) {
this._applyKeyFrame(this.#reversed ? this.#keyframes.length - 1 : 0);
this.#apply_keyframe(0);
}
}
pause() {
this.#paused = true;
this.playState = 'paused';
this.#cancelled();
raf.animations.delete(this);
}
}
/**
* @param {string} a
* @param {string} b
* @param {number} p
*/
function interpolate(a, b, p) {
if (a === b) return a;
const fallback = p < 0.5 ? a : b;
const a_match = a.match(/[\d.]+|[^\d.]+/g);
const b_match = b.match(/[\d.]+|[^\d.]+/g);
if (!a_match || !b_match) return fallback;
if (a_match.length !== b_match.length) return fallback;
reverse() {
if (this.#paused && !raf.animations.has(this)) {
raf.animations.add(this);
let result = '';
for (let i = 0; i < a_match.length; i += 2) {
const a_num = parseFloat(a_match[i]);
const b_num = parseFloat(b_match[i]);
result += a_num + (b_num - a_num) * p;
if (a_match[i + 1] !== b_match[i + 1]) {
// bail
return fallback;
}
this.#timeline_offset = this.currentTime;
this.#reversed = !this.#reversed;
this.playState = 'running';
result += a_match[i + 1] ?? '';
}
return result;
}
/**
* @param {Keyframe[]} keyframes
* @param {{duration?: number}} options
* @param {{duration: number}} options
* @returns {globalThis.Animation}
*/
HTMLElement.prototype.animate = function (keyframes, options) {
const animation = new Animation(this, keyframes, options);
raf.animations.add(animation);
// @ts-ignore
return animation;
};

@ -1,11 +1,13 @@
<script>
import { onMount } from 'svelte';
export let logs;
export let state;
export let logs;
export let state;
onMount(() => {
logs.push(`mount ${state}`);
logs.push(`mount ${state}`);
return () => {
logs.push(`unmount ${state}`);
logs.push(`unmount ${state}`);
};
});
</script>

@ -6,7 +6,7 @@
let resolve;
let value = 0;
export let logs = [];
async function new_promise() {
promise = new Promise(r => {
resolve = r;
@ -20,7 +20,7 @@
export async function test() {
resolve_promise();
await Promise.resolve();
await tick();
new_promise();
resolve_promise();
return tick();
@ -33,4 +33,4 @@
Loading...
{:then state}
<Component {state} {logs} />
{/await}
{/await}

@ -18,7 +18,7 @@ export default test({
raf.tick(150);
assert.htmlEqual(
target.innerHTML,
'<p>foo</p><p class="red svelte-1yszte8 border" style="overflow: hidden; opacity: 0; border-top-width: 0.5000600024000317px; border-bottom-width: 0.5000600024000317px;">bar</p>'
'<p>foo</p><p class="red svelte-1yszte8 border" style="overflow: hidden; opacity: 0; border-top-width: 0.5px; border-bottom-width: 0.5px;">bar</p>'
);
component.open = true;
raf.tick(250);

@ -23,7 +23,6 @@ export default test({
`,
test({ assert, component, target, raf }) {
raf.tick(0);
component.tag = 'p';
assert.equal(target.querySelectorAll('p').length, 5);
@ -52,11 +51,11 @@ export default test({
];
divs = target.querySelectorAll('div');
assert.ok(~divs[0].style.transform);
assert.equal(divs[1].style.transform, 'translate(1px, 0px)');
assert.equal(divs[2].style.transform, 'translate(1px, 0px)');
assert.equal(divs[3].style.transform, 'translate(1px, 0px)');
assert.ok(~divs[4].style.transform);
assert.equal(divs[0].style.transform, 'translate(0px, 120px)');
assert.equal(divs[1].style.transform, '');
assert.equal(divs[2].style.transform, '');
assert.equal(divs[3].style.transform, '');
assert.equal(divs[4].style.transform, 'translate(0px, -120px)');
raf.tick(100);
assert.deepEqual([divs[0].style.transform, divs[4].style.transform], ['', '']);

@ -8,11 +8,11 @@
return {
duration: 100,
css: (t, u) => `transform: translate(${u + dx}px, ${u * dy}px)`
css: (t, u) => `transform: translate(${u * dx}px, ${u * dy}px)`
};
}
</script>
{#each things as thing (thing.id)}
<svelte:element this={tag} animate:flip>{thing.name}</svelte:element>
{/each}
{/each}

@ -15,8 +15,9 @@ export default test({
assert.equal(h1.style.opacity, '');
assert.equal(h2.style.opacity, '');
raf.tick(50);
raf.tick(200);
component.visible = false;
assert.equal(h2.style.opacity, '0.49998000000000004');
raf.tick(250);
assert.equal(h2.style.opacity, '0.5');
}
});

@ -1,4 +1,4 @@
import { ok, test } from '../../test';
import { test } from '../../test';
export default test({
get props() {
@ -8,14 +8,11 @@ export default test({
test({ assert, component, target, raf }) {
component.visible = false;
raf.tick(150);
const outer = /** @type {HTMLSpanElement} */ (target.querySelector('.outer'));
const inner = /** @type {HTMLSpanElement} */ (target.querySelector('.inner'));
raf.tick(150);
assert.deepEqual(
[outer.style.cssText, inner.style.cssText],
['opacity: 0.24999000000000002;', '']
);
assert.deepEqual([outer.style.cssText, inner.style.cssText], ['opacity: 0.25;', '']);
}
});

@ -6,14 +6,12 @@ export default test({
const div = target.querySelector('div');
ok(div);
raf.tick(25);
assert.equal(div.style.opacity, '0.16666');
raf.tick(50);
assert.equal(div.style.opacity, '0.5');
component.visible = false;
raf.tick(40);
assert.ok(div.style.opacity === '0');
raf.tick(75);
assert.equal(div.style.opacity, '0.25');
}
});

@ -13,4 +13,4 @@
{#if visible}
<div transition:foo></div>
{/if}
{/if}

@ -5,6 +5,7 @@ export default test({
async test({ assert, component, target, raf }) {
const frame = /** @type {HTMLIFrameElement} */ (target.querySelector('iframe'));
await tick();
await tick(); // TODO investigate why this second tick is necessary. without it, `Foo.svelte` initializes with `visible = true`, incorrectly
component.visible = true;
const div = frame.contentDocument?.querySelector('div');
@ -14,8 +15,10 @@ export default test({
component.visible = false;
raf.tick(26);
// The exact number doesn't matter here, this test is about ensuring that transitions work in iframes
assert.equal(Number(div.style.opacity).toFixed(4), '0.8333');
raf.tick(25);
assert.equal(div.style.opacity, '0.25');
raf.tick(35);
assert.equal(div.style.opacity, '0.18333333333333335');
}
});

@ -6,18 +6,27 @@ export default test({
const div = target.querySelector('div');
ok(div);
assert.equal(div.style.opacity, '0');
assert.equal(div.style.scale, '0');
raf.tick(50);
component.visible = false;
// both in and out styles
assert.equal(div.style.opacity, '0.49998000000000004');
assert.equal(div.style.scale, '0.5');
assert.equal(div.style.opacity, '1');
assert.equal(div.style.rotate, '360deg');
raf.tick(75);
assert.equal(div.style.scale, '0.75'); // intro continues while outro plays
assert.equal(div.style.opacity, '0.75');
assert.equal(div.style.rotate, '270deg');
component.visible = true;
// reset original styles
assert.equal(div.style.scale, '0');
assert.equal(div.style.opacity, '1');
assert.equal(div.style.rotate, '360deg');
}
});

@ -5,7 +5,7 @@
return {
duration: 100,
css: t => {
return `opacity: ${t}`;
return `scale: ${t}`;
}
};
}
@ -14,7 +14,7 @@
return {
duration: 100,
css: t => {
return `opacity: ${t}`;
return `rotate: ${t * 360}deg; opacity: ${t}`;
}
};
}
@ -22,4 +22,4 @@
{#if visible}
<div in:foo out:bar></div>
{/if}
{/if}

@ -6,29 +6,26 @@ export default test({
const b = target.querySelector('button.b');
ok(a);
ok(b);
// jsdom doesn't set the inert attribute, and the transition checks if it exists, so set it manually to trigger the inert logic
a.inert = false;
b.inert = false;
// check and abort halfway through the outro transition
component.visible = false;
raf.tick(50);
assert.strictEqual(target.querySelector('button.a')?.inert, true);
assert.strictEqual(target.querySelector('button.b')?.inert, true);
assert.ok(target.querySelector('button.a')?.inert);
assert.ok(target.querySelector('button.b')?.inert);
component.visible = true;
assert.strictEqual(target.querySelector('button.a')?.inert, false);
assert.strictEqual(target.querySelector('button.b')?.inert, false);
assert.ok(!target.querySelector('button.a')?.inert);
assert.ok(!target.querySelector('button.b')?.inert);
// let it transition out completely and then back in
component.visible = false;
raf.tick(101);
component.visible = true;
raf.tick(150);
assert.strictEqual(target.querySelector('button.a')?.inert, false);
assert.strictEqual(target.querySelector('button.b')?.inert, false);
assert.ok(!target.querySelector('button.a')?.inert);
assert.ok(!target.querySelector('button.b')?.inert);
raf.tick(151);
assert.strictEqual(target.querySelector('button.a')?.inert, false);
assert.strictEqual(target.querySelector('button.b')?.inert, false);
assert.ok(!target.querySelector('button.a')?.inert);
assert.ok(!target.querySelector('button.b')?.inert);
}
});

@ -23,8 +23,6 @@ export default test({
assert.equal(spans[1].foo, 0.25);
assert.equal(spans[2].foo, 0.75);
raf.tick(7);
component.things = things;
raf.tick(225);
@ -42,8 +40,8 @@ export default test({
target.querySelectorAll('span')
);
assert.equal(spans[0].foo, undefined);
assert.equal(spans[1].foo, undefined);
assert.equal(spans[2].foo, undefined);
assert.equal(spans[0].foo, 1);
assert.equal(spans[1].foo, 1);
assert.equal(spans[2].foo, 1);
}
});

@ -14,8 +14,6 @@ export default test({
intro: true,
async test({ assert, target, component, raf }) {
raf.tick(0);
assert.htmlEqual(target.innerHTML, '<p class="pending" foo="0.0">loading...</p>');
let time = 0;
@ -24,10 +22,6 @@ export default test({
assert.htmlEqual(target.innerHTML, '<p class="pending" foo="0.5">loading...</p>');
await fulfil(42);
await Promise.resolve();
await Promise.resolve();
raf.tick(time);
assert.htmlEqual(
target.innerHTML,
@ -61,9 +55,6 @@ export default test({
fulfil = f;
});
await Promise.resolve();
await Promise.resolve();
raf.tick(time);
assert.htmlEqual(
target.innerHTML,
@ -83,11 +74,6 @@ export default test({
);
await fulfil(43);
await Promise.resolve();
await Promise.resolve();
raf.tick(time);
assert.htmlEqual(
target.innerHTML,
`
@ -109,9 +95,6 @@ export default test({
fulfil = f;
});
await Promise.resolve();
await Promise.resolve();
raf.tick(time);
assert.htmlEqual(
target.innerHTML,
@ -132,11 +115,6 @@ export default test({
);
await fulfil(44);
await Promise.resolve();
await Promise.resolve();
raf.tick(time);
assert.htmlEqual(
target.innerHTML,
`
@ -159,10 +137,6 @@ export default test({
fulfil = f;
});
await Promise.resolve();
await Promise.resolve();
raf.tick(time);
assert.htmlEqual(
target.innerHTML,
`
@ -172,7 +146,6 @@ export default test({
);
raf.tick((time += 40));
assert.htmlEqual(
target.innerHTML,
`
@ -182,10 +155,6 @@ export default test({
);
await fulfil(45);
await Promise.resolve();
await Promise.resolve();
raf.tick(time);
assert.htmlEqual(
target.innerHTML,
@ -197,7 +166,6 @@ export default test({
);
raf.tick((time += 20));
assert.htmlEqual(
target.innerHTML,
`
@ -211,9 +179,6 @@ export default test({
fulfil = f;
});
await Promise.resolve();
await Promise.resolve();
raf.tick(time);
assert.htmlEqual(
target.innerHTML,
@ -238,10 +203,6 @@ export default test({
);
await fulfil(46);
await Promise.resolve();
await Promise.resolve();
raf.tick(time);
assert.htmlEqual(
target.innerHTML,
@ -255,7 +216,6 @@ export default test({
);
raf.tick((time += 10));
assert.htmlEqual(
target.innerHTML,
`
@ -265,7 +225,6 @@ export default test({
);
raf.tick((time += 20));
assert.htmlEqual(
target.innerHTML,
`
@ -274,7 +233,6 @@ export default test({
);
raf.tick((time += 70));
assert.htmlEqual(
target.innerHTML,
`

@ -1,4 +1,4 @@
import { ok, test } from '../../test';
import { test } from '../../test';
/** @type {(value: any) => void} */
let fulfil;
@ -36,8 +36,8 @@ export default test({
);
assert.equal(ps[0].className, 'pending');
assert.equal(ps[1].className, 'then');
assert.equal(ps[0].foo, 0.8);
assert.equal(ps[1].foo, undefined);
assert.equal(ps[0].foo, 0.2);
assert.equal(ps[1].foo, 0.3);
raf.tick(100);
});
}

@ -35,9 +35,9 @@ export default test({
component.visible = true;
raf.tick(100);
assert.equal(divs[0].foo, 1);
assert.equal(divs[1].foo, 1);
assert.equal(divs[2].foo, 1);
assert.equal(divs[0].foo, 0.3);
assert.equal(divs[1].foo, 0.3);
assert.equal(divs[2].foo, 0.3);
assert.equal(divs[0].bar, 1);
assert.equal(divs[1].bar, 1);

@ -15,7 +15,7 @@ export default test({
raf.tick(500);
component.x = true;
assert.equal(component.no, null);
assert.equal(component.yes.foo, undefined);
assert.equal(component.yes.foo, 0);
raf.tick(700);
assert.equal(component.yes.foo, 0.5);

@ -11,7 +11,7 @@ export default test({
btn?.click();
});
assert.htmlEqual(target.innerHTML, `<h1>Outside</h1><button>Hide</button>`);
assert.htmlEqual(target.innerHTML, `<h1>Outside</h1><button style="opacity: 0;">Hide</button>`);
raf.tick(100);

@ -11,7 +11,7 @@ export default test({
btn?.click();
});
assert.htmlEqual(target.innerHTML, `<h1>Outside</h1><button>Hide</button>`);
assert.htmlEqual(target.innerHTML, `<h1>Outside</h1><button style="opacity: 0;">Hide</button>`);
raf.tick(100);

Loading…
Cancel
Save