revojs 0.0.57 → 0.0.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -72,7 +72,9 @@ export interface CustomElementConstructor<TEvents extends Events, TAttributes ex
72
72
  export declare class MountedEvent extends Event {
73
73
  constructor();
74
74
  }
75
- export declare const isTemplate: (value?: any) => value is Template;
75
+ export declare const isTemplate: <T>(value?: T) => value is Template & T;
76
+ export declare const isCustomElement: <T>(value?: T) => value is CustomElement<Events, Attributes> & T;
77
+ export declare const isComponent: <T>(value?: T) => value is ComponentConstructor<Events, Attributes> & T;
76
78
  export declare const useHost: (scope: Scope) => HostContext;
77
79
  export declare const createElement: <TEvents extends Events, TAttributes extends Attributes>(input: string | ComponentConstructor<TEvents, TAttributes>, attributes?: AttributeInput<TAttributes>, ...children: Array<Slot>) => Slot;
78
80
  export declare const toString: (slot: Slot) => string;
package/dist/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { defu } from "defu";
2
- import { h } from "revojs/jsx-runtime";
3
2
 
4
3
  //#region src/app/index.ts
5
4
  const createApp = (config) => {
@@ -112,8 +111,15 @@ var Scope = class extends EventTarget {
112
111
  context;
113
112
  constructor(parentScope) {
114
113
  super();
115
- this.parentScope = parentScope;
116
114
  this.context = new Map();
115
+ this.setParentScope(parentScope);
116
+ }
117
+ getParentScope() {
118
+ return this.parentScope;
119
+ }
120
+ setParentScope(parentScope) {
121
+ this.parentScope = parentScope;
122
+ return this.parentScope?.onStop(() => this.stop());
117
123
  }
118
124
  getContext(input) {
119
125
  let scope = this;
@@ -136,11 +142,9 @@ var Scope = class extends EventTarget {
136
142
  }
137
143
  };
138
144
  var Compute = class extends Scope {
139
- scope;
140
145
  invoke;
141
- constructor(scope, invoke) {
142
- super();
143
- this.scope = scope;
146
+ constructor(parentScope, invoke) {
147
+ super(parentScope);
144
148
  this.invoke = invoke;
145
149
  }
146
150
  run() {
@@ -156,10 +160,7 @@ var Handler = class Handler {
156
160
  const set = computes.get(key) ?? new Set();
157
161
  computes.set(key, set.add(compute));
158
162
  targets.set(target, computes);
159
- compute.scope.onStop(() => {
160
- compute.stop();
161
- set.delete(compute);
162
- });
163
+ compute.getParentScope()?.onStop(() => set.delete(compute));
163
164
  }
164
165
  const value = Reflect.get(target, key);
165
166
  if (value) {
@@ -217,22 +218,39 @@ var MountedEvent = class extends Event {
217
218
  }
218
219
  };
219
220
  const isTemplate = (value) => {
220
- return typeof value === "object" && "tag" in value && "attributes" in value && "children" in value;
221
+ return !!value && typeof value === "object" && "tag" in value && "attributes" in value && "children" in value;
222
+ };
223
+ const isCustomElement = (value) => {
224
+ return !!value && typeof value === "object" && "component" in value;
225
+ };
226
+ const isComponent = (value) => {
227
+ return !!value && typeof value === "function" && "$name" in value;
221
228
  };
222
229
  const useHost = (scope) => {
223
230
  return scope.getContext(HOST_CONTEXT);
224
231
  };
225
232
  const createElement = (input, attributes, ...children) => {
226
- const template = {
227
- tag: typeof input === "function" ? input.$name : input,
233
+ if (isComponent(input)) {
234
+ const template = {
235
+ tag: input.$name,
236
+ attributes: attributes ?? {},
237
+ children
238
+ };
239
+ if (input.$styles.length) {
240
+ const classes = template.attributes["class"];
241
+ template.attributes["class"] = (classes ? [classes, ...input.$styles] : input.$styles).join(" ");
242
+ }
243
+ return template;
244
+ }
245
+ if (typeof input === "string") return {
246
+ tag: input,
228
247
  attributes: attributes ?? {},
229
248
  children
230
249
  };
231
- if (typeof input === "function" && input.$styles.length) {
232
- const classes = template.attributes["class"];
233
- template.attributes["class"] = (classes ? [classes, ...input.$styles] : input.$styles).join(" ");
234
- }
235
- return template;
250
+ return input?.({
251
+ ...attributes,
252
+ children
253
+ });
236
254
  };
237
255
  const toString = (slot) => {
238
256
  switch (typeof slot) {
@@ -302,17 +320,16 @@ const hydrate = async (scope, parentNode, slot, index, previous) => {
302
320
  const Component = components.get(slot.tag);
303
321
  if (Component) registerComponent(Component);
304
322
  if (previous || !(hydration instanceof Element && hydration.tagName.toUpperCase() === slot.tag.toUpperCase())) hydration = document.createElementNS(namespace(slot.tag), slot.tag);
305
- for (const [name, value] of Object.entries(slot.attributes)) createCompute(scope, (childScope) => {
323
+ for (const [name, value] of Object.entries(slot.attributes)) createCompute(scope, (scope$1) => {
306
324
  const target = hydration;
307
325
  if (name.startsWith("on")) {
308
326
  const event = name.substring(2).toLowerCase();
309
- useEvent(childScope, target, event, value);
327
+ useEvent(scope$1, target, event, value);
310
328
  } else {
311
329
  const set = toString(value);
312
330
  if (set === "" || set === "false") return target.removeAttribute(name);
313
331
  return target.setAttribute(name, set);
314
332
  }
315
- scope.onStop(() => childScope.stop());
316
333
  });
317
334
  for (const [index$1, childSlot] of slot.children.entries()) await hydrate(scope, hydration, childSlot, index$1, previous);
318
335
  }
@@ -342,7 +359,6 @@ const renderToString = async (scope, slot) => {
342
359
  let content = `<${prefix}>`;
343
360
  if (CustomElement) {
344
361
  const element = new CustomElement(slot.attributes, scope);
345
- scope.onStop(() => element.scope.stop());
346
362
  const template = await renderToString(element.scope, await element.setup());
347
363
  if (element.shadowRoot) {
348
364
  const shadow = {
@@ -393,15 +409,14 @@ const toCustomElement = (Component) => {
393
409
  internals = this.attachInternals();
394
410
  async connectedCallback() {
395
411
  let rootNode = this;
396
- const findParent = (node) => {
412
+ const findParentScope = (node) => {
397
413
  if (node) {
398
- if ("component" in node) return node;
399
- if (node instanceof ShadowRoot) return findParent(node.host);
400
- return findParent(node.parentNode);
414
+ if (isCustomElement(node)) return node.component.scope;
415
+ if (node instanceof ShadowRoot) return findParentScope(node.host);
416
+ return findParentScope(node.parentNode);
401
417
  }
402
418
  };
403
- const parentNode = findParent(this.parentNode);
404
- if (parentNode) this.component.scope.parentScope = parentNode.component.scope;
419
+ this.component.scope.setParentScope(findParentScope(this.parentNode));
405
420
  if (this.component.shadowRoot) rootNode = this.shadowRoot ?? this.attachShadow(this.component.shadowRoot);
406
421
  for (const [name, event] of Object.entries(Component.$events)) Reflect.set(this.component.events, name, (value) => {
407
422
  if (value instanceof Event) return;
@@ -515,6 +530,9 @@ var Radix = class Radix {
515
530
 
516
531
  //#endregion
517
532
  //#region src/runtime/index.ts
533
+ const isRoute = (value) => {
534
+ return !!value && typeof value === "object" && "fetch" in value;
535
+ };
518
536
  const useRuntime = (scope) => {
519
537
  return scope.getContext(RUNTIME_CONTEXT);
520
538
  };
@@ -566,8 +584,8 @@ const createRuntime = async () => {
566
584
  const [name, method] = toPath(path);
567
585
  radix.insert((method ?? "GET").toUpperCase() + name, defineRoute({ fetch: async (event) => {
568
586
  const route = await routes[path]?.();
569
- if (typeof route === "object") return route.fetch(event);
570
- if (route) return sendHtml(event, await renderToString(event, await import("#virtual/client").then((module) => module.client)));
587
+ if (isRoute(route)) return route.fetch(event);
588
+ return sendHtml(event, await renderToString(event, await import("#virtual/client").then((module) => module.client)));
571
589
  } }));
572
590
  }
573
591
  const assets = await import("#virtual/assets").then((module) => module.assets);
@@ -800,10 +818,7 @@ const Page = defineComponent({
800
818
  name: "x-page",
801
819
  setup: ({ scope }) => {
802
820
  const { route } = useRouter(scope);
803
- return () => {
804
- const Page$1 = route.value;
805
- if (Page$1) return /* @__PURE__ */ h(Page$1, null);
806
- };
821
+ return () => route.value;
807
822
  }
808
823
  });
809
824
 
@@ -852,4 +867,4 @@ const useLocale = (scope, context) => {
852
867
  };
853
868
 
854
869
  //#endregion
855
- export { $fetch, CLIENT, Compute, HOST_CONTEXT, Handler, LOCALE_CONTEXT, MountedEvent, NavigateEvent, Page, ROUTER_CONTEXT, ROUTE_CONTEXT, RUNTIME_CONTEXT, Radix, SERVER, Scope, StopEvent, activeCompute, components, createApp, createCompute, createElement, createLocale, createMemo, createRouter, createRuntime, createState, defineComponent, defineContext, defineMiddleware, defineRoute, fileName, fromValue, hydrate, isClient, isServer, isTemplate, mimeType, preventDefault, registerComponent, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, stopImmediatePropagation, stopPropagation, targets, toArray, toCustomElement, toFragment, toPath, toRange, toString, useCookies, useEvent, useHost, useLocale, useRequestUrl, useRoute, useRouter, useRuntime, useSetCookies };
870
+ export { $fetch, CLIENT, Compute, HOST_CONTEXT, Handler, LOCALE_CONTEXT, MountedEvent, NavigateEvent, Page, ROUTER_CONTEXT, ROUTE_CONTEXT, RUNTIME_CONTEXT, Radix, SERVER, Scope, StopEvent, activeCompute, components, createApp, createCompute, createElement, createLocale, createMemo, createRouter, createRuntime, createState, defineComponent, defineContext, defineMiddleware, defineRoute, fileName, fromValue, hydrate, isClient, isComponent, isCustomElement, isRoute, isServer, isTemplate, mimeType, preventDefault, registerComponent, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, stopImmediatePropagation, stopPropagation, targets, toArray, toCustomElement, toFragment, toPath, toRange, toString, useCookies, useEvent, useHost, useLocale, useRequestUrl, useRoute, useRouter, useRuntime, useSetCookies };
@@ -1,4 +1,7 @@
1
1
  import { type EventListener, type Slot } from "../html";
2
+ export type Fragment = {
3
+ children: Array<Slot>;
4
+ };
2
5
  export type EventAttributes = {
3
6
  onScroll?: EventListener<Event>;
4
7
  onScrollCapture?: EventListener<Event>;
@@ -365,3 +368,4 @@ export declare namespace JSX {
365
368
  export declare const svgElements: Set<string>;
366
369
  export declare const namespace: (tag: string) => "http://www.w3.org/1999/xhtml" | "http://www.w3.org/2000/svg";
367
370
  export declare const h: <TEvents extends import("..").Events, TAttributes extends import("..").Attributes>(input: string | import("..").ComponentConstructor<TEvents, TAttributes>, attributes?: import("..").AttributeInput<TAttributes>, ...children: Array<Slot>) => Slot;
371
+ export declare const fragment: ({ children }: Fragment) => unknown[];
package/dist/jsx/index.js CHANGED
@@ -5,17 +5,31 @@ function defineContext(key) {
5
5
 
6
6
  //#endregion
7
7
  //#region src/html/index.ts
8
+ const isComponent = (value) => {
9
+ return !!value && typeof value === "function" && "$name" in value;
10
+ };
8
11
  const createElement = (input, attributes, ...children) => {
9
- const template = {
10
- tag: typeof input === "function" ? input.$name : input,
12
+ if (isComponent(input)) {
13
+ const template = {
14
+ tag: input.$name,
15
+ attributes: attributes ?? {},
16
+ children
17
+ };
18
+ if (input.$styles.length) {
19
+ const classes = template.attributes["class"];
20
+ template.attributes["class"] = (classes ? [classes, ...input.$styles] : input.$styles).join(" ");
21
+ }
22
+ return template;
23
+ }
24
+ if (typeof input === "string") return {
25
+ tag: input,
11
26
  attributes: attributes ?? {},
12
27
  children
13
28
  };
14
- if (typeof input === "function" && input.$styles.length) {
15
- const classes = template.attributes["class"];
16
- template.attributes["class"] = (classes ? [classes, ...input.$styles] : input.$styles).join(" ");
17
- }
18
- return template;
29
+ return input?.({
30
+ ...attributes,
31
+ children
32
+ });
19
33
  };
20
34
  const HOST_CONTEXT = defineContext("HOST_CONTEXT");
21
35
 
@@ -104,6 +118,7 @@ const namespace = (tag) => {
104
118
  return svgElements.has(tag) ? "http://www.w3.org/2000/svg" : "http://www.w3.org/1999/xhtml";
105
119
  };
106
120
  const h = createElement;
121
+ const fragment = ({ children }) => children;
107
122
 
108
123
  //#endregion
109
- export { h, namespace, svgElements };
124
+ export { fragment, h, namespace, svgElements };
@@ -1,6 +1,6 @@
1
- import { RUNTIME_CONTEXT, Scope } from "./runtime-C9LNWVm0.js";
2
- import { runtime } from "#virtual/runtime";
1
+ import { RUNTIME_CONTEXT, Scope } from "./runtime-BWvA8m4e.js";
3
2
  import { serve } from "bun";
3
+ import { runtime } from "#virtual/runtime";
4
4
 
5
5
  //#region src/presets/bun.ts
6
6
  serve({ fetch: (request) => {
@@ -1,4 +1,4 @@
1
- import { RUNTIME_CONTEXT, Scope } from "./runtime-C9LNWVm0.js";
1
+ import { RUNTIME_CONTEXT, Scope } from "./runtime-BWvA8m4e.js";
2
2
  import { runtime } from "#virtual/runtime";
3
3
 
4
4
  //#region src/presets/cloudflare.ts
@@ -9,8 +9,15 @@ var Scope = class extends EventTarget {
9
9
  context;
10
10
  constructor(parentScope) {
11
11
  super();
12
- this.parentScope = parentScope;
13
12
  this.context = new Map();
13
+ this.setParentScope(parentScope);
14
+ }
15
+ getParentScope() {
16
+ return this.parentScope;
17
+ }
18
+ setParentScope(parentScope) {
19
+ this.parentScope = parentScope;
20
+ return this.parentScope?.onStop(() => this.stop());
14
21
  }
15
22
  getContext(input) {
16
23
  let scope = this;
@@ -1,7 +1,7 @@
1
- import { type Attributes, type ComponentConstructor, type Events } from "../html";
1
+ import { type Slot } from "../html";
2
2
  import { Radix } from "../radix";
3
3
  import { type Descriptor, Scope, type State } from "../signals";
4
- export type Routes = Record<string, () => Promise<ComponentConstructor<Events, Attributes>>>;
4
+ export type Routes = Record<string, () => Promise<Slot>>;
5
5
  export type RouterOptions<T extends Routes = Routes> = {
6
6
  routes: T;
7
7
  };
@@ -9,8 +9,8 @@ export type RouterContext<T extends RouterOptions = RouterOptions> = {
9
9
  options: T;
10
10
  navigator: EventTarget;
11
11
  url: State<URL | undefined>;
12
- radix: Radix<() => Promise<ComponentConstructor<Events, Attributes>>>;
13
- route: State<ComponentConstructor<Events, Attributes> | undefined>;
12
+ radix: Radix<() => Promise<Slot>>;
13
+ route: State<Slot | undefined>;
14
14
  };
15
15
  export declare class NavigateEvent extends Event {
16
16
  constructor();
@@ -20,7 +20,7 @@ export declare const createRouter: <T extends RouterOptions>(options: T) => {
20
20
  ROUTER_CONTEXT: Descriptor<RouterContext<T>>;
21
21
  registerRouterContext: (scope: Scope) => Promise<{
22
22
  url: State<URL | undefined>;
23
- route: State<ComponentConstructor<Events, Attributes> | undefined>;
23
+ route: State<unknown>;
24
24
  navigator: EventTarget;
25
25
  navigate: (path: string) => void;
26
26
  anchorNavigate: (event: Event) => void;
@@ -28,12 +28,12 @@ export declare const createRouter: <T extends RouterOptions>(options: T) => {
28
28
  };
29
29
  export declare const useRouter: <T extends RouterContext>(scope: Scope, context?: Descriptor<T>) => {
30
30
  url: State<URL | undefined>;
31
- route: State<ComponentConstructor<Events, Attributes> | undefined>;
31
+ route: State<unknown>;
32
32
  navigator: EventTarget;
33
33
  navigate: (path: string) => void;
34
34
  anchorNavigate: (event: Event) => void;
35
35
  };
36
- export declare const Page: ComponentConstructor<Events, Attributes>;
36
+ export declare const Page: import("..").ComponentConstructor<import("..").Events, import("..").Attributes>;
37
37
  declare global {
38
38
  interface ElementEventMap {
39
39
  navigate: NavigateEvent;
@@ -20,6 +20,7 @@ export type RuntimeContext<T = Record<string, unknown>> = {
20
20
  export type RouteContext = {
21
21
  inputs: State<Record<string, string>>;
22
22
  };
23
+ export declare const isRoute: <T>(value?: T) => value is Route & T;
23
24
  export declare const useRuntime: <T = Record<string, unknown>>(scope: Scope) => RuntimeContext<T>;
24
25
  export declare const useRoute: (scope: Scope) => RouteContext;
25
26
  export declare const defineRoute: (route: Route) => Route;
@@ -10,18 +10,19 @@ export declare class StopEvent extends Event {
10
10
  constructor();
11
11
  }
12
12
  export declare class Scope extends EventTarget {
13
- parentScope?: Scope;
13
+ private parentScope?;
14
14
  readonly context: Map<string, unknown>;
15
15
  constructor(parentScope?: Scope);
16
+ getParentScope(): Scope | undefined;
17
+ setParentScope(parentScope?: Scope): void | undefined;
16
18
  getContext<T>(input: Descriptor<T>): T;
17
19
  setContext<T>(input: Descriptor<T>, value: T): void;
18
20
  onStop(input: (event: StopEvent) => void): void;
19
21
  stop(): boolean;
20
22
  }
21
23
  export declare class Compute<T = void> extends Scope {
22
- readonly scope: Scope;
23
24
  readonly invoke: (scope: Scope) => T;
24
- constructor(scope: Scope, invoke: (scope: Scope) => T);
25
+ constructor(parentScope: Scope, invoke: (scope: Scope) => T);
25
26
  run(): T;
26
27
  }
27
28
  export declare class Handler<T extends object> implements ProxyHandler<T> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.0.57",
3
+ "version": "0.0.59",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",
@@ -24,7 +24,10 @@
24
24
  "types": "./dist/index.d.ts",
25
25
  "module": "./dist/index.js",
26
26
  "main": "./dist/index.js",
27
- "files": ["dist", "src/types"],
27
+ "files": [
28
+ "dist",
29
+ "src/types"
30
+ ],
28
31
  "scripts": {
29
32
  "build": "rolldown -c rolldown.config.ts && tsc",
30
33
  "watch": "rolldown -w -c rolldown.config.ts && tsc --watch"
@@ -13,7 +13,9 @@ declare module "#virtual/client" {
13
13
  }
14
14
 
15
15
  declare module "#virtual/routes" {
16
- export const routes: Record<string, () => Promise<Route | ComponentConstructor<Events, Attributes>>>;
16
+ import type { Route, Slot } from "revojs";
17
+
18
+ export const routes: Record<string, () => Promise<Route | Slot>>;
17
19
  }
18
20
 
19
21
  declare module "#virtual/runtime" {