@revojs/vue 0.1.21 → 0.1.22

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,23 +1,25 @@
1
+ import * as revojs0 from "revojs";
1
2
  import { Scope } from "revojs";
2
3
  import { Ref } from "vue";
3
4
 
4
5
  //#region src/client/index.d.ts
5
- type AsyncOptions<T> = {
6
+ interface AsyncOptions<T> {
6
7
  catch?: (error: T) => void | Promise<void>;
7
- };
8
+ }
8
9
  declare function useScope(): Scope;
9
10
  declare function useState<T>(scope: Scope, name: string): Ref<T | undefined>;
10
11
  declare function useState<T>(scope: Scope, name: string, value: T): Ref<T>;
11
- declare function useAsync<T, TError = Error>(scope: Scope, name: string, invoke: () => Promise<T>, defaultOptions?: AsyncOptions<TError>): {
12
+ declare function useAsync<T, TError = Error>(scope: Scope, name: string, invoke: () => Promise<T>, defaultOptions?: AsyncOptions<TError>): Promise<{
12
13
  state: Ref<T | undefined, T | undefined>;
13
14
  isLoading: Ref<boolean, boolean>;
14
- execute: (options?: AsyncOptions<TError>) => Promise<T | undefined>;
15
- };
16
- declare function useFetch<T, TError = Error>(scope: Scope, input: string | URL, options?: RequestInit & AsyncOptions<TError>): {
15
+ refresh: (options?: AsyncOptions<TError>) => Promise<T | undefined>;
16
+ }>;
17
+ declare function useFetch<T, TError = Error>(scope: Scope, input: string | URL, options?: RequestInit & AsyncOptions<TError>): Promise<{
17
18
  state: Ref<T | undefined, T | undefined>;
18
19
  isLoading: Ref<boolean, boolean>;
19
- execute: (options?: AsyncOptions<TError> | undefined) => Promise<T | undefined>;
20
- };
21
- declare function navigate(scope: Scope, path: string): void;
20
+ refresh: (options?: AsyncOptions<TError> | undefined) => Promise<T | undefined>;
21
+ }>;
22
+ declare function refreshAsync(scope: Scope, input?: string | Array<string>): Promise<void[] | undefined>;
23
+ declare const REFRESH_ASYNC_HOOK: revojs0.Descriptor<(names?: Array<string>) => Promise<void>>;
22
24
  //#endregion
23
- export { AsyncOptions, navigate, useAsync, useFetch, useScope, useState };
25
+ export { AsyncOptions, REFRESH_ASYNC_HOOK, refreshAsync, useAsync, useFetch, useScope, useState };
@@ -1,6 +1,5 @@
1
- import { $fetch, getState, sendRedirect, setState } from "revojs";
2
- import { customRef, inject, onServerPrefetch, ref } from "vue";
3
- import { useRouter } from "vue-router";
1
+ import { $fetch, defineHook, getState, isClient, isServer, setState } from "revojs";
2
+ import { customRef, inject, onScopeDispose, ref } from "vue";
4
3
 
5
4
  //#region src/client/index.ts
6
5
  function useScope() {
@@ -22,10 +21,10 @@ function useState(scope, name, value) {
22
21
  }
23
22
  }));
24
23
  }
25
- function useAsync(scope, name, invoke, defaultOptions) {
24
+ async function useAsync(scope, name, invoke, defaultOptions) {
26
25
  const state = useState(scope, name);
27
26
  const isLoading = ref(false);
28
- const execute = async (options) => {
27
+ const refresh = async (options) => {
29
28
  const onCatch = options?.catch ?? defaultOptions?.catch;
30
29
  isLoading.value = true;
31
30
  try {
@@ -37,21 +36,24 @@ function useAsync(scope, name, invoke, defaultOptions) {
37
36
  }
38
37
  return state.value;
39
38
  };
40
- const task = execute();
41
- if (import.meta.server) onServerPrefetch(async () => await task);
39
+ const task = refresh();
40
+ if (isServer) await task;
41
+ if (isClient) onScopeDispose(scope.registerHook(REFRESH_ASYNC_HOOK, async (names) => {
42
+ if (names === void 0 || names?.includes(name)) await refresh();
43
+ }));
42
44
  return {
43
45
  state,
44
46
  isLoading,
45
- execute
47
+ refresh
46
48
  };
47
49
  }
48
50
  function useFetch(scope, input, options) {
49
51
  return useAsync(scope, input.toString(), () => $fetch(scope, input, options), options);
50
52
  }
51
- function navigate(scope, path) {
52
- if (import.meta.server) throw sendRedirect(scope, path);
53
- else useRouter().push(path);
53
+ async function refreshAsync(scope, input) {
54
+ if (isClient) return await scope.dispatchHook(REFRESH_ASYNC_HOOK, input === void 0 ? void 0 : Array.isArray(input) ? input : [input]);
54
55
  }
56
+ const REFRESH_ASYNC_HOOK = defineHook("REFRESH_ASYNC_HOOK");
55
57
 
56
58
  //#endregion
57
- export { navigate, useAsync, useFetch, useScope, useState };
59
+ export { REFRESH_ASYNC_HOOK, refreshAsync, useAsync, useFetch, useScope, useState };
@@ -1,4 +1,4 @@
1
- import { Radix, Scope, toRoutePath, useUrl, type Node } from "revojs";
1
+ import { isServer, Radix, Scope, toRoutePath, useUrl, type Node } from "revojs";
2
2
  import { type Component, createSSRApp } from "vue";
3
3
  import { createMemoryHistory, createRouter, createWebHistory, RouterView, type RouteRecordRaw } from "vue-router";
4
4
  import Main from "#alias/vue/main";
@@ -44,7 +44,7 @@ export default async (scope: Scope) => {
44
44
  }
45
45
 
46
46
  const router = createRouter({
47
- history: import.meta.server ? createMemoryHistory() : createWebHistory(),
47
+ history: isServer ? createMemoryHistory() : createWebHistory(),
48
48
  routes: [toRoute("/", radix.rootNode, 0)],
49
49
  });
50
50
 
@@ -52,7 +52,7 @@ export default async (scope: Scope) => {
52
52
 
53
53
  app.config.throwUnhandledErrorInProduction = true;
54
54
 
55
- if (import.meta.server) {
55
+ if (isServer) {
56
56
  const { pathname } = useUrl(scope);
57
57
 
58
58
  await router.push(pathname);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@revojs/vue",
3
- "version": "0.1.21",
3
+ "version": "0.1.22",
4
4
  "type": "module",
5
5
  "repository": "coverbase/revojs",
6
6
  "license": "MIT",