-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed a bug with capturing the progress of a generator with nested ch…
…ains; Added error code filter to the `isCanceledError` method;
- Loading branch information
1 parent
12e9411
commit 7d1b6ae
Showing
5 changed files
with
109 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,84 @@ | ||
import React, {Component} from "react"; | ||
import {CPromise, CanceledError, ReactComponent, E_REASON_UNMOUNTED, async, listen, cancel, progress, canceled} from "../../lib/c-promise"; | ||
import React, { Component } from "react"; | ||
import { | ||
CPromise, | ||
CanceledError, | ||
ReactComponent, | ||
E_REASON_UNMOUNTED, | ||
listen, | ||
async, | ||
cancel | ||
} from "c-promise2"; | ||
import { Spinner } from "react-bootstrap"; | ||
import cpAxios from "cp-axios"; | ||
|
||
class ProtoComponent extends Component{ | ||
onClick2(){ | ||
console.log('ProtoComponent::click2', this); | ||
} | ||
} | ||
|
||
@ReactComponent | ||
class TestComponent extends ProtoComponent{ | ||
class TestComponent extends Component { | ||
state = { | ||
text: "" | ||
text: "idle", | ||
loading: false, | ||
loader: false | ||
}; | ||
|
||
*componentDidMount(scope) { | ||
console.log('mount', scope); | ||
scope.onCancel((err)=> console.log(`Cancel: ${err}`)); | ||
yield CPromise.delay(3000); | ||
*componentDidMount() { | ||
console.log("mounted"); | ||
yield this.fetch(); | ||
} | ||
|
||
onClick2(){ | ||
console.log('TestComponent::click2', this); | ||
super.onClick2(); | ||
* fetch() { | ||
this.setState({text: "fetching...", loading: true}); | ||
yield CPromise.race([ | ||
this.showLoader(1000, 'loader'), | ||
CPromise.run(function* () { | ||
try { | ||
const response = yield cpAxios(this.props.url).timeout( | ||
this.props.timeout | ||
); | ||
this.setState({ | ||
text: JSON.stringify(response.data, null, 2), | ||
loading: false | ||
}); | ||
} catch (err) { | ||
CanceledError.rethrow(err, E_REASON_UNMOUNTED); | ||
this.setState({text: err.toString(), loading: false}); | ||
} | ||
}, {context: this}) | ||
]) | ||
} | ||
|
||
@listen | ||
*fetch(){ | ||
this.setState({text: "fetching..."}); | ||
try { | ||
const response = yield cpAxios(this.props.url).timeout(this.props.timeout); | ||
this.setState({text: JSON.stringify(response.data, null, 2)}); | ||
} catch (err) { | ||
CanceledError.rethrow(err, E_REASON_UNMOUNTED); | ||
this.setState({text:err.toString()}); | ||
} | ||
@async({ scopeArg: true }) | ||
*showLoader(scope, delay= 1000, stateVar) { | ||
yield CPromise.delay(delay); | ||
this.setState({ [stateVar]: true }); | ||
scope.onDone(()=>{ | ||
this.setState({ [stateVar]: false }); | ||
}) | ||
scope.pause(); | ||
} | ||
|
||
render() { | ||
return <div className="component"> | ||
<div className="caption">useAsyncEffect demo:</div> | ||
<div>{this.state.text}</div> | ||
<button | ||
className="btn btn-success" | ||
onClick={this.fetch} | ||
> | ||
Fetch data | ||
</button> | ||
<button className="btn btn-warning" onClick={()=>cancel.call(this, 'oops!')}> | ||
Cancel request | ||
</button> | ||
<button className="btn btn-info" onClick={this.onClick2}>Listener binding test</button> | ||
</div> | ||
return ( | ||
<div className="component"> | ||
<div className="caption">CPromise decorators demo:</div> | ||
<div> | ||
{this.state.loader ? ( | ||
<Spinner animation="border" variant="primary" /> | ||
) : null} | ||
{this.state.text} | ||
</div> | ||
<button className="btn btn-success" onClick={() => this.fetch()}> | ||
Fetch data | ||
</button> | ||
<button | ||
className="btn btn-warning" | ||
onClick={() => cancel.call(this, "oops!")} | ||
> | ||
Cancel request | ||
</button> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default TestComponent; | ||
console.log("componentDidMount", TestComponent.prototype.componentDidMount); | ||
|
||
export default TestComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters