From a3ec6b1f095bffe4875d7e688f7f30f22530c45e Mon Sep 17 00:00:00 2001 From: "wayne.zhou" Date: Sun, 11 Aug 2019 16:08:41 +0800 Subject: [PATCH] fix: onBecome(Un)Observed didn't trigger when using number as key of observable map. --- CHANGELOG.md | 2 ++ src/api/become-observed.ts | 4 ++-- test/base/become-observed.js | 13 +++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 test/base/become-observed.js diff --git a/CHANGELOG.md b/CHANGELOG.md index f647858bf..e6ebd5f4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # 5.13.1 / 4.13.1 * Don't use `global` and `self` keywords unless defined. Fixes [#2070](https://github.com/mobxjs/mobx/issues/2070). +* onBecome(Un)Observed didn't trigger when using number as key of observable map. Fixes [#2067](https://github.com/mobxjs/mobx/issues/2067). + # 5.13.0 / 4.13.0 diff --git a/src/api/become-observed.ts b/src/api/become-observed.ts index 968006fa4..aff3306cc 100644 --- a/src/api/become-observed.ts +++ b/src/api/become-observed.ts @@ -47,8 +47,8 @@ export function onBecomeUnobserved(thing, arg2, arg3?): Lambda { function interceptHook(hook: "onBecomeObserved" | "onBecomeUnobserved", thing, arg2, arg3) { const atom: IObservable = - typeof arg2 === "string" ? getAtom(thing, arg2) : (getAtom(thing) as any) - const cb = typeof arg2 === "string" ? arg3 : arg2 + typeof arg3 === "function" ? getAtom(thing, arg2) : (getAtom(thing) as any) + const cb = typeof arg3 === "function" ? arg3 : arg2 const listenersKey = `${hook}Listeners` as | "onBecomeObservedListeners" | "onBecomeUnobservedListeners" diff --git a/test/base/become-observed.js b/test/base/become-observed.js new file mode 100644 index 000000000..a282fe22d --- /dev/null +++ b/test/base/become-observed.js @@ -0,0 +1,13 @@ +import { autorun, onBecomeObserved, observable } from "../../src/mobx" + +describe("become-observed", () => { + it("work on map with number as key", () => { + const oMap = observable.map() + const key = 1 + oMap.set(key, observable.box("value")) + const cb = jest.fn() + onBecomeObserved(oMap, key, cb) + autorun(() => oMap.get(key)) + expect(cb).toBeCalled() + }) +})