revojs 0.0.13 → 0.0.15

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,5 +1,5 @@
1
1
  import { type HtmlAttributes } from "../jsx";
2
- import { Scope, type State, type Value } from "../signals";
2
+ import { type Descriptor, Hooks, Scope, type State, type Value } from "../signals";
3
3
  export type TypeOf<T> = {
4
4
  (): T;
5
5
  };
@@ -44,10 +44,14 @@ export interface ComponentOptions<TEvents extends Events, TAttributes extends At
44
44
  }
45
45
  export interface Component<TEvents extends Events, TAttributes extends Attributes> {
46
46
  readonly scope: Scope;
47
+ readonly hooks: Hooks;
48
+ readonly context: Map<string, unknown>;
47
49
  readonly events: EventOutput<TEvents>;
48
50
  readonly attributes: State<AttributeOutput<TAttributes>>;
49
51
  readonly shadowRoot: false | ShadowRootInit;
50
52
  readonly host?: CustomElement<TEvents, TAttributes>;
53
+ getContext: <T>(input: string | Descriptor<T>) => T;
54
+ setContext: <T>(input: string | Descriptor<T>, value: T) => void;
51
55
  onMounted: (invoke: (input: HTMLElement) => void | Promise<void>) => void;
52
56
  setup: () => Slot | Promise<Slot>;
53
57
  }
@@ -55,7 +59,7 @@ export interface ComponentConstructor<TEvents extends Events, TAttributes extend
55
59
  $name: string;
56
60
  $events: TEvents;
57
61
  $attributes: TAttributes;
58
- new (input?: Input<TEvents, TAttributes>, host?: CustomElement<TEvents, TAttributes>): Component<TEvents, TAttributes>;
62
+ new (input?: Input<TEvents, TAttributes>, context?: Map<string, unknown>, host?: CustomElement<TEvents, TAttributes>): Component<TEvents, TAttributes>;
59
63
  }
60
64
  export interface CustomElement<TEvents extends Events, TAttributes extends Attributes> extends HTMLElement {
61
65
  readonly component: Component<TEvents, TAttributes>;
@@ -79,5 +83,5 @@ export declare const isServer: () => boolean;
79
83
  export declare const preventDefault: (event: Event) => void;
80
84
  export declare const stopPropagation: (event: Event) => void;
81
85
  export declare const stopImmediatePropagation: (event: Event) => void;
82
- export declare const MOUNTED_HOOK: import("..").Descriptor<HTMLElement>;
86
+ export declare const MOUNTED_HOOK: Descriptor<HTMLElement>;
83
87
  export declare const components: Map<string, ComponentConstructor<Events, Attributes>>;
package/dist/index.js CHANGED
@@ -107,32 +107,28 @@ const namespace = (tag) => {
107
107
 
108
108
  //#endregion
109
109
  //#region src/signals/index.ts
110
- var Scope = class {
110
+ var Hooks = class {
111
111
  hooks;
112
- context;
113
- dispose;
114
112
  constructor() {
115
113
  this.hooks = new Map();
116
- this.context = new Map();
117
- this.dispose = new Array();
118
114
  }
119
- on = (input, invoke) => {
115
+ on = (scope, input, invoke) => {
120
116
  const key = descriptor(input);
121
117
  const invokes = this.hooks.get(key) ?? new Set();
122
118
  invokes.add(invoke);
123
119
  this.hooks.set(key, invokes);
124
- return this.dispose.push(() => invokes.delete(invoke));
120
+ return scope.dispose.push(() => invokes.delete(invoke));
125
121
  };
126
122
  dispatch = (input, value) => {
127
123
  const invokes = this.hooks.get(descriptor(input));
128
124
  if (invokes) for (const invoke of invokes) invoke(value);
129
125
  };
130
- getContext = (input) => {
131
- return this.context.get(descriptor(input)) ?? {};
132
- };
133
- setContext = (input, value) => {
134
- this.context.set(descriptor(input), value);
135
- };
126
+ };
127
+ var Scope = class {
128
+ dispose;
129
+ constructor() {
130
+ this.dispose = new Array();
131
+ }
136
132
  stop() {
137
133
  while (this.dispose.length) this.dispose.pop()?.(this);
138
134
  }
@@ -257,8 +253,7 @@ const renderToString = async (slot, context) => {
257
253
  }, [slot.tag]).join(" ");
258
254
  const children = await renderToString(slot.children, context);
259
255
  if (customElement) {
260
- const element = new customElement(slot.attributes);
261
- for (const [input, value] of context) element.scope.setContext(input, value);
256
+ const element = new customElement(slot.attributes, context);
262
257
  const template = await renderToString(await element.setup(), context);
263
258
  if (element.shadowRoot) {
264
259
  const shadow = await renderToString({
@@ -350,12 +345,16 @@ const defineComponent = (options) => {
350
345
  static $events = options.events ?? {};
351
346
  static $attributes = options.attributes ?? {};
352
347
  scope;
348
+ hooks;
349
+ context;
353
350
  events;
354
351
  attributes;
355
352
  shadowRoot;
356
353
  host;
357
- constructor(input, host) {
354
+ constructor(input, context, host) {
358
355
  this.scope = new Scope();
356
+ this.hooks = new Hooks();
357
+ this.context = context ?? new Map();
359
358
  this.events = Object.keys(options.events ?? {}).reduce((output, name) => {
360
359
  Reflect.set(output, name, input?.[name], output);
361
360
  return output;
@@ -367,8 +366,14 @@ const defineComponent = (options) => {
367
366
  this.shadowRoot = options.shadowRoot ?? { mode: "open" };
368
367
  this.host = host;
369
368
  }
369
+ getContext = (input) => {
370
+ return this.context.get(descriptor(input)) ?? {};
371
+ };
372
+ setContext = (input, value) => {
373
+ this.context.set(descriptor(input), value);
374
+ };
370
375
  onMounted = (invoke) => {
371
- return this.scope.on(MOUNTED_HOOK, invoke);
376
+ return this.hooks.on(this.scope, MOUNTED_HOOK, invoke);
372
377
  };
373
378
  setup = () => options.setup(this);
374
379
  });
@@ -379,13 +384,11 @@ const toCustomElement = (component) => {
379
384
  component;
380
385
  constructor() {
381
386
  super();
382
- this.component = new component(void 0, this);
387
+ this.component = new component(void 0, void 0, this);
383
388
  }
384
389
  async connectedCallback() {
385
390
  const shadow = this.component.shadowRoot ? this.attachShadow(this.component.shadowRoot) : this;
386
391
  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);
388
- for (const [name, value] of parentNode?.component.scope.context ?? []) this.component.scope.setContext(name, value);
389
392
  for (const [name, event] of Object.entries(component.$events)) Reflect.set(this.component.events, name, (value) => {
390
393
  if (value instanceof Event) return;
391
394
  this.dispatchEvent(new CustomEvent(name.substring(2).toLowerCase(), {
@@ -393,9 +396,9 @@ const toCustomElement = (component) => {
393
396
  detail: value
394
397
  }));
395
398
  }, this.component.events);
396
- if (parentNode) for (const key in parentNode.component.scope.context) this.component.scope.setContext(key, parentNode.component.scope.context.get(key));
399
+ if (parentNode) for (const [name, value] of parentNode.component.context) this.component.context.set(name, value);
397
400
  shadow.replaceChildren(await renderToNode(this.component.scope, await this.component.setup()));
398
- this.component.scope.dispatch(MOUNTED_HOOK, this);
401
+ this.component.hooks.dispatch(MOUNTED_HOOK, this);
399
402
  }
400
403
  attributeChangedCallback(name, oldValue, value) {
401
404
  if (value === oldValue) return;
@@ -749,8 +752,8 @@ const RUNTIME_CONTEXT = defineContext("RUNTIME_CONTEXT");
749
752
  const Outlet = defineComponent({
750
753
  name: "x-outlet",
751
754
  shadowRoot: false,
752
- setup: async ({ scope }) => {
753
- const { event } = scope.getContext(RUNTIME_CONTEXT);
755
+ setup: async ({ scope, getContext }) => {
756
+ const { event } = getContext(RUNTIME_CONTEXT);
754
757
  const radix = new Radix();
755
758
  const routes = await getRoutes();
756
759
  for (const path in routes) {
@@ -783,4 +786,4 @@ const anchorNavigate = (event) => {
783
786
  };
784
787
 
785
788
  //#endregion
786
- 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 };
789
+ 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 };
@@ -5,15 +5,15 @@ export interface State<T> {
5
5
  export type Descriptor<T> = {
6
6
  key: symbol;
7
7
  };
8
- export declare class Scope {
8
+ export declare class Hooks {
9
9
  readonly hooks: Map<string, Set<(value: any) => unknown>>;
10
- readonly context: Map<string, unknown>;
11
- readonly dispose: Array<(scope: Scope) => void>;
12
10
  constructor();
13
- on: <T>(input: string | Descriptor<T>, invoke: (value: T) => unknown) => number;
11
+ on: <T>(scope: Scope, input: string | Descriptor<T>, invoke: (value: T) => unknown) => number;
14
12
  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;
13
+ }
14
+ export declare class Scope {
15
+ readonly dispose: Array<(scope: Scope) => void>;
16
+ constructor();
17
17
  stop(): void;
18
18
  }
19
19
  export declare class Compute<T = void> extends Scope {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "revojs",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",