1 | "use strict";
|
2 |
|
3 | var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
4 | exports.__esModule = true;
|
5 | exports.jobsReducer = void 0;
|
6 | var _get2 = _interopRequireDefault(require("lodash/get"));
|
7 | var _merge2 = _interopRequireDefault(require("lodash/merge"));
|
8 | var _findIndex2 = _interopRequireDefault(require("lodash/findIndex"));
|
9 | var _commonTags = require("common-tags");
|
10 | var _moment = _interopRequireDefault(require("moment"));
|
11 | const jobsReducer = (state = {
|
12 | active: [],
|
13 | done: []
|
14 | }, action) => {
|
15 | switch (action.type) {
|
16 | case `CREATE_JOB`:
|
17 | case `SET_JOB`:
|
18 | {
|
19 | if (!action.payload.id) {
|
20 | throw new Error(`An ID must be provided when creating or setting job`);
|
21 | }
|
22 | const index = (0, _findIndex2.default)(state.active, j => j.id === action.payload.id);
|
23 | if (index !== -1) {
|
24 | const mergedJob = (0, _merge2.default)(state.active[index], {
|
25 | ...action.payload,
|
26 | createdAt: Date.now(),
|
27 | plugin: action.plugin
|
28 | });
|
29 | state.active[index] = mergedJob;
|
30 | return state;
|
31 | } else {
|
32 | state.active.push({
|
33 | ...action.payload,
|
34 | createdAt: Date.now(),
|
35 | plugin: action.plugin
|
36 | });
|
37 | return state;
|
38 | }
|
39 | }
|
40 | case `END_JOB`:
|
41 | {
|
42 | if (!action.payload.id) {
|
43 | throw new Error(`An ID must be provided when ending a job`);
|
44 | }
|
45 | const completedAt = Date.now();
|
46 | const index = (0, _findIndex2.default)(state.active, j => j.id === action.payload.id);
|
47 | if (index === -1) {
|
48 | throw new Error((0, _commonTags.oneLine)`
|
49 | The plugin "${(0, _get2.default)(action, `plugin.name`, `anonymous`)}"
|
50 | tried to end a job with the id "${action.payload.id}"
|
51 | that either hasn't yet been created or has already been ended`);
|
52 | }
|
53 | const job = state.active.splice(index, 1)[0];
|
54 | state.done.push({
|
55 | ...job,
|
56 | completedAt,
|
57 | runTime: (0, _moment.default)(completedAt).diff((0, _moment.default)(job.createdAt))
|
58 | });
|
59 | return state;
|
60 | }
|
61 | }
|
62 | return state;
|
63 | };
|
64 | exports.jobsReducer = jobsReducer;
|
65 |
|
\ | No newline at end of file |