Skip to content
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

Add a component.outro method #1449

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 5 additions & 3 deletions src/compile/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export default function dom(
? `@proto`
: deindent`
{
${['destroy', 'get', 'fire', 'on', 'set', '_set', '_mount', '_differs']
${['destroy', 'get', 'fire', 'on', 'outro', 'set', '_set', '_mount', '_differs']
.map(n => `${n}: @${n}`)
.join(',\n')}
}`;
Expand Down Expand Up @@ -227,7 +227,7 @@ export default function dom(
this._fragment.c();
this._fragment.${block.hasIntroMethod ? 'i' : 'm'}(this.shadowRoot, null);

if (options.target) this._mount(options.target, options.anchor);
if (options.target) this._mount(options.target, options.anchor, ${options.skipIntroByDefault ? `options.intro` : 'true'});
` : deindent`
if (options.target) {
${compiler.options.hydratable
Expand All @@ -240,7 +240,7 @@ export default function dom(
${options.dev && `if (options.hydrate) throw new Error("options.hydrate only works if the component was compiled with the \`hydratable: true\` option");`}
this._fragment.c();
`}
this._mount(options.target, options.anchor);
this._mount(options.target, options.anchor, ${options.skipIntroByDefault ? `options.intro` : 'true'});

${(compiler.hasComponents || target.hasComplexBindings || hasInitHooks || target.hasIntroTransitions) && deindent`
${compiler.hasComponents && `this._lock = true;`}
Expand Down Expand Up @@ -338,6 +338,8 @@ export default function dom(
}
` : (!sharedPath && `${name}.prototype._recompute = @noop;`)}

${!block.builders.outro.isEmpty() && `${name}.prototype.outro = @outroWithTransitions;`}

${templateProperties.setup && `%setup(${name});`}

${templateProperties.preload && `${name}.preload = %preload;`}
Expand Down
8 changes: 4 additions & 4 deletions src/compile/nodes/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ export default class Component extends Node {

block.builders.mount.addBlock(deindent`
if (${name}) {
${name}._mount(${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'});
${name}._mount(${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'}, ${compiler.options.skipIntroByDefault ? 'false' : 'true'});
${this.ref && `#component.refs.${this.ref} = ${name};`}
}
`);
Expand All @@ -405,7 +405,7 @@ export default class Component extends Node {
${name}._fragment.c();

${this.children.map(child => child.remount(name))}
${name}._mount(${updateMountNode}, ${anchor});
${name}._mount(${updateMountNode}, ${anchor}, ${compiler.options.skipIntroByDefault ? 'false' : 'true'});

${this.handlers.map(handler => deindent`
${name}.on("${handler.name}", ${handler.var});
Expand Down Expand Up @@ -464,7 +464,7 @@ export default class Component extends Node {
}

block.builders.mount.addLine(
`${name}._mount(${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'});`
`${name}._mount(${parentNode || '#target'}, ${parentNode ? 'null' : 'anchor'}, ${compiler.options.skipIntroByDefault ? 'false' : 'true'});`
);

if (updates.length) {
Expand All @@ -483,7 +483,7 @@ export default class Component extends Node {
}

remount(name: string) {
return `${this.var}._mount(${name}._slotted.default, null);`;
return `${this.var}._mount(${name}._slotted.default, null, ${this.compiler.options.skipIntroByDefault ? 'false' : 'true'});`;
}

ssr() {
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ export interface CompileOptions {

onerror?: (error: Error) => void;
onwarn?: (warning: Warning) => void;

// to remove in v3
skipIntroByDefault?: boolean;
}

export interface GenerateOptions {
Expand Down
25 changes: 21 additions & 4 deletions src/shared/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assign } from './utils.js';
import { noop } from './utils.js';
import { assign, noop } from './utils.js';
import { transitionManager } from './transitions.js';
export * from './await-block.js';
export * from './dom.js';
export * from './keyed-each.js';
Expand Down Expand Up @@ -78,6 +78,21 @@ export function on(eventName, handler) {
};
}

export function outro() {
this.destroy();
return Promise.resolve();
}

export function outroWithTransitions() {
return new Promise(fulfil => {
transitionManager.groupOutros();
this._fragment.o(() => {
this.destroy();
fulfil();
});
});
}

export function run(fn) {
fn();
}
Expand Down Expand Up @@ -128,8 +143,8 @@ export function callAll(fns) {
while (fns && fns.length) fns.shift()();
}

export function _mount(target, anchor) {
this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null);
export function _mount(target, anchor, intro) {
this._fragment[intro && this._fragment.i ? 'i' : 'm'](target, anchor || null);
}

export var PENDING = {};
Expand All @@ -145,6 +160,7 @@ export var proto = {
get,
fire,
on,
outro,
set,
_recompute: noop,
_set,
Expand All @@ -157,6 +173,7 @@ export var protoDev = {
get,
fire,
on,
outro,
set: setDev,
_recompute: noop,
_set,
Expand Down
14 changes: 10 additions & 4 deletions test/cli/samples/basic/expected/Main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* src/Main.html generated by Svelte v2.4.4 */
/* src/Main.html generated by Svelte v2.5.1 */

function create_main_fragment(component, ctx) {
var p;
Expand Down Expand Up @@ -31,7 +31,7 @@ function Main(options) {

if (options.target) {
this._fragment.c();
this._mount(options.target, options.anchor);
this._mount(options.target, options.anchor, true);
}
}

Expand All @@ -40,6 +40,7 @@ assign(Main.prototype, {
get: get,
fire: fire,
on: on,
outro: outro,
set: set,
_set: _set,
_mount: _mount,
Expand Down Expand Up @@ -118,6 +119,11 @@ function on(eventName, handler) {
};
}

function outro() {
this.destroy();
return Promise.resolve();
}

function set(newState) {
this._set(assign({}, newState));
if (this.root._lock) return;
Expand Down Expand Up @@ -149,8 +155,8 @@ function _set(newState) {
}
}

function _mount(target, anchor) {
this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null);
function _mount(target, anchor, intro) {
this._fragment[intro && this._fragment.i ? 'i' : 'm'](target, anchor || null);
}

function _differs(a, b) {
Expand Down
14 changes: 10 additions & 4 deletions test/cli/samples/custom-element/expected/Main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* src/Main.html generated by Svelte v2.4.4 */
/* src/Main.html generated by Svelte v2.5.1 */

function create_main_fragment(component, ctx) {
var p;
Expand Down Expand Up @@ -37,7 +37,7 @@ class Main extends HTMLElement {
this._fragment.c();
this._fragment.m(this.shadowRoot, null);

if (options.target) this._mount(options.target, options.anchor);
if (options.target) this._mount(options.target, options.anchor, true);
}

static get observedAttributes() {
Expand All @@ -54,6 +54,7 @@ assign(Main.prototype, {
get: get,
fire: fire,
on: on,
outro: outro,
set: set,
_set: _set,
_mount: _mount,
Expand Down Expand Up @@ -139,6 +140,11 @@ function on(eventName, handler) {
};
}

function outro() {
this.destroy();
return Promise.resolve();
}

function set(newState) {
this._set(assign({}, newState));
if (this.root._lock) return;
Expand Down Expand Up @@ -170,8 +176,8 @@ function _set(newState) {
}
}

function _mount(target, anchor) {
this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null);
function _mount(target, anchor, intro) {
this._fragment[intro && this._fragment.i ? 'i' : 'm'](target, anchor || null);
}

function _differs(a, b) {
Expand Down
14 changes: 10 additions & 4 deletions test/cli/samples/dev/expected/Main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* src/Main.html generated by Svelte v2.4.4 */
/* src/Main.html generated by Svelte v2.5.1 */

function create_main_fragment(component, ctx) {
var p;
Expand Down Expand Up @@ -34,7 +34,7 @@ function Main(options) {
if (options.target) {
if (options.hydrate) throw new Error("options.hydrate only works if the component was compiled with the `hydratable: true` option");
this._fragment.c();
this._mount(options.target, options.anchor);
this._mount(options.target, options.anchor, true);
}
}

Expand All @@ -43,6 +43,7 @@ assign(Main.prototype, {
get: get,
fire: fire,
on: on,
outro: outro,
set: setDev,
_set: _set,
_mount: _mount,
Expand Down Expand Up @@ -121,6 +122,11 @@ function on(eventName, handler) {
};
}

function outro() {
this.destroy();
return Promise.resolve();
}

function setDev(newState) {
if (typeof newState !== 'object') {
throw new Error(
Expand Down Expand Up @@ -153,8 +159,8 @@ function _set(newState) {
}
}

function _mount(target, anchor) {
this._fragment[this._fragment.i ? 'i' : 'm'](target, anchor || null);
function _mount(target, anchor, intro) {
this._fragment[intro && this._fragment.i ? 'i' : 'm'](target, anchor || null);
}

function _differs(a, b) {
Expand Down
16 changes: 11 additions & 5 deletions test/cli/samples/dir-sourcemap/expected/Main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/cli/samples/dir-sourcemap/expected/Main.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions test/cli/samples/dir-sourcemap/expected/Widget.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/cli/samples/dir-sourcemap/expected/Widget.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading