Skip to content

Commit

Permalink
Merge pull request #311 from laszlopandy/fix-fromPromise-from-pending…
Browse files Browse the repository at this point in the history
…-promise
  • Loading branch information
urugator authored Nov 10, 2022
2 parents f4e2cc8 + 2bbd0ac commit 1a53977
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 6.0.6

* [fromPromise carries forward old value from pending observable](https://github.com/mobxjs/mobx-utils/pull/311)

# 6.0.5

# 6.0.4

* [ObservableGroupMap.ts: remove console.log #289](https://github.com/mobxjs/mobx-utils/pull/289)
Expand Down
5 changes: 2 additions & 3 deletions src/from-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,9 @@ export function fromPromise<T>(

promise.isPromiseBasedObservable = true
promise.case = caseImpl
const oldState = oldPromise ? (oldPromise as any).state : undefined
const oldData =
oldPromise && (oldPromise as any).state === FULFILLED
? (oldPromise as any).value
: undefined
oldState === FULFILLED || oldState === PENDING ? (oldPromise as any).value : undefined
extendObservable(
promise,
{
Expand Down
26 changes: 14 additions & 12 deletions test/from-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,20 @@ test("old state is undefined", (done) => {
)
})

test("resolves old state", (done) => {
const oldP = utils.fromPromise(new Promise((resolve) => resolve(9)))
mobx.when(
() => oldP.state == "fulfilled",
() => {
const p = new Promise((resolve) => resolve(7))
const obs = utils.fromPromise(p, oldP)
expect(obs.value).toBe(9)
expect(obs.state).toBe("pending")
done()
}
)
test("resolves old state", () => {
let obs = utils.fromPromise.resolve(9)
expect(obs.value).toBe(9)
expect(obs.state).toBe("fulfilled")

// Expect old state to be carried forward from fulfilled observable
obs = utils.fromPromise(Promise.resolve(7), obs)
expect(obs.value).toBe(9)
expect(obs.state).toBe("pending")

// Expect old state to be carried forward from *pending* observable
obs = utils.fromPromise(Promise.resolve(10), obs)
expect(obs.value).toBe(9)
expect(obs.state).toBe("pending")
})

test("resolves new state", (done) => {
Expand Down

0 comments on commit 1a53977

Please sign in to comment.