This commit is contained in:
parent
8b203908a0
commit
725945d95c
22 changed files with 381 additions and 135 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { defineStore } from "pinia";
|
||||
import { shallowRef } from "vue";
|
||||
import { ref, shallowRef } from "vue";
|
||||
|
||||
import { SSEManager, SSEMessageResource } from "@/SSEManager";
|
||||
import { useSessionStore } from "@/store/session";
|
||||
|
|
@ -8,6 +8,8 @@ import { apiClient } from "@/client";
|
|||
import {
|
||||
Communication,
|
||||
type CommunicationDTO,
|
||||
Contact,
|
||||
type ContactDTO,
|
||||
PublicReport,
|
||||
type PublicReportDTO,
|
||||
} from "@/type/api";
|
||||
|
|
@ -22,6 +24,7 @@ function createResourceStore<dto, full extends uriHaver>(
|
|||
from_json: jsonConverter<dto, full>,
|
||||
) {
|
||||
const _resourceByURI = shallowRef<Map<string, full>>(new Map());
|
||||
const _resourceFetchAll = ref<Promise<full[]> | null>(null);
|
||||
const _resourceFetchByURI = shallowRef<Map<string, Promise<full> | null>>(
|
||||
new Map(),
|
||||
);
|
||||
|
|
@ -33,20 +36,25 @@ function createResourceStore<dto, full extends uriHaver>(
|
|||
}
|
||||
});
|
||||
|
||||
async function byAll(): Promise<full[]> {
|
||||
const cur = _resourceFetchAll.value;
|
||||
if (cur) {
|
||||
return cur;
|
||||
}
|
||||
return fetchAll();
|
||||
}
|
||||
async function byID(id: string): Promise<full> {
|
||||
const uri = uriFromID(id);
|
||||
const cur = _resourceFetchByURI.value.get(uri);
|
||||
if (cur) {
|
||||
return cur;
|
||||
}
|
||||
return fetchByID(id);
|
||||
return byURI(uri);
|
||||
}
|
||||
async function byURI(uri: string): Promise<full> {
|
||||
const cur = _resourceFetchByURI.value.get(uri);
|
||||
let cur = _resourceFetchByURI.value.get(uri);
|
||||
if (cur) {
|
||||
return cur;
|
||||
}
|
||||
return fetchByURI(uri);
|
||||
cur = fetchByURI(uri);
|
||||
_resourceFetchByURI.value.set(uri, cur);
|
||||
return cur;
|
||||
}
|
||||
async function fetchAll(): Promise<full[]> {
|
||||
const sessionStore = useSessionStore();
|
||||
|
|
@ -88,9 +96,12 @@ function createResourceStore<dto, full extends uriHaver>(
|
|||
return `${api_base}/${id}`;
|
||||
}
|
||||
return {
|
||||
byAll,
|
||||
byID,
|
||||
byURI,
|
||||
fetchAll,
|
||||
fetchByID,
|
||||
fetchByURI,
|
||||
loadingURI,
|
||||
};
|
||||
}
|
||||
|
|
@ -101,6 +112,11 @@ export const useStoreResource = defineStore("resource", () => {
|
|||
"/communication",
|
||||
Communication.fromJSON,
|
||||
),
|
||||
contact: createResourceStore<ContactDTO, Contact>(
|
||||
"sync:contact",
|
||||
"/contact",
|
||||
Contact.fromJSON,
|
||||
),
|
||||
publicreport: createResourceStore<PublicReportDTO, PublicReport>(
|
||||
"sync:publicreport",
|
||||
"/publicreport",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue