revojs 0.0.10 → 0.0.12

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.
@@ -1,7 +1,5 @@
1
- import { Hooks } from "../hooks";
2
1
  import { type HtmlAttributes } from "../jsx";
3
2
  import { Scope, type State, type Value } from "../signals";
4
- import { type Descriptor } from "../types";
5
3
  export type TypeOf<T> = {
6
4
  (): T;
7
5
  };
@@ -46,14 +44,10 @@ export interface ComponentOptions<TEvents extends Events, TAttributes extends At
46
44
  }
47
45
  export interface Component<TEvents extends Events, TAttributes extends Attributes> {
48
46
  readonly scope: Scope;
49
- readonly hooks: Hooks;
50
47
  readonly events: EventOutput<TEvents>;
51
48
  readonly attributes: State<AttributeOutput<TAttributes>>;
52
49
  readonly shadowRoot: false | ShadowRootInit;
53
- readonly context: Record<string, unknown>;
54
50
  readonly host?: CustomElement<TEvents, TAttributes>;
55
- getContext: <T>(input: string | Descriptor<T>) => T;
56
- setContext: <T>(input: string | Descriptor<T>, value: T) => void;
57
51
  onMounted: (invoke: (input: HTMLElement) => void | Promise<void>) => void;
58
52
  setup: () => Slot | Promise<Slot>;
59
53
  }
@@ -61,7 +55,7 @@ export interface ComponentConstructor<TEvents extends Events, TAttributes extend
61
55
  $name: string;
62
56
  $events: TEvents;
63
57
  $attributes: TAttributes;
64
- new (input?: Input<TEvents, TAttributes>, context?: Record<string, unknown>, host?: CustomElement<TEvents, TAttributes>): Component<TEvents, TAttributes>;
58
+ new (input?: Input<TEvents, TAttributes>, host?: CustomElement<TEvents, TAttributes>): Component<TEvents, TAttributes>;
65
59
  }
66
60
  export interface CustomElement<TEvents extends Events, TAttributes extends Attributes> extends HTMLElement {
67
61
  readonly component: Component<TEvents, TAttributes>;
@@ -70,11 +64,10 @@ export interface CustomElementConstructor<TEvents extends Events, TAttributes ex
70
64
  new (): CustomElement<TEvents, TAttributes>;
71
65
  }
72
66
  export declare const isTemplate: (value: object) => value is Template;
73
- export declare const defineContext: <T>(description?: string | number) => Descriptor<T>;
74
67
  export declare const createElement: <TEvents extends Events, TAttributes extends Attributes>(input: string | ComponentConstructor<TEvents, TAttributes>, attributes?: AttributeInput<TAttributes>, ...children: Array<Slot>) => Slot;
75
68
  export declare const toString: (slot: Slot) => string;
76
69
  export declare const toFragment: (nodes: Array<Node>) => DocumentFragment;
77
- export declare const renderToString: (slot: Slot, context: Record<string, unknown>) => Promise<string>;
70
+ export declare const renderToString: (slot: Slot, context: Map<string, unknown>) => Promise<string>;
78
71
  export declare const renderToNode: (scope: Scope, slot: Slot) => Promise<Node>;
79
72
  export declare const defineComponent: <TEvents extends Events = {}, TAttributes extends Attributes = {}>(options: ComponentOptions<TEvents, TAttributes>) => ComponentConstructor<TEvents, TAttributes>;
80
73
  export declare const toCustomElement: <TEvents extends Events = {}, TAttributes extends Attributes = {}>(component: ComponentConstructor<TEvents, TAttributes>) => CustomElementConstructor<TEvents, TAttributes>;
@@ -86,5 +79,5 @@ export declare const isServer: () => boolean;
86
79
  export declare const preventDefault: (event: Event) => void;
87
80
  export declare const stopPropagation: (event: Event) => void;
88
81
  export declare const stopImmediatePropagation: (event: Event) => void;
89
- export declare const MOUNTED_HOOK: Descriptor<HTMLElement>;
82
+ export declare const MOUNTED_HOOK: import("..").Descriptor<HTMLElement>;
90
83
  export declare const components: Map<string, ComponentConstructor<Events, Attributes>>;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  export * from "./app";
2
- export * from "./hooks";
3
2
  export * from "./html";
4
3
  export * from "./http";
5
4
  export * from "./markdown";
@@ -7,4 +6,3 @@ export * from "./radix";
7
6
  export * from "./router";
8
7
  export * from "./runtime";
9
8
  export * from "./signals";
10
- export * from "./types";
package/dist/index.js CHANGED
@@ -20,35 +20,6 @@ const createApp = (config) => {
20
20
  };
21
21
  };
22
22
 
23
- //#endregion
24
- //#region src/types/index.ts
25
- const descriptor = (descriptor$1) => {
26
- return (typeof descriptor$1 === "object" ? descriptor$1.key : descriptor$1).toString();
27
- };
28
-
29
- //#endregion
30
- //#region src/hooks/index.ts
31
- var Hooks = class {
32
- hooks;
33
- constructor() {
34
- this.hooks = new Map();
35
- }
36
- on = (scope, input, invoke) => {
37
- const key = descriptor(input);
38
- const invokes = this.hooks.get(key) ?? new Set();
39
- invokes.add(invoke);
40
- this.hooks.set(key, invokes);
41
- return scope.dispose.push(() => invokes.delete(invoke));
42
- };
43
- dispatch = (input, value) => {
44
- const invokes = this.hooks.get(descriptor(input));
45
- if (invokes) for (const invoke of invokes) invoke(value);
46
- };
47
- };
48
- const defineHook = (description) => {
49
- return { key: Symbol(description) };
50
- };
51
-
52
23
  //#endregion
53
24
  //#region src/jsx/index.ts
54
25
  const svgElements = new Set([
@@ -137,10 +108,31 @@ const namespace = (tag) => {
137
108
  //#endregion
138
109
  //#region src/signals/index.ts
139
110
  var Scope = class {
111
+ hooks;
112
+ context;
140
113
  dispose;
141
114
  constructor() {
115
+ this.hooks = new Map();
116
+ this.context = new Map();
142
117
  this.dispose = new Array();
143
118
  }
119
+ on = (input, invoke) => {
120
+ const key = descriptor(input);
121
+ const invokes = this.hooks.get(key) ?? new Set();
122
+ invokes.add(invoke);
123
+ this.hooks.set(key, invokes);
124
+ return this.dispose.push(() => invokes.delete(invoke));
125
+ };
126
+ dispatch = (input, value) => {
127
+ const invokes = this.hooks.get(descriptor(input));
128
+ if (invokes) for (const invoke of invokes) invoke(value);
129
+ };
130
+ getContext = (input) => {
131
+ return this.context.get(descriptor(input)) ?? {};
132
+ };
133
+ setContext = (input, value) => {
134
+ this.context.set(descriptor(input), value);
135
+ };
144
136
  stop() {
145
137
  while (this.dispose.length) this.dispose.pop()?.(this);
146
138
  }
@@ -210,6 +202,15 @@ function fromValue(value) {
210
202
  if (value instanceof Function) return fromValue(value());
211
203
  return value;
212
204
  }
205
+ function descriptor(descriptor$1) {
206
+ return (typeof descriptor$1 === "object" ? descriptor$1.key : descriptor$1).toString();
207
+ }
208
+ function defineHook(description) {
209
+ return { key: Symbol(description) };
210
+ }
211
+ function defineContext(description) {
212
+ return { key: Symbol(description) };
213
+ }
213
214
  let activeCompute;
214
215
  const targets = new WeakMap();
215
216
 
@@ -218,9 +219,6 @@ const targets = new WeakMap();
218
219
  const isTemplate = (value) => {
219
220
  return "tag" in value && "attributes" in value && "children" in value;
220
221
  };
221
- const defineContext = (description) => {
222
- return { key: Symbol(description) };
223
- };
224
222
  const createElement = (input, attributes, ...children) => {
225
223
  return {
226
224
  tag: typeof input === "function" ? input.$name : input,
@@ -259,7 +257,8 @@ const renderToString = async (slot, context) => {
259
257
  }, [slot.tag]).join(" ");
260
258
  const children = await renderToString(slot.children, context);
261
259
  if (customElement) {
262
- const element = new customElement(slot.attributes, context);
260
+ const element = new customElement(slot.attributes);
261
+ for (const [input, value] of context) element.scope.setContext(input, value);
263
262
  const template = await renderToString(await element.setup(), context);
264
263
  if (element.shadowRoot) {
265
264
  const shadow = await renderToString({
@@ -351,15 +350,12 @@ const defineComponent = (options) => {
351
350
  static $events = options.events ?? {};
352
351
  static $attributes = options.attributes ?? {};
353
352
  scope;
354
- hooks;
355
353
  events;
356
354
  attributes;
357
355
  shadowRoot;
358
- context;
359
356
  host;
360
- constructor(input, context, host) {
357
+ constructor(input, host) {
361
358
  this.scope = new Scope();
362
- this.hooks = new Hooks();
363
359
  this.events = Object.keys(options.events ?? {}).reduce((output, name) => {
364
360
  Reflect.set(output, name, input?.[name], output);
365
361
  return output;
@@ -369,17 +365,10 @@ const defineComponent = (options) => {
369
365
  return attributes;
370
366
  }, createState({}));
371
367
  this.shadowRoot = options.shadowRoot ?? { mode: "open" };
372
- this.context = context ?? {};
373
368
  this.host = host;
374
369
  }
375
- getContext = (input) => {
376
- return this.context[descriptor(input)] ?? {};
377
- };
378
- setContext = (input, value) => {
379
- this.context[descriptor(input)] = value;
380
- };
381
370
  onMounted = (invoke) => {
382
- return this.hooks.on(this.scope, MOUNTED_HOOK, invoke);
371
+ return this.scope.on(MOUNTED_HOOK, invoke);
383
372
  };
384
373
  setup = () => options.setup(this);
385
374
  });
@@ -390,11 +379,12 @@ const toCustomElement = (component) => {
390
379
  component;
391
380
  constructor() {
392
381
  super();
393
- this.component = new component(void 0, void 0, this);
382
+ this.component = new component(void 0, this);
394
383
  }
395
384
  async connectedCallback() {
396
385
  const shadow = this.component.shadowRoot ? this.attachShadow(this.component.shadowRoot) : this;
397
386
  const parentNode = getCustomElement(this.parentNode);
387
+ for (const [name, set] of parentNode?.component.scope.hooks ?? []) for (const invoke of set) this.component.scope.on(name, invoke);
398
388
  for (const [name, event] of Object.entries(component.$events)) Reflect.set(this.component.events, name, (value) => {
399
389
  if (value instanceof Event) return;
400
390
  this.dispatchEvent(new CustomEvent(name.substring(2).toLowerCase(), {
@@ -402,9 +392,9 @@ const toCustomElement = (component) => {
402
392
  detail: value
403
393
  }));
404
394
  }, this.component.events);
405
- if (parentNode) for (const key in parentNode.component.context) this.component.setContext(key, parentNode.component.context[key]);
395
+ if (parentNode) for (const key in parentNode.component.scope.context) this.component.scope.setContext(key, parentNode.component.scope.context.get(key));
406
396
  shadow.replaceChildren(await renderToNode(this.component.scope, await this.component.setup()));
407
- this.component.hooks.dispatch(MOUNTED_HOOK, this);
397
+ this.component.scope.dispatch(MOUNTED_HOOK, this);
408
398
  }
409
399
  attributeChangedCallback(name, oldValue, value) {
410
400
  if (value === oldValue) return;
@@ -714,7 +704,9 @@ const createRuntime = async () => {
714
704
  if (typeof route === "object") return route.fetch(event);
715
705
  if (route) {
716
706
  const slot = await import("#virtual/client").then((module) => module.index);
717
- return sendHtml(event, await renderToString(slot, { [descriptor(RUNTIME_CONTEXT)]: { event } }));
707
+ const context = new Map();
708
+ context.set(descriptor(RUNTIME_CONTEXT), { event });
709
+ return sendHtml(event, await renderToString(slot, context));
718
710
  }
719
711
  } }));
720
712
  }
@@ -756,8 +748,8 @@ const RUNTIME_CONTEXT = defineContext("RUNTIME_CONTEXT");
756
748
  const Outlet = defineComponent({
757
749
  name: "x-outlet",
758
750
  shadowRoot: false,
759
- setup: async ({ scope, getContext }) => {
760
- const { event } = getContext(RUNTIME_CONTEXT);
751
+ setup: async ({ scope }) => {
752
+ const { event } = scope.getContext(RUNTIME_CONTEXT);
761
753
  const radix = new Radix();
762
754
  const routes = await getRoutes();
763
755
  for (const path in routes) {
@@ -790,4 +782,4 @@ const anchorNavigate = (event) => {
790
782
  };
791
783
 
792
784
  //#endregion
793
- export { $fetch, Compute, Handler, Hooks, MOUNTED_HOOK, Outlet, RUNTIME_CONTEXT, Radix, Scope, activeCompute, anchorNavigate, components, createApp, createCompute, createElement, createEvent, createMemo, createRuntime, createState, defineComponent, defineContext, defineHook, defineRoute, descriptor, fileName, fromValue, getAssets, getCookies, getCustomElement, getGlobalStyles, getMimeType, getRequestUrl, getRoutes, getSetCookies, getVariables, isClient, isServer, isTemplate, markdownToSlot, navigate, preventDefault, registerComponent, renderToNode, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, stopImmediatePropagation, stopPropagation, targets, toCustomElement, toFragment, toPath, toString };
785
+ export { $fetch, Compute, Handler, MOUNTED_HOOK, Outlet, RUNTIME_CONTEXT, Radix, Scope, activeCompute, anchorNavigate, components, createApp, createCompute, createElement, createEvent, createMemo, createRuntime, createState, defineComponent, defineContext, defineHook, defineRoute, descriptor, fileName, fromValue, getAssets, getCookies, getCustomElement, getGlobalStyles, getMimeType, getRequestUrl, getRoutes, getSetCookies, getVariables, isClient, isServer, isTemplate, markdownToSlot, navigate, preventDefault, registerComponent, renderToNode, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, stopImmediatePropagation, stopPropagation, targets, toCustomElement, toFragment, toPath, toString };
package/dist/jsx/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
 
2
- //#region src/hooks/index.ts
3
- const defineHook = (description) => {
2
+ //#region src/signals/index.ts
3
+ function defineHook(description) {
4
4
  return { key: Symbol(description) };
5
- };
5
+ }
6
6
 
7
7
  //#endregion
8
8
  //#region src/html/index.ts
@@ -2,9 +2,18 @@ export type Value<T> = T | (() => Value<T>);
2
2
  export interface State<T> {
3
3
  value: T;
4
4
  }
5
+ export type Descriptor<T> = {
6
+ key: symbol;
7
+ };
5
8
  export declare class Scope {
9
+ readonly hooks: Map<string, Set<(value: any) => unknown>>;
10
+ readonly context: Map<string, unknown>;
6
11
  readonly dispose: Array<(scope: Scope) => void>;
7
12
  constructor();
13
+ on: <T>(input: string | Descriptor<T>, invoke: (value: T) => unknown) => number;
14
+ dispatch: <T>(input: string | Descriptor<T>, value: T) => void;
15
+ getContext: <T>(input: string | Descriptor<T>) => T;
16
+ setContext: <T>(input: string | Descriptor<T>, value: T) => void;
8
17
  stop(): void;
9
18
  }
10
19
  export declare class Compute<T = void> extends Scope {
@@ -22,5 +31,8 @@ export declare function createState<T>(value: T): State<T>;
22
31
  export declare function createCompute<T>(scope: Scope, invoke: (scope: Scope) => T): T;
23
32
  export declare function createMemo<T>(scope: Scope, invoke: () => T): State<T>;
24
33
  export declare function fromValue<T>(value: Value<T>): T;
34
+ export declare function descriptor<T>(descriptor: string | Descriptor<T>): string;
35
+ export declare function defineHook<T>(description?: string | number): Descriptor<T>;
36
+ export declare function defineContext<T>(description?: string | number): Descriptor<T>;
25
37
  export declare let activeCompute: Compute | undefined;
26
38
  export declare const targets: WeakMap<object, Map<string | symbol, Set<Compute<void>>>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",
@@ -1,9 +0,0 @@
1
- import { Scope } from "../signals";
2
- import { type Descriptor } from "../types";
3
- export declare class Hooks {
4
- readonly hooks: Map<string, Set<(value: any) => unknown>>;
5
- constructor();
6
- on: <T>(scope: Scope, input: string | Descriptor<T>, invoke: (value: T) => unknown) => number;
7
- dispatch: <T>(input: string | Descriptor<T>, value: T) => void;
8
- }
9
- export declare const defineHook: <T>(description?: string | number) => Descriptor<T>;
@@ -1,4 +0,0 @@
1
- export type Descriptor<T> = {
2
- key: symbol;
3
- };
4
- export declare const descriptor: <T>(descriptor: string | Descriptor<T>) => string;