Skip to content

Commit d3ce078

Browse files
split top level resolving logic and iterator resolving strategies
1 parent f8b3834 commit d3ce078

File tree

1 file changed

+42
-44
lines changed

1 file changed

+42
-44
lines changed

modules/Coroutine.js

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function create(asyncFn) {
1313
observe(props) {
1414
return asyncFn(props);
1515
}
16-
}
16+
};
1717
}
1818

1919
class Coroutine extends Component {
@@ -33,50 +33,22 @@ class Coroutine extends Component {
3333

3434
this.iterator = asyncBody;
3535

36-
if (typeof asyncBody.then === 'function') {
37-
asyncBody.then(data => {
38-
if (this.isComponentMounted && this.iterator === asyncBody) {
39-
this.setState(() => ({ data }));
40-
}
41-
});
36+
const updater = data => {
37+
if (this.isComponentMounted && this.iterator === asyncBody) {
38+
this.setState({ data });
39+
}
40+
};
41+
42+
if (isPromiseLike(asyncBody)) {
43+
asyncBody.then(updater);
4244
} else {
43-
function resolveSyncIterator(i, step, cb) {
44-
if (!step.done) {
45-
if (step.value && typeof step.value.then === 'function') {
46-
step.value.then(data => resolveSyncIterator(i, i.next(data), cb));
47-
} else {
48-
resolveSyncIterator(i, i.next(step.value), cb);
49-
}
50-
} else {
51-
cb(step.value);
52-
}
53-
};
54-
55-
function resolveAsyncIterator(i, step, cb) {
56-
step.then((data) => {
57-
if (data.value !== undefined) {
58-
cb(data.value);
59-
}
60-
61-
return !data.done && resolveAsyncIterator(i, i.next(), cb);
62-
});
63-
};
64-
65-
function resolveIterator(iterator, instance) {
66-
const step = iterator.next();
67-
68-
const updater = (data) => {
69-
return instance.isComponentMounted && instance.setState({ data });
70-
};
71-
72-
if (typeof step.then === 'function') {
73-
resolveAsyncIterator(iterator, step, updater);
74-
} else {
75-
resolveSyncIterator(iterator, step, updater);
76-
}
77-
};
78-
79-
resolveIterator(this.iterator, this);
45+
const step = this.iterator.next();
46+
47+
if (isPromiseLike(step)) {
48+
resolveAsyncIterator(this.iterator, step, updater);
49+
} else {
50+
resolveSyncIterator(this.iterator, step, updater);
51+
}
8052
}
8153
}
8254

@@ -109,3 +81,29 @@ class Coroutine extends Component {
10981
return this.state.data;
11082
}
11183
}
84+
85+
function resolveSyncIterator(i, step, cb) {
86+
if (!step.done) {
87+
if (isPromiseLike(step.value)) {
88+
step.value.then(data => resolveSyncIterator(i, i.next(data), cb));
89+
} else {
90+
resolveSyncIterator(i, i.next(step.value), cb);
91+
}
92+
} else {
93+
cb(step.value);
94+
}
95+
}
96+
97+
function resolveAsyncIterator(i, step, cb) {
98+
step.then(data => {
99+
if (data.value !== undefined) {
100+
cb(data.value);
101+
}
102+
103+
return !data.done && resolveAsyncIterator(i, i.next(), cb);
104+
});
105+
}
106+
107+
function isPromiseLike(target) {
108+
return target && typeof target.then === 'function';
109+
}

0 commit comments

Comments
 (0)