Skip to content

Commit

Permalink
Add updated sensorSlice with better typing
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-griffin committed May 26, 2021
1 parent 2b12ec5 commit 1bd236d
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/features/Entities/Sensor/SensorSlice.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { SensorManager } from '~features';
import { SagaIterator } from '@redux-saga/types';
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { getContext, call, put, takeEvery, takeLatest } from 'redux-saga/effects';
import { call, put, takeEvery, takeLatest } from 'redux-saga/effects';

import { RootState } from '../../../common/store/store';
import { REDUCER, DEPENDENCY } from '../../../common/constants';
import { REDUCER } from '../../../common/constants';
import { ProgramAction } from '~features/Bluetooth/Program/ProgramSlice';
import { getDependency } from '~features/utils/saga';

export interface SensorState {
id: string;
Expand Down Expand Up @@ -215,8 +216,7 @@ const SensorSelector = {
};

function* fetchAll(): SagaIterator {
const DependencyLocator = yield getContext(DEPENDENCY.LOCATOR);
const sensorManager = yield call(DependencyLocator.get, DEPENDENCY.SENSOR_MANAGER);
const sensorManager: SensorManager = yield call(getDependency, 'sensorManager');
try {
const sensors = yield call(sensorManager.getAll);
const mapped = sensors.map((sensor: SensorState) => ({ ...sensor }));
Expand All @@ -227,10 +227,9 @@ function* fetchAll(): SagaIterator {
}

function* update({ payload: { sensorId, key, value } }: UpdateAction): SagaIterator {
const DependencyLocator = yield getContext(DEPENDENCY.LOCATOR);
const manager = yield call(DependencyLocator.get, DEPENDENCY.SENSOR_MANAGER);
const sensorManager: SensorManager = yield call(getDependency, 'sensorManager');
try {
yield call(manager.updateField, sensorId, key, value);
yield call(sensorManager.update, sensorId, { [key]: value });
yield put(SensorAction.updateSuccess(sensorId, key, value));
} catch (e) {
yield put(SensorAction.updateFail(e.message));
Expand All @@ -240,8 +239,7 @@ function* update({ payload: { sensorId, key, value } }: UpdateAction): SagaItera
function* create({
payload: { macAddress, logInterval, logDelay, batteryLevel },
}: CreateAction): SagaIterator {
const DependencyLocator = yield getContext(DEPENDENCY.LOCATOR);
const sensorManager = yield call(DependencyLocator.get, DEPENDENCY.SENSOR_MANAGER);
const sensorManager: SensorManager = yield call(getDependency, 'sensorManager');
try {
const newlyAddedSensor: SensorState = yield call(
sensorManager.addNewSensor,
Expand Down Expand Up @@ -276,8 +274,7 @@ function* createNewSensor({
}

function* remove({ payload: { id } }: PayloadAction<RemovePayload>): SagaIterator {
const DependencyLocator = yield getContext(DEPENDENCY.LOCATOR);
const sensorManager: SensorManager = yield call(DependencyLocator.get, DEPENDENCY.SENSOR_MANAGER);
const sensorManager: SensorManager = yield call(getDependency, 'sensorManager');

try {
yield call(sensorManager.remove, id);
Expand Down

0 comments on commit 1bd236d

Please sign in to comment.