Refactor $$setup function

pull/5139/head
Christopher Mardell 5 years ago
parent 07e2dde83d
commit 3c69b175be

@ -463,7 +463,7 @@ export default function dom(
constructor(options) { constructor(options) {
super(); super();
if (options) { if (options) {
this.$$runSetup(options); this.$$setup(options);
} }
} }
$$setup(options) { $$setup(options) {

@ -171,32 +171,26 @@ if (typeof HTMLElement === 'function') {
this.attachShadow({ mode: 'open' }); this.attachShadow({ mode: 'open' });
} }
// placeholder object to allow props to be set pre-$$setup
$$initialProps: Record<string, any> | null = {}; $$initialProps: Record<string, any> | null = {};
$$options: Record<string, any> | null = {}; $$setup(_options?) {
$$setup(_options) {
// overridden by instance // overridden by instance
} }
$$runSetup(options?: Record<string, any>) {
console.log("$$runSetup");
const {$$initialProps} = this;
if ($$initialProps) {
const opts = (options || this.$$options);
this.$$initialProps = null;
this.$$options = null;
this.$$setup({
...opts,
props: {
...opts.props,
...$$initialProps
}
});
}
}
connectedCallback() { connectedCallback() {
console.log("connectedCallback"); console.log("connectedCallback");
this.$$runSetup(); if (!this.$$) {
// wasn't set up from constructor as options were not ready
const options = Object.keys(this.$$initialProps).length ?
{
props: this.$$initialProps
} :
null;
this.$$setup(options);
// clean up
this.$$initialProps = null;
}
// @ts-ignore todo: improve typings // @ts-ignore todo: improve typings
for (const key in this.$$.slotted) { for (const key in this.$$.slotted) {
// @ts-ignore todo: improve typings // @ts-ignore todo: improve typings

Loading…
Cancel
Save