Skip to content

Commit c639d1b

Browse files
committed
fix: use State instead of Ref
1 parent 1d0a866 commit c639d1b

5 files changed

Lines changed: 15 additions & 11 deletions

File tree

hyper/node.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Tag } from "./lib/tags.ts";
22
import { EmptyElements } from "./lib/emptyElements.ts";
33
import { Attr } from "./lib/attributes.ts";
44
import { Falsy, isFalsy } from "./util.ts";
5-
import { Ref, ReadonlyRef } from "./state.ts";
5+
import { State, ReadonlyState } from "./state.ts";
66

77
export type NonEmptyElement = Exclude<Tag, EmptyElements>;
88

@@ -30,7 +30,9 @@ interface HN<T extends Tag = Tag> {
3030
children: (HyperNode<Tag> | HyperTextNode)[];
3131
}
3232

33-
export type HyperNode<T extends Tag = Tag> = { [T in Tag]: HN<T> }[T];
33+
export type HyperNodes = { [T in Tag]: HN<T> };
34+
35+
export type HyperNode<T extends Tag = Tag> = HyperNodes[T];
3436

3537
// TypeScript constructors cannot return custom types, including unions.
3638
// Instead, we create a class expression and assert it to the correct constructor type which returns the HyperNode union.
@@ -44,16 +46,16 @@ export type HyperNodeish<T extends Tag = Tag> =
4446
| HyperTextNode
4547
| HyperHTMLStringNode
4648
| Falsy
47-
| Ref<HyperNode<T> | HyperTextNode | HyperHTMLStringNode | Falsy>
48-
| ReadonlyRef<HyperNode<T> | HyperTextNode | HyperHTMLStringNode | Falsy>;
49+
| State<HyperNode<T> | HyperTextNode | HyperHTMLStringNode | Falsy>
50+
| ReadonlyState<HyperNode<T> | HyperTextNode | HyperHTMLStringNode | Falsy>;
4951

5052
// deno-lint-ignore no-explicit-any
5153
const isHyperNode = (n: any): n is HyperNode | HyperHTMLStringNode | HyperTextNode =>
5254
n instanceof HyperNode || n instanceof HyperHTMLStringNode || typeof n === "string";
5355

5456
export function normaliseParams<T extends Tag>(props?: Attr<T> | HyperNodeish, childNodes?: HyperNodeish[]) {
5557
const [attrs, children]: [Attr<T>, HyperNodeish[]] =
56-
isHyperNode(props) || isFalsy(props) || Ref.isRef(props)
58+
isHyperNode(props) || isFalsy(props) || State.isState(props)
5759
? [{}, [props, ...(childNodes || [])]]
5860
: [props || {}, childNodes || []];
5961

hyper/render/dom.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { guessEnv } from "../guessEnv.ts";
22
import { Falsy, isFalsy } from "../util.ts";
3-
import { Ref, ReadonlyRef } from "../state.ts";
3+
import { State, ReadonlyState } from "../state.ts";
44
import { Document, HTMLElement, Node, Text } from "../lib/dom.ts";
55
import { HyperHTMLStringNode, HyperNodeish } from "../node.ts";
66

@@ -17,7 +17,7 @@ type NodeToDOM<N extends HyperNodeish> = N extends Falsy
1717
? null
1818
: N extends string
1919
? Text
20-
: N extends ReadonlyRef<string>
20+
: N extends ReadonlyState<string>
2121
? Text
2222
: HTMLElement;
2323

@@ -48,7 +48,7 @@ const toDOM = function toDOM(parent: HTMLElement, node: HyperNodeish): Node | nu
4848
if (typeof node === "string") return document.createTextNode(node);
4949
if (isFalsy(node)) return null;
5050
if (node instanceof HyperHTMLStringNode) return htmlStringToElement(node.htmlString);
51-
if (Ref.isRef(node)) {
51+
if (State.isState(node)) {
5252
let init = toDOM(parent, node.value);
5353

5454
node.listen(val => {

hyper/render/html.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { HyperHTMLStringNode, HyperNodeish } from "../node.ts";
22
import { EmptyElements } from "../lib/emptyElements.ts";
3-
import { Ref } from "../state.ts";
3+
import { State } from "../state.ts";
44
import { escapeAttr, escapeTextNode, type Falsy, isFalsy } from "../util.ts";
55

66
// deno-lint-ignore no-explicit-any
@@ -30,7 +30,7 @@ export function renderHTML(node: HyperNodeish): string {
3030
if (isFalsy(node)) return "";
3131
if (typeof node === "string") return escapeTextNode(node);
3232
if (node instanceof HyperHTMLStringNode) return node.htmlString;
33-
if (Ref.isRef(node)) return renderHTML(node.value);
33+
if (State.isState(node)) return renderHTML(node.value);
3434

3535
let stringified = "<" + node.tag;
3636

hyper/state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class State<T = any> extends ReadonlyState<T> {
3939
}
4040

4141
/**
42-
* Merge multiple refs into a single Ref
42+
* Merge multiple states into a single state
4343
*/
4444
static merge<T>(...states: [State<T>, ...State<T>[]]): MergedState<MapEntries<State<T>[]>>;
4545

hyper/util.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ export type Falsy = SetContents<typeof Falsy>;
1919

2020
// deno-lint-ignore no-explicit-any
2121
export const isFalsy = (n: any): n is Falsy => Falsy.has(n);
22+
23+
export type Keyof<O> = Extract<keyof O, string>;

0 commit comments

Comments
 (0)