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

35 lines
867 B
JavaScript
Raw Normal View History

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