revojs 0.0.66 → 0.0.68

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.
@@ -76,7 +76,7 @@ export declare const isTemplate: <T>(value?: T) => value is Template & T;
76
76
  export declare const isCustomElement: <T>(value?: T) => value is CustomElement<Events, Attributes> & T;
77
77
  export declare const isComponent: <T>(value?: T) => value is ComponentConstructor<Events, Attributes> & T;
78
78
  export declare const useHost: (scope: Scope) => HostContext;
79
- export declare const createElement: <TEvents extends Events, TAttributes extends Attributes>(input: string | ComponentConstructor<TEvents, TAttributes>, attributes?: AttributeInput<TAttributes>, ...children: Array<Slot>) => Slot;
79
+ export declare const createElement: <TEvents extends Events, TAttributes extends Attributes>(input: string | ((attributes: AttributeInput<Attributes>) => Slot) | ComponentConstructor<TEvents, TAttributes>, attributes?: AttributeInput<TAttributes>, ...children: Array<Slot>) => Slot;
80
80
  export declare const toString: (slot: Slot) => string;
81
81
  export declare const toArray: (hydration: Hydration) => Array<Node>;
82
82
  export declare const toRange: (hydration: Hydration) => Range;
package/dist/index.js CHANGED
@@ -1,9 +1,134 @@
1
- import { defu } from "defu";
1
+ //#region src/signals/index.ts
2
+ var StopEvent = class extends Event {
3
+ constructor() {
4
+ super("stop");
5
+ }
6
+ };
7
+ var Scope = class extends EventTarget {
8
+ parentScope;
9
+ context;
10
+ constructor(parentScope) {
11
+ super();
12
+ this.parentScope = parentScope;
13
+ this.parentScope?.onStop(() => this.stop());
14
+ this.context = /* @__PURE__ */ new Map();
15
+ }
16
+ getContext(input) {
17
+ let scope = this;
18
+ while (scope) {
19
+ if (scope.context.has(input)) return scope.context.get(input);
20
+ scope = scope.parentScope;
21
+ }
22
+ return {};
23
+ }
24
+ setContext(input, value) {
25
+ this.context.set(input, value);
26
+ }
27
+ onStop(input) {
28
+ this.addEventListener("stop", input, { once: true });
29
+ }
30
+ stop() {
31
+ return this.dispatchEvent(new StopEvent());
32
+ }
33
+ };
34
+ var Compute = class extends Scope {
35
+ invoke;
36
+ constructor(parentScope, invoke) {
37
+ super(parentScope);
38
+ this.invoke = invoke;
39
+ }
40
+ run() {
41
+ this.stop();
42
+ return this.invoke(this);
43
+ }
44
+ };
45
+ var Handler = class Handler {
46
+ get(target, key) {
47
+ const compute = activeCompute;
48
+ if (compute) {
49
+ const computes = targets.get(target) ?? /* @__PURE__ */ new Map();
50
+ const set = computes.get(key) ?? /* @__PURE__ */ new Set();
51
+ computes.set(key, set.add(compute));
52
+ targets.set(target, computes);
53
+ compute.parentScope?.onStop(() => {
54
+ set.delete(compute);
55
+ if (set.size === 0) {
56
+ computes.delete(key);
57
+ if (computes.size === 0) targets.delete(target);
58
+ }
59
+ });
60
+ }
61
+ const value = Reflect.get(target, key);
62
+ if (value) {
63
+ if (typeof value === "function" && !value.prototype) return value.bind(target);
64
+ if (typeof value === "object") {
65
+ const tag = Object.prototype.toString.call(value);
66
+ 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());
67
+ }
68
+ }
69
+ return value;
70
+ }
71
+ set(target, key, value) {
72
+ const result = Reflect.set(target, key, value);
73
+ for (const compute of targets.get(target)?.get(key) ?? []) compute.run();
74
+ return result;
75
+ }
76
+ };
77
+ function createState(value) {
78
+ return new Proxy({ value }, new Handler());
79
+ }
80
+ function createCompute(scope, invoke) {
81
+ let previous = activeCompute;
82
+ activeCompute = new Compute(scope, invoke);
83
+ const result = invoke(activeCompute);
84
+ if (result instanceof Promise) return result.finally(() => activeCompute = previous);
85
+ activeCompute = previous;
86
+ return result;
87
+ }
88
+ function createMemo(scope, invoke) {
89
+ let state;
90
+ const compute = createCompute(scope, (scope$1) => {
91
+ const value = invoke(scope$1);
92
+ if (typeof state === "object") state.value = value;
93
+ return value;
94
+ });
95
+ state = createState(compute);
96
+ return state;
97
+ }
98
+ function fromValue(value) {
99
+ if (value instanceof Function) return fromValue(value());
100
+ return value;
101
+ }
102
+ function untrack(invoke) {
103
+ let previous = activeCompute;
104
+ activeCompute = void 0;
105
+ const result = invoke();
106
+ if (result instanceof Promise) return result.finally(() => activeCompute = previous);
107
+ activeCompute = previous;
108
+ return result;
109
+ }
110
+ function mergeObjects(base, input) {
111
+ const object = structuredClone(input);
112
+ for (const key in base) {
113
+ if (key === "__proto__" || key === "constructor") continue;
114
+ const value = base[key];
115
+ if (value === null || value === void 0) continue;
116
+ if (typeof value === "object" && typeof object[key] === "object") object[key] = mergeObjects(value, object[key]);
117
+ else object[key] = value;
118
+ }
119
+ return object;
120
+ }
121
+ function defineContext(key) {
122
+ return key;
123
+ }
124
+ let activeCompute;
125
+ const targets = /* @__PURE__ */ new WeakMap();
2
126
 
127
+ //#endregion
3
128
  //#region src/app/index.ts
4
129
  const createApp = (config) => {
5
130
  return {
6
- config: defu(config, {
131
+ config: mergeObjects(config, {
7
132
  client: { entry: "./index.html" },
8
133
  server: { entry: "revojs/presets/node" },
9
134
  dev: { middleware: [] }
@@ -256,122 +381,6 @@ var Radix = class Radix {
256
381
  };
257
382
  };
258
383
 
259
- //#endregion
260
- //#region src/signals/index.ts
261
- var StopEvent = class extends Event {
262
- constructor() {
263
- super("stop");
264
- }
265
- };
266
- var Scope = class extends EventTarget {
267
- parentScope;
268
- context;
269
- constructor(parentScope) {
270
- super();
271
- this.parentScope = parentScope;
272
- this.parentScope?.onStop(() => this.stop());
273
- this.context = /* @__PURE__ */ new Map();
274
- }
275
- getContext(input) {
276
- let scope = this;
277
- while (scope) {
278
- if (scope.context.has(input)) return scope.context.get(input);
279
- scope = scope.parentScope;
280
- }
281
- return {};
282
- }
283
- setContext(input, value) {
284
- this.context.set(input, value);
285
- }
286
- onStop(input) {
287
- this.addEventListener("stop", input, { once: true });
288
- }
289
- stop() {
290
- return this.dispatchEvent(new StopEvent());
291
- }
292
- };
293
- var Compute = class extends Scope {
294
- invoke;
295
- constructor(parentScope, invoke) {
296
- super(parentScope);
297
- this.invoke = invoke;
298
- }
299
- run() {
300
- this.stop();
301
- return this.invoke(this);
302
- }
303
- };
304
- var Handler = class Handler {
305
- get(target, key) {
306
- const compute = activeCompute;
307
- if (compute) {
308
- const computes = targets.get(target) ?? /* @__PURE__ */ new Map();
309
- const set = computes.get(key) ?? /* @__PURE__ */ new Set();
310
- computes.set(key, set.add(compute));
311
- targets.set(target, computes);
312
- compute.parentScope?.onStop(() => {
313
- set.delete(compute);
314
- if (set.size === 0) {
315
- computes.delete(key);
316
- if (computes.size === 0) targets.delete(target);
317
- }
318
- });
319
- }
320
- const value = Reflect.get(target, key);
321
- if (value) {
322
- if (typeof value === "function" && !value.prototype) return value.bind(target);
323
- if (typeof value === "object") {
324
- const tag = Object.prototype.toString.call(value);
325
- 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());
326
- }
327
- }
328
- return value;
329
- }
330
- set(target, key, value) {
331
- const result = Reflect.set(target, key, value);
332
- for (const compute of targets.get(target)?.get(key) ?? []) compute.run();
333
- return result;
334
- }
335
- };
336
- function createState(value) {
337
- return new Proxy({ value }, new Handler());
338
- }
339
- function createCompute(scope, invoke) {
340
- let previous = activeCompute;
341
- activeCompute = new Compute(scope, invoke);
342
- const result = invoke(activeCompute);
343
- if (result instanceof Promise) return result.finally(() => activeCompute = previous);
344
- activeCompute = previous;
345
- return result;
346
- }
347
- function createMemo(scope, invoke) {
348
- let state;
349
- const compute = createCompute(scope, (scope$1) => {
350
- const value = invoke(scope$1);
351
- if (typeof state === "object") state.value = value;
352
- return value;
353
- });
354
- state = createState(compute);
355
- return state;
356
- }
357
- function fromValue(value) {
358
- if (value instanceof Function) return fromValue(value());
359
- return value;
360
- }
361
- function untrack(invoke) {
362
- let previous = activeCompute;
363
- activeCompute = void 0;
364
- const result = invoke();
365
- if (result instanceof Promise) return result.finally(() => activeCompute = previous);
366
- activeCompute = previous;
367
- return result;
368
- }
369
- function defineContext(key) {
370
- return key;
371
- }
372
- let activeCompute;
373
- const targets = /* @__PURE__ */ new WeakMap();
374
-
375
384
  //#endregion
376
385
  //#region src/runtime/index.ts
377
386
  const isRoute = (value) => {
@@ -421,17 +430,29 @@ const $fetch = async (scope, input, options) => {
421
430
  default: return response;
422
431
  }
423
432
  };
424
- const useAsync = (scope, invoke) => {
433
+ const useAsync = (scope, invoke, options) => {
425
434
  const { tasks } = useRuntime(scope);
426
435
  const state = createState();
427
436
  const isLoading = createState(true);
428
- const task = invoke().then((value) => state.value = value).finally(() => isLoading.value = false);
437
+ const execute = async () => {
438
+ try {
439
+ state.value = await invoke().finally(() => isLoading.value = false);
440
+ } catch (error) {
441
+ options?.catch?.(error);
442
+ }
443
+ return state.value;
444
+ };
445
+ const task = execute();
429
446
  if (isServer()) tasks.push(task);
430
447
  return {
431
448
  state,
432
- isLoading
449
+ isLoading,
450
+ execute
433
451
  };
434
452
  };
453
+ const useFetch = (scope, input, options) => {
454
+ return useAsync(scope, async () => await $fetch(scope, input, options), options);
455
+ };
435
456
  const createRuntime = async () => {
436
457
  const radix = new Radix();
437
458
  const middlewares = new Array();
@@ -901,4 +922,4 @@ const useLocale = (scope, context) => {
901
922
  const LOCALE_CONTEXT = defineContext("LOCALE_CONTEXT");
902
923
 
903
924
  //#endregion
904
- export { $fetch, AfterNavigateEvent, 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, createMemo, createRuntime, createState, defineComponent, defineContext, defineMiddleware, defineRoute, fileName, fromValue, hydrate, isClient, isComponent, isCustomElement, isRoute, isServer, isTemplate, mimeType, onMounted, preventDefault, provideLocaleContext, provideRouterContext, registerComponent, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, startViewTransition, stopImmediatePropagation, stopPropagation, targets, toArray, toCustomElement, toFragment, toPath, toRange, toString, untrack, useAsync, useCookies, useEvent, useHost, useLocale, useRequestUrl, useRoute, useRouter, useRuntime, useSetCookies };
925
+ export { $fetch, AfterNavigateEvent, 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, createMemo, createRuntime, createState, defineComponent, defineContext, defineMiddleware, defineRoute, fileName, fromValue, hydrate, isClient, isComponent, isCustomElement, isRoute, isServer, isTemplate, mergeObjects, mimeType, onMounted, preventDefault, provideLocaleContext, provideRouterContext, registerComponent, renderToString, sendBadRequest, sendHtml, sendJson, sendRedirect, sendText, sendUnauthorized, setCookie, startViewTransition, stopImmediatePropagation, stopPropagation, targets, toArray, toCustomElement, toFragment, toPath, toRange, toString, untrack, useAsync, useCookies, useEvent, useFetch, useHost, useLocale, useRequestUrl, useRoute, useRouter, useRuntime, useSetCookies };
@@ -367,5 +367,5 @@ export declare namespace JSX {
367
367
  }
368
368
  export declare const svgElements: Set<string>;
369
369
  export declare const namespace: (tag: string) => "http://www.w3.org/1999/xhtml" | "http://www.w3.org/2000/svg";
370
- export declare const h: <TEvents extends import("..").Events, TAttributes extends import("..").Attributes>(input: string | import("..").ComponentConstructor<TEvents, TAttributes>, attributes?: import("..").AttributeInput<TAttributes>, ...children: Array<Slot>) => Slot;
370
+ export declare const h: <TEvents extends import("..").Events, TAttributes extends import("..").Attributes>(input: string | ((attributes: import("..").AttributeInput<import("..").Attributes>) => Slot) | import("..").ComponentConstructor<TEvents, TAttributes>, attributes?: import("..").AttributeInput<TAttributes>, ...children: Array<Slot>) => Slot;
371
371
  export declare const fragment: ({ children }: Fragment) => unknown[];
@@ -21,6 +21,9 @@ export type RuntimeContext<T = Record<string, unknown>> = {
21
21
  export type RouteContext = {
22
22
  inputs: State<Record<string, string>>;
23
23
  };
24
+ export type AsyncOptions = {
25
+ catch?: (error: unknown) => void | Promise<void>;
26
+ };
24
27
  export declare const isRoute: <T>(value?: T) => value is Route & T;
25
28
  export declare const useRuntime: <T = Record<string, unknown>>(scope: Scope) => RuntimeContext<T>;
26
29
  export declare const useRoute: (scope: Scope) => RouteContext;
@@ -29,9 +32,15 @@ export declare const defineMiddleware: (middleware: Middleware) => Middleware;
29
32
  export declare const fileName: (path: string) => string | undefined;
30
33
  export declare const toPath: (value: string) => (string | undefined)[];
31
34
  export declare const $fetch: <T>(scope: Scope, input: string | URL, options?: RequestInit) => Promise<T>;
32
- export declare const useAsync: <T>(scope: Scope, invoke: () => Promise<T>) => {
35
+ export declare const useAsync: <T>(scope: Scope, invoke: () => Promise<T>, options?: AsyncOptions) => {
36
+ state: State<T | undefined>;
37
+ isLoading: State<boolean>;
38
+ execute: () => Promise<T | undefined>;
39
+ };
40
+ export declare const useFetch: <T>(scope: Scope, input: string | URL, options?: RequestInit & AsyncOptions) => {
33
41
  state: State<T | undefined>;
34
42
  isLoading: State<boolean>;
43
+ execute: () => Promise<T | undefined>;
35
44
  };
36
45
  export declare const createRuntime: () => Promise<Runtime>;
37
46
  export declare const RUNTIME_CONTEXT: import("..").Descriptor<RuntimeContext<Record<string, unknown>>>;
@@ -33,6 +33,7 @@ export declare function createCompute<T>(scope: Scope, invoke: (scope: Scope) =>
33
33
  export declare function createMemo<T>(scope: Scope, invoke: (scope: Scope) => T): State<T>;
34
34
  export declare function fromValue<T>(value: Value<T>): T;
35
35
  export declare function untrack<T>(invoke: () => T): T;
36
+ export declare function mergeObjects<TBase, TInput>(base: TBase, input: TInput): TBase & TInput;
36
37
  export declare function defineContext<T>(key: string): Descriptor<T>;
37
38
  export declare let activeCompute: Compute | undefined;
38
39
  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.66",
3
+ "version": "0.0.68",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",
@@ -32,15 +32,12 @@
32
32
  "build": "rolldown -c rolldown.config.ts && tsc",
33
33
  "watch": "rolldown -w -c rolldown.config.ts && tsc --watch"
34
34
  },
35
- "dependencies": {
36
- "defu": "^6.1.4"
37
- },
38
35
  "optionalDependencies": {
39
36
  "@rolldown/binding-linux-x64-gnu": "*"
40
37
  },
41
38
  "devDependencies": {
42
- "@revojs/tsconfig": "*",
43
39
  "@revojs/rolldown": "*",
40
+ "@revojs/tsconfig": "*",
44
41
  "@types/bun": "^1.2.17",
45
42
  "rolldown": "^1.0.0-beta.19"
46
43
  }