def reducer(state: State, action: Action) -> State: """Combine state and action to produce new state""" state = copy.deepcopy(state) if isinstance(state, dict): state = forest.state.State.from_dict(state) if isinstance(action, dict): try: action = forest.actions.Action.from_dict(action) except TypeError: return state.to_dict() if action.kind == SET_FIGURES: state.layers.figures = action.payload elif action.kind == ON_ADD: state.layers.mode.state = "add" elif action.kind == ON_CLOSE: row_index = action.payload try: layer_index = sorted(state.layers.index.keys())[row_index] del state.layers.index[layer_index] except IndexError: pass elif action.kind == ON_EDIT: row_index = action.payload layer_index = sorted(state.layers.index.keys())[row_index] state.layers.mode.state = "edit" state.layers.mode.index = layer_index elif action.kind == SAVE_LAYER: # NOTE: Layer index is stored in payload layer_index = action.payload["index"] settings = action.payload["settings"] if layer_index in state.layers.index: state.layers.index[layer_index].update(settings) else: state.layers.index[layer_index] = settings elif action.kind == SET_ACTIVE: active = action.payload["active"] row_index = action.payload["row_index"] row_to_layer = sorted(state.layers.index.keys()) try: layer_index = row_to_layer[row_index] state.layers.index[layer_index]["active"] = active except IndexError: pass return state.to_dict()
def reducer(state, action): """Add HTML loaded action to state""" if isinstance(action, dict): try: action = forest.actions.Action.from_dict(action) except TypeError: # TODO: Remove try/except when Actions are supported return state if isinstance(state, dict): state = forest.state.State.from_dict(state) if action.kind == forest.actions.HTML_LOADED: state.bokeh.html_loaded = True return state.to_dict()
def borders_reducer(state, action): """Configure borders, coastlines and lakes""" if isinstance(action, dict): try: action = actions.Action.from_dict(action) except TypeError: # TODO: Support Action throughout codebase return state # Reduce state.borders if isinstance(state, dict): state = forest.state.State.from_dict(state) if action.kind == actions.SET_BORDERS_VISIBLE: state.borders.visible = action.payload elif action.kind == actions.SET_BORDERS_LINE_COLOR: state.borders.line_color = action.payload return state.to_dict()
def render_id(self, state, layer_id): """Render layer settings""" if isinstance(state, dict): state = forest.state.State.from_dict() if layer_id in state.layers.index: spec = LayerSpec(**state.layers.index[layer_id]) if spec.dataset == "": return self.visible.active = spec.active # Layer-specific state layer_state = {} layer_state.update(state.to_dict()) if spec.variable != "": layer_state.update(variable=spec.variable, colorbar=spec.colorbar) self.map_view.render(layer_state)