Skip to content

Instantly share code, notes, and snippets.

@SergProduction
Last active August 25, 2017 10:03
Show Gist options
  • Select an option

  • Save SergProduction/6cc3d83246666972735930ac7d8ebedf to your computer and use it in GitHub Desktop.

Select an option

Save SergProduction/6cc3d83246666972735930ac7d8ebedf to your computer and use it in GitHub Desktop.
cancelable promis and react hoc
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 }
}
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
}
@SergProduction
Copy link
Author

SergProduction commented Aug 22, 2017

use cancelablePromise

import { cancelablePromise } from 'cancelablePromise'

const test = (str) => `test ${str}`

const req = (data)  => {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve(data)
    }, 1000)
  })
}

const a = cancelablePromise(req)
const b = cancelablePromise((data) => req(test(data)))


a('a').then(res => {
  console.log(res)
  b('b').then(console.log)
})

setTimeout(() => {
  a.cancel()  
}, 500)

https://repl.it/KUE8/7

@SergProduction
Copy link
Author

SergProduction commented Aug 22, 2017

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)

https://www.webpackbin.com/bins/-KsE32InAfELP4vPJdij

@SergProduction
Copy link
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