From 3c69b175be220a9bf37792b0ff06944e5e6b9f01 Mon Sep 17 00:00:00 2001 From: Christopher Mardell Date: Mon, 13 Jul 2020 21:09:54 +0930 Subject: [PATCH] Refactor $$setup function --- src/compiler/compile/render_dom/index.ts | 2 +- src/runtime/internal/Component.ts | 34 ++++++++++-------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts index 5b4b47895e..868e274f2e 100644 --- a/src/compiler/compile/render_dom/index.ts +++ b/src/compiler/compile/render_dom/index.ts @@ -463,7 +463,7 @@ export default function dom( constructor(options) { super(); if (options) { - this.$$runSetup(options); + this.$$setup(options); } } $$setup(options) { diff --git a/src/runtime/internal/Component.ts b/src/runtime/internal/Component.ts index f1334c791d..73b24f84d0 100644 --- a/src/runtime/internal/Component.ts +++ b/src/runtime/internal/Component.ts @@ -171,32 +171,26 @@ if (typeof HTMLElement === 'function') { this.attachShadow({ mode: 'open' }); } + // placeholder object to allow props to be set pre-$$setup $$initialProps: Record | null = {}; - $$options: Record | null = {}; - $$setup(_options) { + $$setup(_options?) { // overridden by instance } - $$runSetup(options?: Record) { - 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() { 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 for (const key in this.$$.slotted) { // @ts-ignore todo: improve typings