From 24469762ec6fac5b437ae7d63d32fe5798d8e80d Mon Sep 17 00:00:00 2001 From: Damian Tarnawski Date: Mon, 20 Feb 2023 07:31:42 +0100 Subject: [PATCH] DEV: Add component function to component owner (#1563) * Add component funciton to dev component * Create nice-dogs-draw.md --- .changeset/nice-dogs-draw.md | 6 ++++++ packages/solid/src/reactive/signal.ts | 17 ++++++++++++----- packages/solid/test/dev.spec.ts | 17 ++++++++++++++++- 3 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 .changeset/nice-dogs-draw.md diff --git a/.changeset/nice-dogs-draw.md b/.changeset/nice-dogs-draw.md new file mode 100644 index 000000000..2780afc81 --- /dev/null +++ b/.changeset/nice-dogs-draw.md @@ -0,0 +1,6 @@ +--- +"solid-js": patch +--- + +Add a reference to the component funciton to DevComponent owner. +Rename DevComponent's property from `componentName` to `name`. diff --git a/packages/solid/src/reactive/signal.ts b/packages/solid/src/reactive/signal.ts index 4e4061b3d..bfcd25429 100644 --- a/packages/solid/src/reactive/signal.ts +++ b/packages/solid/src/reactive/signal.ts @@ -136,7 +136,7 @@ export function createRoot(fn: RootFunction, detachedOwner?: typeof Owner) : fn : () => fn(() => untrack(() => cleanNode(root))); - if ("_SOLID_DEV_") DevHooks.afterCreateOwner && DevHooks.afterCreateOwner(root);; + if ("_SOLID_DEV_") DevHooks.afterCreateOwner && DevHooks.afterCreateOwner(root); Owner = root; Listener = null; @@ -1048,7 +1048,8 @@ export function resumeEffects(e: Computation[]) { export interface DevComponent extends Memo { props: T; - componentName: string; + name: string; + component: (props: T) => unknown; } // Dev @@ -1066,7 +1067,8 @@ export function devComponent(Comp: (props: P) => V, props: P): V { c.props = props; c.observers = null; c.observerSlots = null; - c.componentName = Comp.name; + c.name = Comp.name; + c.component = Comp; updateComputation(c); return (c.tValue !== undefined ? c.tValue : c.value) as V; } @@ -1632,14 +1634,19 @@ function castError(err: unknown): Error { return new Error(typeof err === "string" ? err : "Unknown error", { cause: err }); } function runErrors(fns: ((err: any) => void)[], err: any) { - for (const f of fns) f(err) + for (const f of fns) f(err); } function handleError(err: unknown) { const fns = ERROR && lookup(Owner, ERROR); if (!fns) throw err; const error = castError(err); if (Effects) - Effects!.push({ fn() { runErrors(fns, error); }, state: STALE } as unknown as Computation); + Effects!.push({ + fn() { + runErrors(fns, error); + }, + state: STALE + } as unknown as Computation); else runErrors(fns, error); } diff --git a/packages/solid/test/dev.spec.ts b/packages/solid/test/dev.spec.ts index c890a48d1..a80bd87a5 100644 --- a/packages/solid/test/dev.spec.ts +++ b/packages/solid/test/dev.spec.ts @@ -5,8 +5,10 @@ import { createEffect, createComputed, DEV, - createContext + createContext, + createComponent } from "../src"; +import type { DevComponent } from "../src/reactive/signal"; import { createStore, unwrap, DEV as STORE_DEV } from "../store/src"; describe("Dev features", () => { @@ -139,4 +141,17 @@ describe("Dev features", () => { expect(cb).toHaveBeenCalledTimes(2); expect(cb).toHaveBeenCalledWith(unwrap(s.inner), "foo", 2, 1); }); + + test("createComponent should create a component owner in DEV", () => { + createRoot(() => { + const props = {}; + createComponent(function MyComponent() { + const owner = getOwner() as DevComponent<{}>; + expect(owner.name).toBe("MyComponent"); + expect(owner.props).toBe(props); + expect(owner.component).toBe(MyComponent); + return null; + }, props); + }); + }); });