Skip to content

Commit

Permalink
use a simpler insert and append function when not compile with hydrat…
Browse files Browse the repository at this point in the history
…able
  • Loading branch information
tanhauhau committed Jul 13, 2021
1 parent fdd3d4b commit 73f80f2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ export default class Component {
} else {
let name = node.name.slice(1);

if (compile_options.hydratable) {
if (internal_exports.has(`${name}_hydration`)) {
name += '_hydration';
}
}

if (compile_options.dev) {
if (internal_exports.has(`${name}_dev`)) {
name += '_dev';
Expand Down
12 changes: 11 additions & 1 deletion src/runtime/internal/dev.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { custom_event, append, insert, detach, listen, attr } from './dom';
import { custom_event, append, append_hydration, insert, insert_hydration, detach, listen, attr } from './dom';
import { SvelteComponent } from './Component';

export function dispatch_dev<T=any>(type: string, detail?: T) {
Expand All @@ -10,11 +10,21 @@ export function append_dev(target: Node, node: Node) {
append(target, node);
}

export function append_hydration_dev(target: Node, node: Node) {
dispatch_dev('SvelteDOMInsert', { target, node });
append_hydration(target, node);
}

export function insert_dev(target: Node, node: Node, anchor?: Node) {
dispatch_dev('SvelteDOMInsert', { target, node, anchor });
insert(target, node, anchor);
}

export function insert_hydration_dev(target: Node, node: Node, anchor?: Node) {
dispatch_dev('SvelteDOMInsert', { target, node, anchor });
insert_hydration(target, node, anchor);
}

export function detach_dev(node: Node) {
dispatch_dev('SvelteDOMRemove', { node });
detach(node);
Expand Down
14 changes: 11 additions & 3 deletions src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ function init_hydrate(target: NodeEx) {
}
}

export function append(target: NodeEx, node: NodeEx) {
export function append(target: Node, node: Node) {
target.appendChild(node);
}

export function append_hydration(target: NodeEx, node: NodeEx) {
if (is_hydrating) {
init_hydrate(target);

Expand All @@ -129,9 +133,13 @@ export function append(target: NodeEx, node: NodeEx) {
}
}

export function insert(target: NodeEx, node: NodeEx, anchor?: NodeEx) {
export function insert(target: Node, node: Node, anchor?: Node) {
target.insertBefore(node, anchor || null);
}

export function insert_hydration(target: NodeEx, node: NodeEx, anchor?: NodeEx) {
if (is_hydrating && !anchor) {
append(target, node);
append_hydration(target, node);
} else if (node.parentNode !== target || node.nextSibling != anchor) {
target.insertBefore(node, anchor || null);
}
Expand Down

0 comments on commit 73f80f2

Please sign in to comment.