Skip to content

Commit

Permalink
Add the progressDelay task start option
Browse files Browse the repository at this point in the history
  • Loading branch information
peterhudec committed Jul 17, 2024
1 parent fb21924 commit 7fe27e1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/client/components/Task/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import Err from './Error'
import ProgressIndicator from '../ProgressIndicator'
import AccessDenied from '../AccessDenied'

const DEFAULT_PROGRESS_DELAY = 100

const StyledLoadingBox = styled(LoadingBox)({
paddingBottom: 0,
})
Expand Down Expand Up @@ -45,6 +47,10 @@ const startOnRenderPropTypes = {
* result of a _tasks_ success. If set, a {SuccessAction} with this value
* as its `type` will be dispatched when the _task_ succeeds, but before the
* _task_ is _cleared_.
* @property {number} [progressDelay=100] - The duration in milliseconds
* for which switching a task to a progress state should be delayed.
* This is to avoid showing the progress indicator for tasks which resolve
* immediately.
*/

/**
Expand Down Expand Up @@ -122,13 +128,18 @@ const startOnRenderPropTypes = {
const Task = connect(
(state) => state.tasks,
(dispatch) => ({
start: (name, id, { payload, onSuccessDispatch }) =>
start: (
name,
id,
{ payload, onSuccessDispatch, progressDelay = DEFAULT_PROGRESS_DELAY }
) =>
dispatch({
type: TASK__START,
payload,
id,
name,
onSuccessDispatch,
progressDelay,
}),
cancel: (name, id) =>
dispatch({
Expand Down Expand Up @@ -174,11 +185,13 @@ Task.propTypes = {
* @example
* <Task.StartOnRender name="foo" id="a" payload={123} onSuccessDispatch="FOO"/>
*/
// TODO: Re-implement with <Task/>
Task.StartOnRender = connect(
(state, { name, id }) => get(state, ['tasks', name, id], {}),
(dispatch, { id, name }) => ({
start: (options) =>
dispatch({
progressDelay: DEFAULT_PROGRESS_DELAY,
...options,
type: TASK__START,
id,
Expand Down
10 changes: 9 additions & 1 deletion src/client/components/Task/saga.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
call,
select,
cancel,
delay,
} from 'redux-saga/effects'

import {
Expand All @@ -18,15 +19,22 @@ import {
TASK__CANCEL,
} from '../../actions'

function* switchToProgressAfterDelay(action) {
yield delay(action.progressDelay || 0)
yield put({ ...action, type: TASK__PROGRESS })
}

/**
* Starts a task and waits for its resolution or rejection
* @param {Task} task - the task function
* @param {TaskStartAction} action - the `TASK__START` action
*/
function* startTask(task, action) {
yield put({ ...action, type: TASK__PROGRESS })
const progressDelayProcess = yield fork(switchToProgressAfterDelay, action)

try {
const result = yield call(task, action.payload, action.id)
yield cancel(progressDelayProcess)
const { id, name, payload, onSuccessDispatch } = action
if (onSuccessDispatch) {
yield put({
Expand Down

0 comments on commit 7fe27e1

Please sign in to comment.