Skip to content

chore: remove blocks #10880

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

Merged
merged 21 commits into from
Mar 22, 2024
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
47 changes: 16 additions & 31 deletions packages/svelte/src/internal/client/dom/blocks/await.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import {
set_current_reaction
} from '../../runtime.js';
import { destroy_effect, pause_effect, render_effect } from '../../reactivity/effects.js';
import { DESTROYED, INERT } from '../../constants.js';
import { create_block } from './utils.js';
import { INERT } from '../../constants.js';

/**
* @template V
Expand All @@ -22,8 +21,6 @@ import { create_block } from './utils.js';
* @returns {void}
*/
export function await_block(anchor, get_input, pending_fn, then_fn, catch_fn) {
const block = create_block();

const component_context = current_component_context;

hydrate_block_anchor(anchor);
Expand All @@ -48,7 +45,7 @@ export function await_block(anchor, get_input, pending_fn, then_fn, catch_fn) {
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);
var effect = render_effect(() => fn(anchor, value), true);
set_current_component_context(null);
set_current_reaction(null);
set_current_effect(null);
Expand All @@ -60,18 +57,6 @@ export function await_block(anchor, get_input, pending_fn, then_fn, catch_fn) {
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;

Expand All @@ -80,53 +65,53 @@ export function await_block(anchor, get_input, pending_fn, then_fn, catch_fn) {

if (pending_fn) {
if (pending_effect && (pending_effect.f & INERT) === 0) {
if (pending_effect.block?.d) remove(pending_effect.block.d);
if (pending_effect.dom) remove(pending_effect.dom);
destroy_effect(pending_effect);
}

pending_effect = render_effect(() => pending_fn(anchor), {}, true);
pending_effect = render_effect(() => pending_fn(anchor), true);
}

if (then_effect) pause(then_effect);
if (catch_effect) pause(catch_effect);
if (then_effect) pause_effect(then_effect);
if (catch_effect) pause_effect(catch_effect);

promise.then(
(value) => {
if (promise !== input) return;
if (pending_effect) pause(pending_effect);
if (pending_effect) pause_effect(pending_effect);

if (then_fn) {
then_effect = create_effect(then_fn, value);
}
},
(error) => {
if (promise !== input) return;
if (pending_effect) pause(pending_effect);
if (pending_effect) pause_effect(pending_effect);

if (catch_fn) {
catch_effect = create_effect(catch_fn, error);
}
}
);
} else {
if (pending_effect) pause(pending_effect);
if (catch_effect) pause(catch_effect);
if (pending_effect) pause_effect(pending_effect);
if (catch_effect) pause_effect(catch_effect);

if (then_fn) {
if (then_effect) {
if (then_effect.block?.d) remove(then_effect.block.d);
if (then_effect.dom) remove(then_effect.dom);
destroy_effect(then_effect);
}

then_effect = render_effect(() => then_fn(anchor, input), {}, true);
then_effect = render_effect(() => then_fn(anchor, input), true);
}
}
}, 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);
if (pending_effect?.dom) remove(pending_effect.dom);
if (then_effect?.dom) remove(then_effect.dom);
if (catch_effect?.dom) remove(catch_effect.dom);
};
}
Loading