Skip to content

Commit dbacaee

Browse files
committed
Avoid using static class properties for better interop.
1 parent 8c85c4d commit dbacaee

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

src/index.js

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ export const createInstance = (defaultProps = {}) => {
1313
constructor(props) {
1414
super(props)
1515

16+
this.load = this.load.bind(this)
17+
this.run = this.run.bind(this)
18+
this.cancel = this.cancel.bind(this)
19+
this.onResolve = this.onResolve.bind(this)
20+
this.onReject = this.onReject.bind(this)
21+
this.setData = this.setData.bind(this)
22+
this.setError = this.setError.bind(this)
23+
1624
const promiseFn = props.promiseFn || defaultProps.promiseFn
1725
const initialValue = props.initialValue || defaultProps.initialValue
1826
const initialError = initialValue instanceof Error ? initialValue : undefined
@@ -35,7 +43,7 @@ export const createInstance = (defaultProps = {}) => {
3543
this.run(...this.args)
3644
},
3745
setData: this.setData,
38-
setError: this.setError
46+
setError: this.setError,
3947
}
4048
}
4149

@@ -57,50 +65,57 @@ export const createInstance = (defaultProps = {}) => {
5765
this.mounted = false
5866
}
5967

60-
load = () => {
68+
load() {
6169
const promiseFn = this.props.promiseFn || defaultProps.promiseFn
6270
if (!promiseFn) return
6371
this.counter++
6472
this.setState({ isLoading: true, startedAt: new Date(), finishedAt: undefined })
6573
return promiseFn(this.props).then(this.onResolve(this.counter), this.onReject(this.counter))
6674
}
6775

68-
run = (...args) => {
76+
run(...args) {
6977
const deferFn = this.props.deferFn || defaultProps.deferFn
7078
if (!deferFn) return
7179
this.counter++
7280
this.args = args
7381
this.setState({ isLoading: true, startedAt: new Date(), finishedAt: undefined })
74-
return deferFn(...args, this.props).then(this.onResolve(this.counter), this.onReject(this.counter))
82+
return deferFn(...args, this.props).then(
83+
this.onResolve(this.counter),
84+
this.onReject(this.counter)
85+
)
7586
}
7687

77-
cancel = () => {
88+
cancel() {
7889
this.counter++
7990
this.setState({ isLoading: false, startedAt: undefined })
8091
}
8192

82-
onResolve = counter => data => {
83-
if (this.mounted && this.counter === counter) {
84-
const onResolve = this.props.onResolve || defaultProps.onResolve
85-
this.setData(data, () => onResolve && onResolve(data))
93+
onResolve(counter) {
94+
return data => {
95+
if (this.mounted && this.counter === counter) {
96+
const onResolve = this.props.onResolve || defaultProps.onResolve
97+
this.setData(data, () => onResolve && onResolve(data))
98+
}
99+
return data
86100
}
87-
return data
88101
}
89102

90-
onReject = counter => error => {
91-
if (this.mounted && this.counter === counter) {
92-
const onReject = this.props.onReject || defaultProps.onReject
93-
this.setError(error, () => onReject && onReject(error))
103+
onReject(counter) {
104+
return error => {
105+
if (this.mounted && this.counter === counter) {
106+
const onReject = this.props.onReject || defaultProps.onReject
107+
this.setError(error, () => onReject && onReject(error))
108+
}
109+
return error
94110
}
95-
return error
96111
}
97112

98-
setData = (data, callback) => {
113+
setData(data, callback) {
99114
this.setState({ data, error: undefined, isLoading: false, finishedAt: new Date() }, callback)
100115
return data
101116
}
102117

103-
setError = (error, callback) => {
118+
setError(error, callback) {
104119
this.setState({ error, isLoading: false, finishedAt: new Date() }, callback)
105120
return error
106121
}

0 commit comments

Comments
 (0)