for custom elements run onMount in connectedCallback

pull/4527/head
hontas 6 years ago
parent 0245239eaa
commit da0ff6c302

@ -56,6 +56,9 @@ export function mount_component(component, target, anchor) {
const { fragment, on_mount, on_destroy, after_update } = component.$$; const { fragment, on_mount, on_destroy, after_update } = component.$$;
fragment && fragment.m(target, anchor); fragment && fragment.m(target, anchor);
// custom element: call onMount in connectedCallback instead
if (component.shadowRoot) return;
// onMount happens before the initial afterUpdate // onMount happens before the initial afterUpdate
add_render_callback(() => { add_render_callback(() => {
@ -176,6 +179,9 @@ if (typeof HTMLElement === 'function') {
// @ts-ignore todo: improve typings // @ts-ignore todo: improve typings
this.appendChild(this.$$.slotted[key]); this.appendChild(this.$$.slotted[key]);
} }
const new_on_destroy = this.$$.on_mount.map(run).filter(is_function);
this.$$.on_destroy.push(...new_on_destroy);
} }
attributeChangedCallback(attr, _oldValue, newValue) { attributeChangedCallback(attr, _oldValue, newValue) {

@ -4,8 +4,11 @@
import { onMount } from 'svelte'; import { onMount } from 'svelte';
export let wasCreated; export let wasCreated;
export let propsSetBeforeMount;
export let attrs;
onMount(() => { onMount(() => {
wasCreated = true; wasCreated = true;
propsSetBeforeMount = attrs === 'should be set';
}); });
</script> </script>

@ -2,7 +2,8 @@ import * as assert from 'assert';
import './main.svelte'; import './main.svelte';
export default function (target) { export default function (target) {
target.innerHTML = '<my-app/>'; target.innerHTML = '<my-app attrs="should be set" />';
const el = target.querySelector('my-app'); const el = target.querySelector('my-app');
assert.ok(el.wasCreated); assert.ok(el.wasCreated);
assert.ok(el.propsSetBeforeMount);
} }
Loading…
Cancel
Save