Skip to content

Commit 20d781a

Browse files
committed
flat Promise value into state
1 parent 74f0c44 commit 20d781a

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/xclass.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,18 @@ export function genXComponentClass<E extends HKTS, I, S>(WrappedComponent: React
6060
console.log('UPDATE:', action)
6161
this.setState((prevState, props) => {
6262
let newState = action.call(this, prevState, props);
63-
this.context[XREACT_ENGINE].history$.next(newState)
63+
let newStateWithoutPromise = {}
64+
for (let i in newState) {
65+
if (isPromise(newState[i])) {
66+
newState[i].then(v => this.setState(v))
67+
} else {
68+
newStateWithoutPromise[i] = newState[i]
69+
}
70+
}
71+
this.context[XREACT_ENGINE].history$.next(newStateWithoutPromise)
6472
if (process.env.NODE_ENV == 'debug')
65-
console.log('STATE:', newState)
66-
return newState;
73+
console.log('STATE:', newStateWithoutPromise)
74+
return newStateWithoutPromise;
6775
});
6876
} else {
6977
/* istanbul ignore next */
@@ -140,3 +148,7 @@ function pick(names, obj) {
140148
}
141149
return result;
142150
}
151+
152+
function isPromise(p) {
153+
return p !== null && typeof p === 'object' && typeof p.then === 'function'
154+
}

0 commit comments

Comments
 (0)