From 3a7d4987a3bb70f48da298acc4b7f7e531f8aadb Mon Sep 17 00:00:00 2001 From: evlist Date: Wed, 14 Sep 2022 11:52:37 +0200 Subject: [PATCH] Refactoring to take advantage of Immer's drafts --- src/store/map.ts | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/store/map.ts b/src/store/map.ts index bb73750..e622d10 100644 --- a/src/store/map.ts +++ b/src/store/map.ts @@ -60,12 +60,29 @@ const nbTilesY = _.ceil(mapState.viewport.height / tileSize) + 4; */ -const computeStateFromScope = (scope: MapScope) => { - const newScope = _.cloneDeep(scope); - let state: MapState = {} as MapState; - state.scope = newScope; - state.tiles = {} as TilesDescription; - state.slippy = {} as SlippyState; +let initialMapState: MapState = { + scope: initialMapScope, + slippy: { + scale: 1, + translation: { + 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); const softZoom = state.scope.zoom - state.tiles.zoom; state.slippy.scale = 2 ** softZoom; @@ -94,18 +111,20 @@ const computeStateFromScope = (scope: MapScope) => { x: tilesCenterTargetLocation.x - tilesCenterActualLocation.x, y: tilesCenterTargetLocation.y - tilesCenterActualLocation.y, }; - - return state; }; -const initialMapState: MapState = computeStateFromScope(initialMapScope); +evaluateStateFromScope(initialMapState); + +const reevaluateState = (state: MapState) => {}; + + const mapSlice = createSlice({ name: 'map', initialState: initialMapState, reducers: { resize: (state) => { - return computeStateFromScope(state.scope); + evaluateStateFromScope(state); }, shift: (state, action) => { state.slippy.translation = {