Last active
August 25, 2017 10:03
-
-
Save SergProduction/6cc3d83246666972735930ac7d8ebedf to your computer and use it in GitHub Desktop.
cancelable promis and react hoc
This file contains hidden or 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
| export const cancelablePromise = (promise) => { | |
| let state = false | |
| const prom = (...param) => | |
| new Promise((resolve, reject) => { | |
| promise(...param) | |
| .then((res) => { | |
| if (!state) resolve(res) | |
| }) | |
| .catch((err) => { | |
| if (!state) reject(err) | |
| }) | |
| }) | |
| prom.cancel = () => { | |
| state = true | |
| } | |
| return prom | |
| } | |
| export const cancelablePromiseAll = (promisesMap) => { | |
| const withCancPromise = Object.keys(promisesMap).map(key => ( | |
| { [key]: cancelablePromise(promisesMap[key]) } | |
| )) | |
| const propMaps = Object.assign(...withCancPromise) | |
| const cancel = () => { | |
| Object.keys(propMaps).forEach(key => propMaps[key].cancel()) | |
| } | |
| return { ...propMaps, cancel } | |
| } |
This file contains hidden or 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
| import React, { Component } from 'react' | |
| import { cancelablePromise } from 'cancelablePromise' | |
| export const cancable = promisesMap => (WrappedComponent) => { | |
| class CancPromise extends Component { | |
| state = { propMaps: {} } | |
| componentWillMount() { | |
| const withCancPromise = Object.keys(promisesMap).map(key => ( | |
| { [key]: cancelablePromise(promisesMap[key]) } | |
| )) | |
| const propMaps = Object.assign(...withCancPromise) | |
| this.setState({ propMaps }) | |
| } | |
| componentWillUnmount() { | |
| this.cancel() | |
| } | |
| cancel = () => { | |
| const { propMaps } = this.state | |
| Object.keys(propMaps).forEach(key => propMaps[key].cancel()) | |
| } | |
| render() { | |
| return ( | |
| <WrappedComponent cancel={this.cancel} {...this.state.propMaps} {...this.props} /> | |
| ) | |
| } | |
| } | |
| CancPromise.displayName = `withCancelablePromise(${WrappedComponent.displayName || WrappedComponent.name || 'Component'})` | |
| return CancPromise | |
| } |
Author
Author
use hoc react
import React, { Component } from 'react'
import { cancelable } from 'cancelablePromise'
class Timer extends React.Component {
state = {text: ''}
componentWillMount() {
this.props.startTime(2, 'TADA')
.then((res) => {
this.setState({ text: res })
})
}
stop = () => {
this.props.cancel()
}
render() {
return (
<div>
<h1>{this.state.text}</h1>
<button onClick={this.stop}> cancel Timer </button>
</div>
)
}
}
const timer = (seconds, text) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(text)
}, seconds * 1000)
})
}
export default cancelable({
startTime: timer
})(Timer)
Author
Suppression at the root
https://repl.it/KUE8/5
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use cancelablePromise
https://repl.it/KUE8/7