revojs 0.0.57 → 0.0.58

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,8 @@ 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;
76
77
  export declare const useHost: (scope: Scope) => HostContext;
77
78
  export declare const createElement: <TEvents extends Events, TAttributes extends Attributes>(input: string | ComponentConstructor<TEvents, TAttributes>, attributes?: AttributeInput<TAttributes>, ...children: Array<Slot>) => Slot;
78
79
  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,7 +218,10 @@ 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;
221
225
  };
222
226
  const useHost = (scope) => {
223
227
  return scope.getContext(HOST_CONTEXT);
@@ -302,17 +306,16 @@ const hydrate = async (scope, parentNode, slot, index, previous) => {
302
306
  const Component = components.get(slot.tag);
303
307
  if (Component) registerComponent(Component);
304
308
  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) => {
309
+ for (const [name, value] of Object.entries(slot.attributes)) createCompute(scope, (scope$1) => {
306
310
  const target = hydration;
307
311
  if (name.startsWith("on")) {
308
312
  const event = name.substring(2).toLowerCase();
309
- useEvent(childScope, target, event, value);
313
+ useEvent(scope$1, target, event, value);
310
314
  } else {
311
315
  const set = toString(value);
312
316
  if (set === "" || set === "false") return target.removeAttribute(name);
313
317
  return target.setAttribute(name, set);
314
318
  }
315
- scope.onStop(() => childScope.stop());
316
319
  });
317
320
  for (const [index$1, childSlot] of slot.children.entries()) await hydrate(scope, hydration, childSlot, index$1, previous);
318
321
  }
@@ -342,7 +345,6 @@ const renderToString = async (scope, slot) => {
342
345
  let content = `<${prefix}>`;
343
346
  if (CustomElement) {
344
347
  const element = new CustomElement(slot.attributes, scope);
345
- scope.onStop(() => element.scope.stop());
346
348
  const template = await renderToString(element.scope, await element.setup());
347
349
  if (element.shadowRoot) {
348
350
  const shadow = {
@@ -393,15 +395,14 @@ const toCustomElement = (Component) => {
393
395
  internals = this.attachInternals();
394
396
  async connectedCallback() {
395
397
  let rootNode = this;
396
- const findParent = (node) => {
398
+ const findParentScope = (node) => {
397
399
  if (node) {
398
- if ("component" in node) return node;
399
- if (node instanceof ShadowRoot) return findParent(node.host);
400
- return findParent(node.parentNode);
400
+ if (isCustomElement(node)) return node.component.scope;
401
+ if (node instanceof ShadowRoot) return findParentScope(node.host);
402
+ return findParentScope(node.parentNode);
401
403
  }
402
404
  };
403
- const parentNode = findParent(this.parentNode);
404
- if (parentNode) this.component.scope.parentScope = parentNode.component.scope;
405
+ this.component.scope.setParentScope(findParentScope(this.parentNode));
405
406
  if (this.component.shadowRoot) rootNode = this.shadowRoot ?? this.attachShadow(this.component.shadowRoot);
406
407
  for (const [name, event] of Object.entries(Component.$events)) Reflect.set(this.component.events, name, (value) => {
407
408
  if (value instanceof Event) return;
@@ -515,6 +516,9 @@ var Radix = class Radix {
515
516
 
516
517
  //#endregion
517
518
  //#region src/runtime/index.ts
519
+ const isRoute = (value) => {
520
+ return !!value && typeof value === "object" && "fetch" in value;
521
+ };
518
522
  const useRuntime = (scope) => {
519
523
  return scope.getContext(RUNTIME_CONTEXT);
520
524
  };
@@ -566,8 +570,8 @@ const createRuntime = async () => {
566
570
  const [name, method] = toPath(path);
567
571
  radix.insert((method ?? "GET").toUpperCase() + name, defineRoute({ fetch: async (event) => {
568
572
  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)));
573
+ if (isRoute(route)) return route.fetch(event);
574
+ return sendHtml(event, await renderToString(event, await import("#virtual/client").then((module) => module.client)));
571
575
  } }));
572
576
  }
573
577
  const assets = await import("#virtual/assets").then((module) => module.assets);
@@ -800,10 +804,7 @@ const Page = defineComponent({
800
804
  name: "x-page",
801
805
  setup: ({ scope }) => {
802
806
  const { route } = useRouter(scope);
803
- return () => {
804
- const Page$1 = route.value;
805
- if (Page$1) return /* @__PURE__ */ h(Page$1, null);
806
- };
807
+ return () => route.value;
807
808
  }
808
809
  });
809
810
 
@@ -852,4 +853,4 @@ const useLocale = (scope, context) => {
852
853
  };
853
854
 
854
855
  //#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 };
856
+ 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, 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,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.58",
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" {