From e554e27095fb2a543ba7085e5481e098502d20af Mon Sep 17 00:00:00 2001 From: Janne Savolainen Date: Mon, 7 Feb 2022 07:39:17 +0200 Subject: [PATCH] Fix observation of asyncComputed when invalidated before ever observed --- .../injectable-react/src/asyncComputed/asyncComputed.js | 3 ++- .../src/asyncComputed/asyncComputed.test.js | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/injectable-react/src/asyncComputed/asyncComputed.js b/packages/injectable-react/src/asyncComputed/asyncComputed.js index d1105434..0ead6e25 100644 --- a/packages/injectable-react/src/asyncComputed/asyncComputed.js +++ b/packages/injectable-react/src/asyncComputed/asyncComputed.js @@ -1,3 +1,4 @@ +import { noop } from 'lodash/fp'; import { computed, createAtom, observable, runInAction, untracked } from 'mobx'; const neutralizeObsoletePromiseSymbol = Symbol.for( @@ -9,7 +10,7 @@ export default (getObservedPromise, pendingValue) => { const pendingBox = observable.box(false); - let neutralizeObsoletePromise; + let neutralizeObsoletePromise = noop; const syncValueBox = observable.box(pendingValue, { name: 'sync-value-box-for-async-computed', diff --git a/packages/injectable-react/src/asyncComputed/asyncComputed.test.js b/packages/injectable-react/src/asyncComputed/asyncComputed.test.js index b6f9f9e4..ec53cbb4 100644 --- a/packages/injectable-react/src/asyncComputed/asyncComputed.test.js +++ b/packages/injectable-react/src/asyncComputed/asyncComputed.test.js @@ -20,6 +20,14 @@ describe('asyncComputed', () => { }, 'some-pending-value'); }); + it('given invalidated before observation, when observed, does not throw', () => { + someAsyncComputed.invalidate(); + + expect(() => { + observe(someAsyncComputed.value, () => {}); + }).not.toThrow(); + }); + describe('when only status is observed but not value', () => { beforeEach(() => { observe(someAsyncComputed.pending, () => {});