Refactoring to take advantage of Immer's drafts

This commit is contained in:
Eric van der Vlist 2022-09-14 11:52:37 +02:00
parent d5196f4340
commit 3a7d4987a3
1 changed files with 29 additions and 10 deletions

View File

@ -60,12 +60,29 @@ const nbTilesY = _.ceil(mapState.viewport.height / tileSize) + 4;
*/ */
const computeStateFromScope = (scope: MapScope) => { let initialMapState: MapState = {
const newScope = _.cloneDeep(scope); scope: initialMapScope,
let state: MapState = {} as MapState; slippy: {
state.scope = newScope; scale: 1,
state.tiles = {} as TilesDescription; translation: {
state.slippy = {} as SlippyState; x: 0,
y: 0,
},
},
tiles: {
first: {
x: 0,
y: 0,
},
nb: {
x: 0,
y: 0,
},
zoom: 0,
},
};
const evaluateStateFromScope = (state: MapState) => {
state.tiles.zoom = _.round(state.scope.zoom); state.tiles.zoom = _.round(state.scope.zoom);
const softZoom = state.scope.zoom - state.tiles.zoom; const softZoom = state.scope.zoom - state.tiles.zoom;
state.slippy.scale = 2 ** softZoom; state.slippy.scale = 2 ** softZoom;
@ -94,18 +111,20 @@ const computeStateFromScope = (scope: MapScope) => {
x: tilesCenterTargetLocation.x - tilesCenterActualLocation.x, x: tilesCenterTargetLocation.x - tilesCenterActualLocation.x,
y: tilesCenterTargetLocation.y - tilesCenterActualLocation.y, y: tilesCenterTargetLocation.y - tilesCenterActualLocation.y,
}; };
return state;
}; };
const initialMapState: MapState = computeStateFromScope(initialMapScope); evaluateStateFromScope(initialMapState);
const reevaluateState = (state: MapState) => {};
const mapSlice = createSlice({ const mapSlice = createSlice({
name: 'map', name: 'map',
initialState: initialMapState, initialState: initialMapState,
reducers: { reducers: {
resize: (state) => { resize: (state) => {
return computeStateFromScope(state.scope); evaluateStateFromScope(state);
}, },
shift: (state, action) => { shift: (state, action) => {
state.slippy.translation = { state.slippy.translation = {