@revojs/vue 0.1.36 → 0.1.38

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,20 +1,20 @@
1
1
  import * as revojs from "revojs";
2
2
  import { Scope } from "revojs";
3
- import { Ref } from "vue";
3
+ import { MaybeRefOrGetter, Ref } from "vue";
4
4
 
5
5
  //#region src/client/index.d.ts
6
6
  interface AsyncOptions<T> {
7
7
  catch?: (error: T) => void | Promise<void>;
8
8
  }
9
9
  declare function useScope(): Scope;
10
- declare function useState<T>(scope: Scope, name: string): Ref<T | undefined>;
11
- declare function useState<T>(scope: Scope, name: string, value: T): Ref<T>;
12
- declare function useAsync<T, TError = Error>(scope: Scope, name: string, invoke: () => Promise<T>, defaultOptions?: AsyncOptions<TError>): Promise<{
10
+ declare function useState<T>(scope: Scope, name: MaybeRefOrGetter<string>): Ref<T | undefined>;
11
+ declare function useState<T>(scope: Scope, name: MaybeRefOrGetter<string>, value: T): Ref<T>;
12
+ declare function useAsync<T, TError = Error>(scope: Scope, name: MaybeRefOrGetter<string>, invoke: () => Promise<T>, defaultOptions?: AsyncOptions<TError>): Promise<{
13
13
  state: Ref<T | undefined, T | undefined>;
14
14
  isLoading: Ref<boolean, boolean>;
15
15
  refresh: (options?: AsyncOptions<TError>) => Promise<T | undefined>;
16
16
  }>;
17
- declare function useFetch<T, TError = Error>(scope: Scope, input: string | URL, options?: RequestInit & AsyncOptions<TError>): Promise<{
17
+ declare function useFetch<T, TError = Error>(scope: Scope, input: MaybeRefOrGetter<string>, options?: RequestInit & AsyncOptions<TError>): Promise<{
18
18
  state: Ref<T | undefined, T | undefined>;
19
19
  isLoading: Ref<boolean, boolean>;
20
20
  refresh: (options?: AsyncOptions<TError> | undefined) => Promise<T | undefined>;
@@ -1,5 +1,5 @@
1
1
  import { $fetch, defineHook, getState, isClient, isServer, setState } from "revojs";
2
- import { customRef, inject, onScopeDispose, ref } from "vue";
2
+ import { customRef, inject, isRef, onScopeDispose, ref, toValue, watch } from "vue";
3
3
 
4
4
  //#region src/client/index.ts
5
5
  function useScope() {
@@ -8,15 +8,14 @@ function useScope() {
8
8
  throw new Error();
9
9
  }
10
10
  function useState(scope, name, value) {
11
- let state = getState(scope, name) ?? value;
12
11
  return customRef((track, trigger) => ({
13
12
  get() {
14
13
  track();
15
- return state;
14
+ return getState(scope, toValue(name)) ?? value;
16
15
  },
17
16
  set(update) {
18
- state = update;
19
- setState(scope, name, state);
17
+ console.log(toValue(name), update);
18
+ setState(scope, toValue(name), update);
20
19
  trigger();
21
20
  }
22
21
  }));
@@ -40,7 +39,7 @@ async function useAsync(scope, name, invoke, defaultOptions) {
40
39
  const task = refresh();
41
40
  if (isServer) await task;
42
41
  if (isClient) onScopeDispose(scope.registerHook(REFRESH_ASYNC_HOOK, async (names) => {
43
- if (names === void 0 || names?.includes(name)) await refresh();
42
+ if (names === void 0 || names?.includes(toValue(name))) await refresh();
44
43
  }));
45
44
  return {
46
45
  state,
@@ -48,8 +47,16 @@ async function useAsync(scope, name, invoke, defaultOptions) {
48
47
  refresh
49
48
  };
50
49
  }
51
- function useFetch(scope, input, options) {
52
- return useAsync(scope, input.toString(), () => $fetch(scope, input, options), options);
50
+ async function useFetch(scope, input, options) {
51
+ const { state, isLoading, refresh } = await useAsync(scope, input, () => $fetch(scope, toValue(input), options), options);
52
+ if (isClient && (isRef(input) || typeof input === "function")) watch(input, async (next, previous) => {
53
+ if (next !== previous) await refresh();
54
+ });
55
+ return {
56
+ state,
57
+ isLoading,
58
+ refresh
59
+ };
53
60
  }
54
61
  async function refreshAsync(scope, input) {
55
62
  if (isClient) return await scope.dispatchHook(REFRESH_ASYNC_HOOK, input === void 0 ? void 0 : Array.isArray(input) ? input : [input]);
@@ -1,5 +1,5 @@
1
1
  import Main from "#alias/vue/main";
2
- import pages from "#virtual/pages";
2
+ import { entries } from "#pages";
3
3
  import { isServer, Radix, Scope, useUrl, type Node } from "revojs";
4
4
  import { createApp, createSSRApp, type Component } from "vue";
5
5
  import {
@@ -45,7 +45,7 @@ function toRoute(path: string, node: Node<Component>, index: number): RouteRecor
45
45
  export default async (scope: Scope) => {
46
46
  const radix = new Radix<Component>();
47
47
 
48
- for (const path in pages) {
48
+ for (const path in entries) {
49
49
  const segments = path.split("/");
50
50
 
51
51
  for (const attribute of segments.pop()?.split(".") ?? []) {
@@ -56,7 +56,7 @@ export default async (scope: Scope) => {
56
56
  segments.push(attribute);
57
57
  }
58
58
 
59
- radix.use(segments, pages[path]);
59
+ radix.use(segments, entries[path]);
60
60
  }
61
61
 
62
62
  const rootNode = toRoute("/", radix.rootNode, 0);
@@ -1,5 +1,5 @@
1
1
  import createApp from "#alias/vue/app";
2
- import client from "#virtual/client";
2
+ import client from "#client";
3
3
  import { defineRoute, sendHtml, useServer } from "revojs";
4
4
  import { renderToString } from "vue/server-renderer";
5
5
  import { isServerSideRendering } from "../../../client";
@@ -23,12 +23,16 @@ export default defineRoute({
23
23
  }
24
24
  }
25
25
 
26
+ const value = JSON.stringify(states)
27
+ .replace(/</g, "\\u003c")
28
+ .replace(/>/g, "\\u003e")
29
+ .replace(/&/g, "\\u0026")
30
+ .replace(/\u2028/g, "\\u2028")
31
+ .replace(/\u2029/g, "\\u2029");
32
+
26
33
  return sendHtml(
27
34
  scope,
28
- content.replace(
29
- "<!-- HEAD -->",
30
- "<script id='STATES' type='application/json'>" + JSON.stringify(states) + "</script>",
31
- ),
35
+ content.replace("<!-- HEAD -->", "<script id='STATES' type='application/json'>" + value + "</script>"),
32
36
  );
33
37
  },
34
38
  });
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@revojs/vue",
3
- "version": "0.1.36",
3
+ "version": "0.1.38",
4
4
  "license": "MIT",
5
- "repository": "coverbase/revojs",
5
+ "repository": "tellua/revojs",
6
6
  "files": [
7
7
  "dist",
8
8
  "src/types"
@@ -1,4 +1,5 @@
1
1
  declare module "#alias/vue/app" {
2
+ import type { Scope } from "revojs";
2
3
  import type { App } from "vue";
3
4
 
4
5
  export default function createApp(scope: Scope): Promise<App>;
@@ -12,14 +13,6 @@ declare module "#alias/vue/main" {
12
13
  export default app;
13
14
  }
14
15
 
15
- declare module "#virtual/pages" {
16
- import type { Component } from "vue";
17
-
18
- const pages: Record<string, Component>;
19
-
20
- export default pages;
21
- }
22
-
23
16
  declare module "*.vue" {
24
17
  import type { DefineComponent } from "vue";
25
18
 
@@ -0,0 +1,5 @@
1
+ declare module "#pages" {
2
+ import type { Component } from "vue";
3
+
4
+ export const entries: Record<string, Component>;
5
+ }