Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withJob has workingProps state to ignore an old work's late resolve. #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions commonjs/withJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ function withJob(config) {
this.setState({
data: result ? result.data : null,
error: null,
completed: result != null
completed: result != null,
workingProps: null
});
}
}, {
Expand Down Expand Up @@ -171,36 +172,36 @@ function withJob(config) {
this.resolveWork = function (props) {
var workDefinition = void 0;

_this2.setState({ completed: false, data: null, error: null });
_this2.setState({ completed: false, data: null, error: null, workingProps: props });

try {
workDefinition = work(props);
} catch (error) {
_this2.setState({ completed: true, error: error });
_this2.setState({ completed: true, error: error, workingProps: null });
// Ensures asyncBootstrap stops
return false;
}

if ((0, _utils.isPromise)(workDefinition)) {
// Asynchronous result.
return workDefinition.then(function (data) {
if (_this2.unmounted) {
if (_this2.unmounted || _this2.state.workingProps !== props) {
return undefined;
}
_this2.setState({ completed: true, data: data });
_this2.setState({ completed: true, data: data, workingProps: null });
if (_this2.context.jobs) {
_this2.context.jobs.register(id, { data: data });
}
// Ensures asyncBootstrap continues
return true;
}).catch(function (error) {
if (_this2.unmounted) {
if (_this2.unmounted || _this2.state.workingProps !== props) {
return undefined;
}
if (env === 'browser') {
setTimeout(function () {
if (!_this2.unmounted) {
_this2.setState({ completed: true, error: error });
_this2.setState({ completed: true, error: error, workingProps: null });
}
}, 16);
} else {
Expand All @@ -218,7 +219,7 @@ function withJob(config) {
}

// Synchronous result.
_this2.setState({ completed: true, data: workDefinition, error: null });
_this2.setState({ completed: true, data: workDefinition, error: null, workingProps: null });

// Ensures asyncBootstrap continues
return true;
Expand Down
15 changes: 8 additions & 7 deletions src/withJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export default function withJob(config) {
data: result ? result.data : null,
error: null,
completed: result != null,
workingProps: null,
})
}

Expand Down Expand Up @@ -110,12 +111,12 @@ export default function withJob(config) {
resolveWork = (props) => {
let workDefinition

this.setState({ completed: false, data: null, error: null })
this.setState({ completed: false, data: null, error: null, workingProps: props })

try {
workDefinition = work(props)
} catch (error) {
this.setState({ completed: true, error })
this.setState({ completed: true, error, workingProps: null })
// Ensures asyncBootstrap stops
return false
}
Expand All @@ -124,25 +125,25 @@ export default function withJob(config) {
// Asynchronous result.
return workDefinition
.then((data) => {
if (this.unmounted) {
if (this.unmounted || this.state.workingProps !== props) {
return undefined
}
this.setState({ completed: true, data })
this.setState({ completed: true, data, workingProps: null })
if (this.context.jobs) {
this.context.jobs.register(id, { data })
}
// Ensures asyncBootstrap continues
return true
})
.catch((error) => {
if (this.unmounted) {
if (this.unmounted || this.state.workingProps !== props) {
return undefined
}
if (env === 'browser') {
setTimeout(
() => {
if (!this.unmounted) {
this.setState({ completed: true, error })
this.setState({ completed: true, error, workingProps: null })
}
},
16,
Expand All @@ -162,7 +163,7 @@ export default function withJob(config) {
}

// Synchronous result.
this.setState({ completed: true, data: workDefinition, error: null })
this.setState({ completed: true, data: workDefinition, error: null, workingProps: null })

// Ensures asyncBootstrap continues
return true
Expand Down
17 changes: 9 additions & 8 deletions umd/react-jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ function withJob(config) {
this.setState({
data: result ? result.data : null,
error: null,
completed: result != null
completed: result != null,
workingProps: null
});
}
}, {
Expand Down Expand Up @@ -327,36 +328,36 @@ function withJob(config) {
this.resolveWork = function (props) {
var workDefinition = void 0;

_this2.setState({ completed: false, data: null, error: null });
_this2.setState({ completed: false, data: null, error: null, workingProps: props });

try {
workDefinition = work(props);
} catch (error) {
_this2.setState({ completed: true, error: error });
_this2.setState({ completed: true, error: error, workingProps: null });
// Ensures asyncBootstrap stops
return false;
}

if ((0, _utils.isPromise)(workDefinition)) {
// Asynchronous result.
return workDefinition.then(function (data) {
if (_this2.unmounted) {
if (_this2.unmounted || _this2.state.workingProps !== props) {
return undefined;
}
_this2.setState({ completed: true, data: data });
_this2.setState({ completed: true, data: data, workingProps: null });
if (_this2.context.jobs) {
_this2.context.jobs.register(id, { data: data });
}
// Ensures asyncBootstrap continues
return true;
}).catch(function (error) {
if (_this2.unmounted) {
if (_this2.unmounted || _this2.state.workingProps !== props) {
return undefined;
}
if (env === 'browser') {
setTimeout(function () {
if (!_this2.unmounted) {
_this2.setState({ completed: true, error: error });
_this2.setState({ completed: true, error: error, workingProps: null });
}
}, 16);
} else {
Expand All @@ -374,7 +375,7 @@ function withJob(config) {
}

// Synchronous result.
_this2.setState({ completed: true, data: workDefinition, error: null });
_this2.setState({ completed: true, data: workDefinition, error: null, workingProps: null });

// Ensures asyncBootstrap continues
return true;
Expand Down
Loading