add lifecycle hooks

pull/1299/head
Rich-Harris 7 years ago
parent 788aa89b41
commit a5d5a6c952

@ -1,4 +1,5 @@
import { Node } from './interfaces';
import Generator from './generators/Generator';
const now = (typeof process !== 'undefined' && process.hrtime)
? () => {
@ -63,31 +64,36 @@ export default class Stats {
this.currentChildren = this.currentTiming ? this.currentTiming.children : this.timings;
}
render({ imports }: {
imports: Node[]
}) {
render(generator: Generator) {
const timings = Object.assign({
total: now() - this.startTime
}, collapseTimings(this.timings));
const imports = generator.imports.map(node => {
return {
source: node.source.value,
specifiers: node.specifiers.map(specifier => {
return {
name: (
specifier.type === 'ImportDefaultSpecifier' ? 'default' :
specifier.type === 'ImportNamespaceSpecifier' ? '*' :
specifier.imported.name
),
as: specifier.local.name
};
})
}
});
const hooks: Record<string, boolean> = {};
if (generator.templateProperties.oncreate) hooks.oncreate = true;
if (generator.templateProperties.ondestroy) hooks.ondestroy = true;
return {
timings,
warnings: [], // TODO
imports: imports.map(node => {
return {
source: node.source.value,
specifiers: node.specifiers.map(specifier => {
return {
name: (
specifier.type === 'ImportDefaultSpecifier' ? 'default' :
specifier.type === 'ImportNamespaceSpecifier' ? '*' :
specifier.imported.name
),
as: specifier.local.name
};
})
}
})
imports,
hooks
};
}
}

@ -385,9 +385,7 @@ export default class Generator {
ast: this.ast,
js,
css,
stats: this.stats.render({
imports: this.imports
}),
stats: this.stats.render(this),
// TODO deprecate
code: js.code,

@ -0,0 +1,7 @@
export default {
test(assert, stats) {
assert.deepEqual(stats.hooks, {
oncreate: true
});
}
};

@ -0,0 +1,7 @@
<script>
export default {
oncreate() {
console.log('creating');
}
};
</script>
Loading…
Cancel
Save