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 onunmount hook (#1212) #1234

Closed
wants to merge 3 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
4 changes: 4 additions & 0 deletions src/compile/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,10 @@ export default class Compiler {
addDeclaration('oncreate', templateProperties.oncreate.value);
}

if (templateProperties.onunmount && dom) {
addDeclaration('onunmount', templateProperties.onunmount.value);
}

if (templateProperties.ondestroy && dom) {
addDeclaration('ondestroy', templateProperties.ondestroy.value);
}
Expand Down
5 changes: 5 additions & 0 deletions src/compile/dom/Block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export default class Block {
claim: new CodeBuilder(),
hydrate: new CodeBuilder(),
mount: new CodeBuilder(),
unmount: new CodeBuilder(),
measure: new CodeBuilder(),
fix: new CodeBuilder(),
animate: new CodeBuilder(),
Expand All @@ -100,6 +101,10 @@ export default class Block {
if (this.key) this.aliases.set('key', this.getUniqueName('key'));

this.hasUpdateMethod = false; // determined later

if (this.compiler.templateProperties.onunmount) {
this.builders.unmount.addLine(`%onunmount.call(#component${this.key ? `, ${this.getUniqueName('key')}` : ''})`);
}
}

addDependencies(dependencies: Set<string>) {
Expand Down
6 changes: 6 additions & 0 deletions src/compile/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ export default function dom(
}];`
)}

${(templateProperties.onunmount || storeProps.length) && (
`this._handlers.unmount = [${
[templateProperties.onunmount && `%onunmount`, storeProps.length && `@removeFromStore`].filter(Boolean).join(', ')
}];`
)}

${compiler.slots.size && `this._slotted = options.slots || {};`}

${compiler.customElement ?
Expand Down
1 change: 1 addition & 0 deletions src/shared/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function blankObject() {

export function destroy(detach) {
this.destroy = noop;
this.fire('unmount');
this.fire('destroy');
this.set = noop;

Expand Down
2 changes: 2 additions & 0 deletions src/validate/js/propValidators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import actions from './actions';
import animations from './animations';
import computed from './computed';
import oncreate from './oncreate';
import onunmount from './onunmount';
import ondestroy from './ondestroy';
import onstate from './onstate';
import onupdate from './onupdate';
Expand All @@ -27,6 +28,7 @@ export default {
animations,
computed,
oncreate,
onunmount,
ondestroy,
onstate,
onupdate,
Expand Down
14 changes: 14 additions & 0 deletions src/validate/js/propValidators/onunmount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import usesThisOrArguments from '../utils/usesThisOrArguments';
import { Validator } from '../../';
import { Node } from '../../../interfaces';

export default function onunmont(validator: Validator, prop: Node) {
if (prop.value.type === 'ArrowFunctionExpression') {
if (usesThisOrArguments(prop.value.body)) {
validator.error(
`'onunmont' should be a function expression, not an arrow function expression`,
prop.start
);
}
}
}
17 changes: 17 additions & 0 deletions test/runtime/samples/lifecycle-events/Nested.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div ref:div></div>

<script>
export default {
oncreate() {
this.events = {create: this.refs.div.parentNode === null};
},

onunmount() {
this.events.unmount = this.refs.div.parentNode === null;
},

ondestroy() {
this.events.destroy = this.refs.div.parentNode === null;
}
}
</script>
7 changes: 6 additions & 1 deletion test/runtime/samples/lifecycle-events/_config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
export default {
test(assert, component) {
const nested = component.refs.nested;


assert.deepEqual(component.events, ['create']);
assert.deepEqual(nested.events, { create: false });
component.destroy();
assert.deepEqual(component.events, ['create', 'destroy']);
assert.deepEqual(component.events, ['create', 'unmount', 'destroy']);
assert.deepEqual(nested.events, { create: false, unmount: false, destroy: false });
}
};
10 changes: 9 additions & 1 deletion test/runtime/samples/lifecycle-events/main.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
<div></div>
<Nested ref:nested />

<script>
import Nested from './Nested.html';
export default {
components: {
Nested,
},
oncreate() {
this.events = ['create'];
},

onunmount() {
this.events.push('unmount');
},

ondestroy() {
this.events.push('destroy');
}
Expand Down