mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-04-26 02:03:49 +02:00
add @tanstack/query
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
import { request } from '../service/request';
|
||||
import { useWorldStore } from '../stores';
|
||||
import {
|
||||
entityQueryPolicies,
|
||||
fetchWithEntityPolicy,
|
||||
patchAndRefetchActiveQuery,
|
||||
queryKeys
|
||||
} from '../query';
|
||||
|
||||
const worldReq = {
|
||||
/**
|
||||
@@ -24,26 +30,14 @@ const worldReq = {
|
||||
* @returns {Promise<{json: any, ref: any, cache?: boolean, params}>}
|
||||
*/
|
||||
getCachedWorld(params) {
|
||||
const worldStore = useWorldStore();
|
||||
return new Promise((resolve, reject) => {
|
||||
const ref = worldStore.cachedWorlds.get(params.worldId);
|
||||
if (typeof ref === 'undefined') {
|
||||
worldReq
|
||||
.getWorld(params)
|
||||
.then((args) => {
|
||||
args.ref = worldStore.applyWorld(args.json);
|
||||
resolve(args);
|
||||
})
|
||||
.catch(reject);
|
||||
} else {
|
||||
resolve({
|
||||
cache: true,
|
||||
json: ref,
|
||||
params,
|
||||
ref
|
||||
});
|
||||
}
|
||||
});
|
||||
return fetchWithEntityPolicy({
|
||||
queryKey: queryKeys.world(params.worldId),
|
||||
policy: entityQueryPolicies.world,
|
||||
queryFn: () => worldReq.getWorld(params)
|
||||
}).then(({ data, cache }) => ({
|
||||
...data,
|
||||
cache
|
||||
}));
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -70,6 +64,24 @@ const worldReq = {
|
||||
return args;
|
||||
});
|
||||
},
|
||||
/**
|
||||
* @param {object} params
|
||||
* @param {string} [option]
|
||||
* @returns {Promise<{json: any, cache?: boolean, params: any, option?: string}>}
|
||||
*/
|
||||
getCachedWorlds(params, option) {
|
||||
return fetchWithEntityPolicy({
|
||||
queryKey: queryKeys.worldsByUser({
|
||||
...params,
|
||||
option: option || ''
|
||||
}),
|
||||
policy: entityQueryPolicies.worldCollection,
|
||||
queryFn: () => worldReq.getWorlds(params, option)
|
||||
}).then(({ data, cache }) => ({
|
||||
...data,
|
||||
cache
|
||||
}));
|
||||
},
|
||||
/**
|
||||
* @param {{worldId: string}} params
|
||||
* @returns {Promise<{json: any, params}>}
|
||||
@@ -100,6 +112,12 @@ const worldReq = {
|
||||
params
|
||||
};
|
||||
args.ref = worldStore.applyWorld(json);
|
||||
patchAndRefetchActiveQuery({
|
||||
queryKey: queryKeys.world(args.ref.id),
|
||||
nextData: args
|
||||
}).catch((err) => {
|
||||
console.error('Failed to refresh world query after mutation:', err);
|
||||
});
|
||||
return args;
|
||||
});
|
||||
},
|
||||
@@ -119,6 +137,12 @@ const worldReq = {
|
||||
params
|
||||
};
|
||||
args.ref = worldStore.applyWorld(json);
|
||||
patchAndRefetchActiveQuery({
|
||||
queryKey: queryKeys.world(args.ref.id),
|
||||
nextData: args
|
||||
}).catch((err) => {
|
||||
console.error('Failed to refresh world query after publish:', err);
|
||||
});
|
||||
return args;
|
||||
});
|
||||
},
|
||||
@@ -138,6 +162,12 @@ const worldReq = {
|
||||
params
|
||||
};
|
||||
args.ref = worldStore.applyWorld(json);
|
||||
patchAndRefetchActiveQuery({
|
||||
queryKey: queryKeys.world(args.ref.id),
|
||||
nextData: args
|
||||
}).catch((err) => {
|
||||
console.error('Failed to refresh world query after unpublish:', err);
|
||||
});
|
||||
return args;
|
||||
});
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user