This repository has been archived on 2023-10-27. You can view files and clone it, but cannot push or open issues or pull requests.
Files
scoresaber-reloaded/src/stores/http/http-player-store.js

38 lines
886 B
JavaScript
Raw Normal View History

2023-10-17 23:38:18 +01:00
import createHttpStore from "./http-store";
import playerApiClient from "../../network/clients/scoresaber/player/api";
2023-10-17 21:42:37 +01:00
2023-10-17 23:38:18 +01:00
export default (
playerId = null,
initialState = null,
initialStateType = "initial",
) => {
2023-10-17 21:42:37 +01:00
let currentPlayerId = playerId;
2023-10-17 23:38:18 +01:00
const onNewData = ({ fetchParams }) => {
2023-10-17 21:42:37 +01:00
currentPlayerId = fetchParams?.playerId ?? null;
2023-10-17 23:38:18 +01:00
};
2023-10-17 21:42:37 +01:00
const httpStore = createHttpStore(
playerApiClient,
2023-10-17 23:38:18 +01:00
playerId ? { playerId } : null,
2023-10-17 21:42:37 +01:00
initialState,
{
onInitialized: onNewData,
onAfterStateChange: onNewData,
},
initialStateType,
);
const fetch = async (playerId = currentPlayerId, force = false) => {
if (!playerId || (playerId === currentPlayerId && !force)) return false;
2023-10-17 23:38:18 +01:00
return httpStore.fetch({ playerId }, force, playerApiClient);
};
2023-10-17 21:42:37 +01:00
return {
...httpStore,
fetch,
getPlayerId: () => currentPlayerId,
2023-10-17 23:38:18 +01:00
};
};