revojs 0.0.14 → 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 { Hooks, 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
  };
@@ -45,10 +45,13 @@ export interface ComponentOptions<TEvents extends Events, TAttributes extends At
45
45
  export interface Component<TEvents extends Events, TAttributes extends Attributes> {
46
46
  readonly scope: Scope;
47
47
  readonly hooks: Hooks;
48
+ readonly context: Map<string, unknown>;
48
49
  readonly events: EventOutput<TEvents>;
49
50
  readonly attributes: State<AttributeOutput<TAttributes>>;
50
51
  readonly shadowRoot: false | ShadowRootInit;
51
52
  readonly host?: CustomElement<TEvents, TAttributes>;
53
+ getContext: <T>(input: string | Descriptor<T>) => T;
54
+ setContext: <T>(input: string | Descriptor<T>, value: T) => void;
52
55
  onMounted: (invoke: (input: HTMLElement) => void | Promise<void>) => void;
53
56
  setup: () => Slot | Promise<Slot>;
54
57
  }
@@ -56,7 +59,7 @@ export interface ComponentConstructor<TEvents extends Events, TAttributes extend
56
59
  $name: string;
57
60
  $events: TEvents;
58
61
  $attributes: TAttributes;
59
- 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>;
60
63
  }
61
64
  export interface CustomElement<TEvents extends Events, TAttributes extends Attributes> extends HTMLElement {
62
65
  readonly component: Component<TEvents, TAttributes>;
@@ -80,5 +83,5 @@ export declare const isServer: () => boolean;
80
83
  export declare const preventDefault: (event: Event) => void;
81
84
  export declare const stopPropagation: (event: Event) => void;
82
85
  export declare const stopImmediatePropagation: (event: Event) => void;
83
- export declare const MOUNTED_HOOK: import("..").Descriptor<HTMLElement>;
86
+ export declare const MOUNTED_HOOK: Descriptor<HTMLElement>;
84
87
  export declare const components: Map<string, ComponentConstructor<Events, Attributes>>;
package/dist/index.js CHANGED
@@ -125,10 +125,8 @@ var Hooks = class {
125
125
  };
126
126
  };
127
127
  var Scope = class {
128
- context;
129
128
  dispose;
130
129
  constructor() {
131
- this.context = new Map();
132
130
  this.dispose = new Array();
133
131
  }
134
132
  stop() {
@@ -209,12 +207,6 @@ function defineHook(description) {
209
207
  function defineContext(description) {
210
208
  return { key: Symbol(description) };
211
209
  }
212
- function getContext(scope, input) {
213
- return scope.context.get(descriptor(input)) ?? {};
214
- }
215
- function setContext(scope, input, value) {
216
- scope.context.set(descriptor(input), value);
217
- }
218
210
  let activeCompute;
219
211
  const targets = new WeakMap();
220
212
 
@@ -261,8 +253,7 @@ const renderToString = async (slot, context) => {
261
253
  }, [slot.tag]).join(" ");
262
254
  const children = await renderToString(slot.children, context);
263
255
  if (customElement) {
264
- const element = new customElement(slot.attributes);
265
- for (const [input, value] of context) setContext(element.scope, input, value);
256
+ const element = new customElement(slot.attributes, context);
266
257
  const template = await renderToString(await element.setup(), context);
267
258
  if (element.shadowRoot) {
268
259
  const shadow = await renderToString({
@@ -355,13 +346,15 @@ const defineComponent = (options) => {
355
346
  static $attributes = options.attributes ?? {};
356
347
  scope;
357
348
  hooks;
349
+ context;
358
350
  events;
359
351
  attributes;
360
352
  shadowRoot;
361
353
  host;
362
- constructor(input, host) {
354
+ constructor(input, context, host) {
363
355
  this.scope = new Scope();
364
356
  this.hooks = new Hooks();
357
+ this.context = context ?? new Map();
365
358
  this.events = Object.keys(options.events ?? {}).reduce((output, name) => {
366
359
  Reflect.set(output, name, input?.[name], output);
367
360
  return output;
@@ -373,6 +366,12 @@ const defineComponent = (options) => {
373
366
  this.shadowRoot = options.shadowRoot ?? { mode: "open" };
374
367
  this.host = host;
375
368
  }
369
+ getContext = (input) => {
370
+ return this.context.get(descriptor(input)) ?? {};
371
+ };
372
+ setContext = (input, value) => {
373
+ this.context.set(descriptor(input), value);
374
+ };
376
375
  onMounted = (invoke) => {
377
376
  return this.hooks.on(this.scope, MOUNTED_HOOK, invoke);
378
377
  };
@@ -385,7 +384,7 @@ const toCustomElement = (component) => {
385
384
  component;
386
385
  constructor() {
387
386
  super();
388
- this.component = new component(void 0, this);
387
+ this.component = new component(void 0, void 0, this);
389
388
  }
390
389
  async connectedCallback() {
391
390
  const shadow = this.component.shadowRoot ? this.attachShadow(this.component.shadowRoot) : this;
@@ -397,7 +396,7 @@ const toCustomElement = (component) => {
397
396
  detail: value
398
397
  }));
399
398
  }, this.component.events);
400
- if (parentNode) for (const [name, value] of parentNode.component.scope.context) setContext(this.component.scope, name, value);
399
+ if (parentNode) for (const [name, value] of parentNode.component.context) this.component.context.set(name, value);
401
400
  shadow.replaceChildren(await renderToNode(this.component.scope, await this.component.setup()));
402
401
  this.component.hooks.dispatch(MOUNTED_HOOK, this);
403
402
  }
@@ -753,8 +752,8 @@ const RUNTIME_CONTEXT = defineContext("RUNTIME_CONTEXT");
753
752
  const Outlet = defineComponent({
754
753
  name: "x-outlet",
755
754
  shadowRoot: false,
756
- setup: async ({ scope }) => {
757
- const { event } = getContext(scope, RUNTIME_CONTEXT);
755
+ setup: async ({ scope, getContext }) => {
756
+ const { event } = getContext(RUNTIME_CONTEXT);
758
757
  const radix = new Radix();
759
758
  const routes = await getRoutes();
760
759
  for (const path in routes) {
@@ -787,4 +786,4 @@ const anchorNavigate = (event) => {
787
786
  };
788
787
 
789
788
  //#endregion
790
- 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, getContext, getCookies, getCustomElement, getGlobalStyles, getMimeType, getRequestUrl, getRoutes, getSetCookies, getVariables, isClient, isServer, isTemplate, markdownToSlot, navigate, preventDefault, registerComponent, renderToNode, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setContext, 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 };
@@ -12,7 +12,6 @@ export declare class Hooks {
12
12
  dispatch: <T>(input: string | Descriptor<T>, value: T) => void;
13
13
  }
14
14
  export declare class Scope {
15
- readonly context: Map<string, unknown>;
16
15
  readonly dispose: Array<(scope: Scope) => void>;
17
16
  constructor();
18
17
  stop(): void;
@@ -35,7 +34,5 @@ export declare function fromValue<T>(value: Value<T>): T;
35
34
  export declare function descriptor<T>(descriptor: string | Descriptor<T>): string;
36
35
  export declare function defineHook<T>(description?: string | number): Descriptor<T>;
37
36
  export declare function defineContext<T>(description?: string | number): Descriptor<T>;
38
- export declare function getContext<T>(scope: Scope, input: string | Descriptor<T>): T;
39
- export declare function setContext<T>(scope: Scope, input: string | Descriptor<T>, value: T): void;
40
37
  export declare let activeCompute: Compute | undefined;
41
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.14",
3
+ "version": "0.0.15",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",