revojs 0.0.56 → 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;
@@ -86,7 +87,7 @@ export declare const toCustomElement: <TEvents extends Events, TAttributes exten
86
87
  export declare const registerComponent: <TEvents extends Events, TAttributes extends Attributes>(component: ComponentConstructor<TEvents, TAttributes>) => ComponentConstructor<TEvents, TAttributes>;
87
88
  export declare function useEvent<T extends keyof ElementEventMap>(scope: Scope, target: EventTarget | undefined | null, event: T, input: EventListener<ElementEventMap[T]>, options?: AddEventListenerOptions): void;
88
89
  export declare function useEvent<T extends keyof WindowEventMap>(scope: Scope, target: Window | undefined | null, event: T, input: EventListener<WindowEventMap[T]>, options?: AddEventListenerOptions): void;
89
- export declare function useEvent<T extends keyof HTMLElementEventMap>(scope: Scope, target: HTMLElement | undefined | null, event: T, input: EventListener<HTMLElementEventMap[T]>, options?: AddEventListenerOptions): void;
90
+ export declare function useEvent<T extends keyof HTMLElementEventMap>(scope: Scope, target: Document | HTMLElement | undefined | null, event: T, input: EventListener<HTMLElementEventMap[T]>, options?: AddEventListenerOptions): void;
90
91
  export declare const isClient: () => boolean;
91
92
  export declare const isServer: () => boolean;
92
93
  export declare const preventDefault: (event: Event) => void;
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,15 +160,15 @@ 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) {
166
167
  if (typeof value === "function" && !value.prototype) return value.bind(target);
167
- if (typeof value === "object") return new Proxy(value, new Handler());
168
+ if (typeof value === "object") {
169
+ const tag = Object.prototype.toString.call(value);
170
+ if (tag === "[object Object]" || tag === "[object Array]" || tag === "[object Map]" || tag === "[object Set]" || tag === "[object WeakMap]" || tag === "[object WeakSet]") return new Proxy(value, new Handler());
171
+ }
168
172
  }
169
173
  return value;
170
174
  }
@@ -214,7 +218,10 @@ var MountedEvent = class extends Event {
214
218
  }
215
219
  };
216
220
  const isTemplate = (value) => {
217
- 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;
218
225
  };
219
226
  const useHost = (scope) => {
220
227
  return scope.getContext(HOST_CONTEXT);
@@ -299,17 +306,16 @@ const hydrate = async (scope, parentNode, slot, index, previous) => {
299
306
  const Component = components.get(slot.tag);
300
307
  if (Component) registerComponent(Component);
301
308
  if (previous || !(hydration instanceof Element && hydration.tagName.toUpperCase() === slot.tag.toUpperCase())) hydration = document.createElementNS(namespace(slot.tag), slot.tag);
302
- 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) => {
303
310
  const target = hydration;
304
311
  if (name.startsWith("on")) {
305
312
  const event = name.substring(2).toLowerCase();
306
- useEvent(childScope, target, event, value);
313
+ useEvent(scope$1, target, event, value);
307
314
  } else {
308
315
  const set = toString(value);
309
316
  if (set === "" || set === "false") return target.removeAttribute(name);
310
317
  return target.setAttribute(name, set);
311
318
  }
312
- scope.onStop(() => childScope.stop());
313
319
  });
314
320
  for (const [index$1, childSlot] of slot.children.entries()) await hydrate(scope, hydration, childSlot, index$1, previous);
315
321
  }
@@ -339,7 +345,6 @@ const renderToString = async (scope, slot) => {
339
345
  let content = `<${prefix}>`;
340
346
  if (CustomElement) {
341
347
  const element = new CustomElement(slot.attributes, scope);
342
- scope.onStop(() => element.scope.stop());
343
348
  const template = await renderToString(element.scope, await element.setup());
344
349
  if (element.shadowRoot) {
345
350
  const shadow = {
@@ -390,15 +395,14 @@ const toCustomElement = (Component) => {
390
395
  internals = this.attachInternals();
391
396
  async connectedCallback() {
392
397
  let rootNode = this;
393
- const findParent = (node) => {
398
+ const findParentScope = (node) => {
394
399
  if (node) {
395
- if ("component" in node) return node;
396
- if (node instanceof ShadowRoot) return findParent(node.host);
397
- 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);
398
403
  }
399
404
  };
400
- const parentNode = findParent(this.parentNode);
401
- if (parentNode) this.component.scope.parentScope = parentNode.component.scope;
405
+ this.component.scope.setParentScope(findParentScope(this.parentNode));
402
406
  if (this.component.shadowRoot) rootNode = this.shadowRoot ?? this.attachShadow(this.component.shadowRoot);
403
407
  for (const [name, event] of Object.entries(Component.$events)) Reflect.set(this.component.events, name, (value) => {
404
408
  if (value instanceof Event) return;
@@ -512,6 +516,9 @@ var Radix = class Radix {
512
516
 
513
517
  //#endregion
514
518
  //#region src/runtime/index.ts
519
+ const isRoute = (value) => {
520
+ return !!value && typeof value === "object" && "fetch" in value;
521
+ };
515
522
  const useRuntime = (scope) => {
516
523
  return scope.getContext(RUNTIME_CONTEXT);
517
524
  };
@@ -563,8 +570,8 @@ const createRuntime = async () => {
563
570
  const [name, method] = toPath(path);
564
571
  radix.insert((method ?? "GET").toUpperCase() + name, defineRoute({ fetch: async (event) => {
565
572
  const route = await routes[path]?.();
566
- if (typeof route === "object") return route.fetch(event);
567
- 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)));
568
575
  } }));
569
576
  }
570
577
  const assets = await import("#virtual/assets").then((module) => module.assets);
@@ -797,10 +804,7 @@ const Page = defineComponent({
797
804
  name: "x-page",
798
805
  setup: ({ scope }) => {
799
806
  const { route } = useRouter(scope);
800
- return () => {
801
- const Page$1 = route.value;
802
- if (Page$1) return /* @__PURE__ */ h(Page$1, null);
803
- };
807
+ return () => route.value;
804
808
  }
805
809
  });
806
810
 
@@ -849,4 +853,4 @@ const useLocale = (scope, context) => {
849
853
  };
850
854
 
851
855
  //#endregion
852
- 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.56",
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" {