djledda.de main
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

2 个月前
2 个月前
2 个月前
2 个月前
2 个月前
2 个月前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { onMounted, onServerPrefetch, type ShallowRef, shallowRef } from "vue";
  2. import useDJSSRContext from "@/useDJSSRContext.ts";
  3. declare global {
  4. // deno-lint-ignore no-var
  5. var appstate: Partial<Record<string, unknown>>;
  6. }
  7. export default function useAsyncState<T>(
  8. key: string,
  9. getter: (context: { hostUrl: string }) => Promise<T | null>,
  10. options?: { suspensible: boolean },
  11. ): { result: ShallowRef<T | null>; stateIsReady: Promise<unknown> } {
  12. const ssrContext = useDJSSRContext();
  13. const isClient = typeof ssrContext === "undefined";
  14. const registry = ssrContext?.registry ?? globalThis?.appstate;
  15. const hostUrl = isClient ? globalThis.location.origin : "http://localhost:8080";
  16. const state = shallowRef<T | null>(null);
  17. let resolve = () => {};
  18. const promise = new Promise<void>((res) => {
  19. resolve = res;
  20. });
  21. if (key in registry) {
  22. state.value = registry[key] as T;
  23. delete registry[key];
  24. resolve();
  25. } else {
  26. if (!isClient) {
  27. resolve();
  28. }
  29. onServerPrefetch(async () => {
  30. const result = await getter({ hostUrl });
  31. registry[key] = result;
  32. state.value = result;
  33. });
  34. if (options?.suspensible ?? true) {
  35. getter({ hostUrl }).then((result) => {
  36. state.value = result;
  37. resolve();
  38. }).catch(resolve);
  39. } else {
  40. onMounted(async () => {
  41. state.value = await getter({ hostUrl });
  42. resolve();
  43. });
  44. }
  45. }
  46. return { result: state, stateIsReady: promise };
  47. }