From f9f23a520c520dad3e18e52ca16414c0ace10221 Mon Sep 17 00:00:00 2001 From: Matthieu Louis Date: Tue, 12 Feb 2019 19:03:09 +0000 Subject: [PATCH] Cast status property to Status type --- src/index.tsx | 2 +- src/state/workloads/epics.ts | 11 ++--------- src/state/workloads/services.ts | 16 ++++++++-------- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index e93d5a8..318b54f 100755 --- a/src/index.tsx +++ b/src/index.tsx @@ -28,6 +28,6 @@ ReactDOM.render( - ), + ), document.getElementById('root'), ); diff --git a/src/state/workloads/epics.ts b/src/state/workloads/epics.ts index 06d053f..f1b06b4 100644 --- a/src/state/workloads/epics.ts +++ b/src/state/workloads/epics.ts @@ -1,4 +1,4 @@ -import { combineEpics, Epic, ofType } from 'redux-observable'; +import { combineEpics, Epic } from 'redux-observable'; import { filter, map, tap, ignoreElements } from 'rxjs/operators'; import { isActionOf } from 'typesafe-actions'; @@ -8,13 +8,6 @@ import * as workloadsActions from './actions'; type AppEpic = Epic; - -// import { WorkloadService } from './services'; -// const workloadService = new WorkloadService(); -// workloadService.create({ complexity: 1 }) -// .then(console.log.bind(console, 'workloadService create')); - - const logWorkloadSubmissions: AppEpic = (action$, state$) => ( action$.pipe( filter(isActionOf(workloadsActions.submit)), @@ -29,4 +22,4 @@ export const epics = combineEpics( logWorkloadSubmissions, ); -export default epics; \ No newline at end of file +export default epics; diff --git a/src/state/workloads/services.ts b/src/state/workloads/services.ts index bb64d34..d678a10 100644 --- a/src/state/workloads/services.ts +++ b/src/state/workloads/services.ts @@ -3,18 +3,18 @@ import moment from 'moment'; import { Status } from './types'; -export class WorkloadService { - +export class WorkloadService { + private workLoads: { [key in number]: Work } = {}; private counter = 0; - + private getWorkload(id: number): Work | undefined { - return this.workLoads[id]; + return this.workLoads[id]; } private completeWorkload(work: Work) { - work.status = work.id % 2 + work.status = work.id % 2 ? 'FAILURE' : 'SUCCESS'; } @@ -22,8 +22,8 @@ export class WorkloadService { public create({ complexity }: { complexity: number }) { const id = this.counter++; - const status = 'WORKING'; - + const status: Status = 'WORKING'; + const milliseconds = complexity * 1000; const completeDate = moment().add(complexity, 'second').toDate(); const timer = setTimeout(() => this.completeWorkload(work), milliseconds); @@ -50,7 +50,7 @@ export class WorkloadService { const work = this.getWorkload(id); if (!work) return Promise.reject('Workload not found'); if (work.status !== 'WORKING') return Promise.reject('Workload cannot be canceled'); - + clearTimeout(work.timer) work.status = 'CANCELED';