Skip to content

Commit

Permalink
simplify some code
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessioCoser committed Mar 29, 2024
1 parent 4f5d87b commit 3637174
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
19 changes: 8 additions & 11 deletions src/dom/h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ export function h<K extends keyof HTMLElementTagNameMap>(
a?: DoomProperties<K> | Children,
b?: Children
): Element {
const { props, children } = prepare(a, b);
const el: HTMLElementTagNameMap[K] = document.createElement(component as K);
const { ...properties } = props as DoomProperties<K>;
const { properties, children } = prepareArguments(a, b);

toProperties(properties as DoomProperties<K>).forEach(({ key, value }) => {
toProperties(properties).forEach(({ key, value }) => {
if (key === "style") {
effect(() => {
const styles = evaluate(value as Reactive<Styles>);
Expand All @@ -35,9 +34,7 @@ export function h<K extends keyof HTMLElementTagNameMap>(
return;
}

effect(() => {
el[key] = evaluate(value);
});
effect(() => (el[key] = evaluate(value)));
});

addChildren(el, children as Children);
Expand Down Expand Up @@ -108,21 +105,21 @@ const pass = <T>(prop: Reactive<T>): T => prop as T;
const evaluate = <T>(prop: Reactive<T>): T =>
typeof prop !== "function" ? prop : (prop as Function)();

function prepare<K extends keyof HTMLElementTagNameMap>(
function prepareArguments<K extends keyof HTMLElementTagNameMap>(
a: unknown,
b: unknown
) {
if (!a && !b) {
return { props: {} as DoomProperties<K>, children: [] as Children };
return { properties: {} as DoomProperties<K>, children: [] as Children };
}

if (!b) {
if (Array.isArray(a) || typeof a === "function" || typeof a === "string") {
return { props: {} as DoomProperties<K>, children: a as Children };
return { properties: {} as DoomProperties<K>, children: a as Children };
} else {
return { props: a as DoomProperties<K>, children: [] as Children };
return { properties: a as DoomProperties<K>, children: [] as Children };
}
}

return { props: a as DoomProperties<K>, children: b as Children };
return { properties: a as DoomProperties<K>, children: b as Children };
}
4 changes: 0 additions & 4 deletions src/reactivity/utils/MapSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,4 @@ export class MapSet<K, V> {
get(key: K | null): V[] {
return (key && this.map.get(key)) || [];
}

first(key: K | null): V | null {
return (key && this.map.get(key)?.[0]) ?? null;
}
}
6 changes: 3 additions & 3 deletions src/reactivity/utils/Subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Subscriber, _Signal } from "../types";

export class Subscriptions {
private currentSubscriber: Subscriber | null = null;
private ancestors = new MapSet<_Signal<any>, _Signal<any>>();
private ancestors = new Map<_Signal<any>, _Signal<any>>();
private subscriptions = new MapSet<_Signal<any>, Subscriber>();

run(subscriber: Subscriber, fn: () => void): void {
Expand All @@ -22,7 +22,7 @@ export class Subscriptions {
}

if (subscriber.derived) {
this.ancestors.addTo(subscriber.derived, signal);
this.ancestors.set(subscriber.derived, signal);
}

this.subscriptions.addTo(this._getAncestor(signal), subscriber);
Expand All @@ -36,7 +36,7 @@ export class Subscriptions {
}

private _getAncestor(signal: _Signal<any>): _Signal<any> {
const ancestor = this.ancestors.first(signal);
const ancestor = this.ancestors.get(signal);
if (!ancestor) {
return signal;
}
Expand Down

0 comments on commit 3637174

Please sign in to comment.