add @tanstack/query

This commit is contained in:
pa
2026-03-06 18:14:24 +09:00
parent 7d2bb022a4
commit e665b3815d
40 changed files with 2171 additions and 232 deletions

View File

@@ -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;
});
},