Skip to content

Commit

Permalink
export functions from dom-expressions server.js to match client.js ex…
Browse files Browse the repository at this point in the history
…ports, and make client-only functions throw on the serverside
  • Loading branch information
trusktr committed Sep 1, 2024
1 parent ab2d670 commit 7fc029c
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 4 deletions.
5 changes: 3 additions & 2 deletions packages/dom-expressions/src/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function dynamicProperty(props: unknown, key: string): unknown;
export function hydrate(
fn: () => JSX.Element,
node: MountableElement,
options?: { renderId?: string, owner?: unknown }
options?: { renderId?: string; owner?: unknown }
): () => void;
export function getHydrationKey(): string;
export function getNextElement(template?: HTMLTemplateElement): Element;
Expand All @@ -74,4 +74,5 @@ export interface RequestEvent {
request: Request;
}
export declare const RequestContext: unique symbol;
export function getRequestEvent(): RequestEvent | undefined;
export function getRequestEvent(): RequestEvent | undefined;
export function runHydrationEvents(): void;
2 changes: 1 addition & 1 deletion packages/dom-expressions/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ function insertExpression(parent, value, current, marker, unwrapArray) {
if (marker === undefined) return (current = [...parent.childNodes]);
let node = array[0];
if (node.parentNode !== parent) return current;
const nodes = [node]
const nodes = [node];
while ((node = node.nextSibling) !== marker) nodes.push(node);
return (current = nodes);
}
Expand Down
85 changes: 85 additions & 0 deletions packages/dom-expressions/src/server.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
export const Aliases: Record<string, string>;
export const Properties: Set<string>;
export const ChildProperties: Set<string>;
export const DelegatedEvents: Set<string>;
export const DOMElements: Set<string>;
export const SVGElements: Set<string>;
export const SVGNamespace: Record<string, string>;
export function getPropAlias(prop: string, tagName: string): string | undefined;

export function renderToString<T>(
fn: () => T,
options?: {
Expand Down Expand Up @@ -87,3 +96,79 @@ export function pipeToNodeWritable<T>(
onCompleteAll?: () => void;
}
): void;

export function untrack<T>(fn: () => T): T;

// client-only APIs

/** @deprecated not supported on the server side */
export function classList(
node: Element,
value: { [k: string]: boolean },
prev?: { [k: string]: boolean }
): { [k: string]: boolean };

/** @deprecated not supported on the server side */
export function style(
node: Element,
value: { [k: string]: string },
prev?: { [k: string]: string }
): void;

/** @deprecated not supported on the server side */
export function insert<T>(
parent: MountableElement,
accessor: (() => T) | T,
marker?: Node | null,
init?: JSX.Element
): JSX.Element;

/** @deprecated not supported on the server side */
export function spread<T>(
node: Element,
accessor: (() => T) | T,
isSVG?: Boolean,
skipChildren?: Boolean
): void;

/** @deprecated not supported on the server side */
export function delegateEvents(eventNames: string[], d?: Document): void;
/** @deprecated not supported on the server side */
export function dynamicProperty(props: unknown, key: string): unknown;
/** @deprecated not supported on the server side */
export function setAttribute(node: Element, name: string, value: string): void;
/** @deprecated not supported on the server side */
export function setAttributeNS(node: Element, namespace: string, name: string, value: string): void;

/** @deprecated not supported on the server side */
export function addEventListener(
node: Element,
name: string,
handler: () => void,
delegate: boolean
): void;

/** @deprecated not supported on the server side */
export function render(code: () => JSX.Element, element: MountableElement): () => void;
/** @deprecated not supported on the server side */
export function template(html: string, isCE?: boolean, isSVG?: boolean): () => Element;
/** @deprecated not supported on the server side */
export function setProperty(node: Element, name: string, value: any): void;
/** @deprecated not supported on the server side */
export function className(node: Element, value: string): void;
/** @deprecated not supported on the server side */
export function assign(node: Element, props: any, isSVG?: Boolean, skipChildren?: Boolean): void;

/** @deprecated not supported on the server side */
export function hydrate(
fn: () => JSX.Element,
node: MountableElement,
options?: { renderId?: string; owner?: unknown }
): () => void;

/** @deprecated not supported on the server side */
export function getNextElement(template?: HTMLTemplateElement): Element;
/** @deprecated not supported on the server side */
export function getNextMatch(start: Node, elementName: string): Element;
/** @deprecated not supported on the server side */
export function getNextMarker(start: Node): [Node, Array<Node>];
39 changes: 38 additions & 1 deletion packages/dom-expressions/src/server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { Aliases, BooleanAttributes, ChildProperties } from "./constants";
import { sharedConfig, root } from "rxcore";
import { createSerializer, getLocalHeaderScript } from "./serializer";
export { createComponent } from "rxcore";

export {
getOwner,
createComponent,
createRenderEffect as effect,
createMemo as memo,
untrack
} from "rxcore";

export {
Properties,
Expand Down Expand Up @@ -685,3 +692,33 @@ export function ssrSpread(props, isSVG, skipChildren) {
}
return result;
}

// client-only APIs

export {
notSup as classList,
notSup as style,
notSup as insert,
notSup as spread,
notSup as delegateEvents,
notSup as dynamicProperty,
notSup as setAttribute,
notSup as setAttributeNS,
notSup as addEventListener,
notSup as render,
notSup as template,
notSup as setProperty,
notSup as className,
notSup as assign,
notSup as hydrate,
notSup as getNextElement,
notSup as getNextMatch,
notSup as getNextMarker,
notSup as runHydrationEvents
};

function notSup() {
throw new Error(
"Client-only API called on the server side. Run client-only code in onMount, or conditionally run client-only component with <Show>."
);
}

0 comments on commit 7fc029c

Please sign in to comment.