Skip to content

Commit

Permalink
DEV: Add component function to component owner (#1563)
Browse files Browse the repository at this point in the history
* Add component funciton to dev component

* Create nice-dogs-draw.md
  • Loading branch information
thetarnav authored Feb 20, 2023
1 parent 04aaaf5 commit 2446976
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/nice-dogs-draw.md
Original file line number Diff line number Diff line change
@@ -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`.
17 changes: 12 additions & 5 deletions packages/solid/src/reactive/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export function createRoot<T>(fn: RootFunction<T>, 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;
Expand Down Expand Up @@ -1048,7 +1048,8 @@ export function resumeEffects(e: Computation<any>[]) {

export interface DevComponent<T> extends Memo<unknown> {
props: T;
componentName: string;
name: string;
component: (props: T) => unknown;
}

// Dev
Expand All @@ -1066,7 +1067,8 @@ export function devComponent<P, V>(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;
}
Expand Down Expand Up @@ -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<any>);
Effects!.push({
fn() {
runErrors(fns, error);
},
state: STALE
} as unknown as Computation<any>);
else runErrors(fns, error);
}

Expand Down
17 changes: 16 additions & 1 deletion packages/solid/test/dev.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 2446976

Please sign in to comment.