fix lifecycle order, add CSS if necessary

pull/1864/head
Rich Harris 7 years ago
parent e03f8d6590
commit 4d12065915

@ -119,10 +119,22 @@ export default function dom(
); );
} }
// TODO injecting CSS this way is kinda dirty. Maybe it should be an
// explicit opt-in, or something?
const should_add_css = (
!component.options.customElement &&
component.stylesheet.hasStyles &&
options.css !== false
);
const body = [ const body = [
deindent` deindent`
$$init($$make_dirty) { $$init($$make_dirty) {
${component.init_uses_self && `const $$self = this;`} ${component.init_uses_self && `const $$self = this;`}
${should_add_css &&
`if (!document.getElementById("${component.stylesheet.id}-style")) @add_css();`}
${component.javascript || component.exports.map(x => `let ${x.name};`)} ${component.javascript || component.exports.map(x => `let ${x.name};`)}
${component.event_handlers.map(handler => handler.body)} ${component.event_handlers.map(handler => handler.body)}

@ -96,9 +96,14 @@ export class $$Component {
this.$$fragment[this.$$fragment.i ? 'i' : 'm'](target, anchor); this.$$fragment[this.$$fragment.i ? 'i' : 'm'](target, anchor);
this.$$.inject_refs(this.$$refs); this.$$.inject_refs(this.$$refs);
const onDestroy = this.$$onMount.map(run).filter(is_function); // onMount happens after the initial afterRender. Because
this.$$onDestroy.push(...onDestroy); // afterRender callbacks happen in reverse order (inner first)
this.$$onMount = []; // we schedule onMount callbacks before afterRender callbacks
after_render(() => {
const onDestroy = this.$$onMount.map(run).filter(is_function);
this.$$onDestroy.push(...onDestroy);
this.$$onMount = [];
});
this.$$afterRender.forEach(after_render); this.$$afterRender.forEach(after_render);
} }

@ -1,8 +1,8 @@
export default { export default {
test ( assert, component, target, window ) { test(assert, component, target, window) {
const [ control, test ] = target.querySelectorAll( 'p' ); const [control, test] = target.querySelectorAll('p');
assert.equal( window.getComputedStyle( control ).color, '' ); assert.equal(window.getComputedStyle(control).color, '');
assert.equal( window.getComputedStyle( test ).color, 'rgb(255, 0, 0)' ); assert.equal(window.getComputedStyle(test).color, 'rgb(255, 0, 0)');
} }
}; };

@ -1,7 +1,7 @@
export default { export default {
test(assert, component) { test(assert, component) {
assert.deepEqual(component.events, ['create']); assert.deepEqual(component.events, ['mount']);
component.$destroy(); component.$destroy();
assert.deepEqual(component.events, ['create', 'destroy']); assert.deepEqual(component.events, ['mount', 'destroy']);
} }
}; };

@ -4,7 +4,7 @@
export let events; export let events;
onMount(() => { onMount(() => {
events = ['create']; events = ['mount'];
}); });
onDestroy(() => { onDestroy(() => {

Loading…
Cancel
Save