move snippet code, remove blocks.js

pull/10767/head
Rich Harris 2 years ago
parent 76b156809b
commit 0119c1f04c

@ -1895,7 +1895,7 @@ export const template_visitors = {
if (is_reactive) { if (is_reactive) {
context.state.after_update.push( context.state.after_update.push(
b.stmt(b.call('$.snippet_effect', b.thunk(snippet_function), ...args)) b.stmt(b.call('$.snippet', b.thunk(snippet_function), ...args))
); );
} else { } else {
context.state.after_update.push( context.state.after_update.push(

@ -1,45 +0,0 @@
import {
ROOT_BLOCK,
HEAD_BLOCK,
DYNAMIC_ELEMENT_BLOCK,
DYNAMIC_COMPONENT_BLOCK,
SNIPPET_BLOCK
} from './constants.js';
import { current_block } from './runtime.js';
/**
* @param {boolean} intro
* @returns {import('./types.js').RootBlock}
*/
export function create_root_block(intro) {
return {
// dom
d: null,
// effect
e: null,
// intro
i: intro,
// parent
p: null,
// transition
r: null,
// type
t: ROOT_BLOCK
};
}
/** @returns {import('./types.js').SnippetBlock} */
export function create_snippet_block() {
return {
// dom
d: null,
// parent
p: /** @type {import('./types.js').Block} */ (current_block),
// effect
e: null,
// transition
r: null,
// type
t: SNIPPET_BLOCK
};
}

@ -0,0 +1,40 @@
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';
/**
* @param {() => Function | null | undefined} get_snippet
* @param {Node} node
* @param {(() => any)[]} args
* @returns {void}
*/
export function snippet(get_snippet, node, ...args) {
/** @type {import('#client').SnippetBlock} */
const block = {
// dom
d: null,
// parent
p: /** @type {import('#client').Block} */ (current_block),
// effect
e: null,
// transition
r: null,
// type
t: SNIPPET_BLOCK
};
render_effect(() => {
// Only rerender when the snippet function itself changes,
// not when an eagerly-read prop inside the snippet function changes
const snippet = get_snippet();
if (snippet) {
untrack(() => snippet(node, ...args));
}
return () => {
if (block.d !== null) {
remove(block.d);
}
};
}, block);
}

@ -10,7 +10,6 @@ import {
map_set, map_set,
set_class_name set_class_name
} from './operations.js'; } from './operations.js';
import { create_root_block, create_snippet_block } from './block.js';
import { import {
PassiveDelegatedEvents, PassiveDelegatedEvents,
DelegatedEvents, DelegatedEvents,
@ -69,7 +68,7 @@ import { run } from '../common.js';
import { bind_transition } from './transitions.js'; import { bind_transition } from './transitions.js';
import { mutable_source, source, set } from './reactivity/sources.js'; import { mutable_source, source, set } from './reactivity/sources.js';
import { safe_equals, safe_not_equal } from './reactivity/equality.js'; import { safe_equals, safe_not_equal } from './reactivity/equality.js';
import { STATE_SYMBOL } from './constants.js'; import { ROOT_BLOCK, STATE_SYMBOL } from './constants.js';
/** @type {Set<string>} */ /** @type {Set<string>} */
const all_registered_events = new Set(); const all_registered_events = new Set();
@ -2295,7 +2294,22 @@ export function hydrate(component, options) {
function _mount(Component, options) { function _mount(Component, options) {
const registered_events = new Set(); const registered_events = new Set();
const container = options.target; const container = options.target;
const block = create_root_block(options.intro || false);
/** @type {import('#client').RootBlock} */
const block = {
// dom
d: null,
// effect
e: null,
// intro
i: options.intro || false,
// parent
p: null,
// transition
r: null,
// type
t: ROOT_BLOCK
};
/** @type {Exports} */ /** @type {Exports} */
// @ts-expect-error will be defined because the render effect runs synchronously // @ts-expect-error will be defined because the render effect runs synchronously
@ -2408,29 +2422,6 @@ export function sanitize_slots(props) {
return sanitized; return sanitized;
} }
/**
* @param {() => Function | null | undefined} get_snippet
* @param {Node} node
* @param {(() => any)[]} args
* @returns {void}
*/
export function snippet_effect(get_snippet, node, ...args) {
const block = create_snippet_block();
render_effect(() => {
// Only rerender when the snippet function itself changes,
// not when an eagerly-read prop inside the snippet function changes
const snippet = get_snippet();
if (snippet) {
untrack(() => snippet(node, ...args));
}
return () => {
if (block.d !== null) {
remove(block.d);
}
};
}, block);
}
/** /**
* @param {Node} target * @param {Node} target
* @param {string} style_sheet_id * @param {string} style_sheet_id

@ -25,6 +25,7 @@ export { await_block as await } from './client/dom/blocks/await.js';
export { if_block as if } from './client/dom/blocks/if.js'; export { if_block as if } from './client/dom/blocks/if.js';
export { key_block as key } from './client/dom/blocks/key.js'; export { key_block as key } from './client/dom/blocks/key.js';
export * from './client/dom/blocks/each.js'; export * from './client/dom/blocks/each.js';
export * from './client/dom/blocks/snippet.js';
export * from './client/dom/blocks/svelte-component.js'; export * from './client/dom/blocks/svelte-component.js';
export * from './client/dom/blocks/svelte-element.js'; export * from './client/dom/blocks/svelte-element.js';
export * from './client/dom/blocks/svelte-head.js'; export * from './client/dom/blocks/svelte-head.js';

Loading…
Cancel
Save