From 42f8ac057ec70c508232339016cc7249123f0fd0 Mon Sep 17 00:00:00 2001 From: urugator <11457665+urugator@users.noreply.github.com> Date: Sat, 7 Jan 2023 12:44:14 +0100 Subject: [PATCH] fix (#3596) --- .changeset/bright-pianos-divide.md | 5 +++++ .../mobx/__tests__/v5/base/become-observed.ts | 17 +++++++++++++++++ packages/mobx/src/types/observableset.ts | 2 +- packages/mobx/src/types/type-utils.ts | 2 +- 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 .changeset/bright-pianos-divide.md diff --git a/.changeset/bright-pianos-divide.md b/.changeset/bright-pianos-divide.md new file mode 100644 index 000000000..893f63f71 --- /dev/null +++ b/.changeset/bright-pianos-divide.md @@ -0,0 +1,5 @@ +--- +"mobx": patch +--- + +fix #3595 onBecomeObserved is not called for ObservableSet diff --git a/packages/mobx/__tests__/v5/base/become-observed.ts b/packages/mobx/__tests__/v5/base/become-observed.ts index d643ab63d..32d42d346 100644 --- a/packages/mobx/__tests__/v5/base/become-observed.ts +++ b/packages/mobx/__tests__/v5/base/become-observed.ts @@ -509,3 +509,20 @@ test("#2667", () => { "onBecomeUnobservednew" ]) }) + +test("works with ObservableSet #3595", () => { + const onSetObserved = jest.fn() + const onSetUnobserved = jest.fn() + + const set = observable.set() + + const disposeOBO = onBecomeObserved(set, onSetObserved) + const disposeOBU = onBecomeUnobserved(set, onSetUnobserved) + const diposeAutorun = autorun(() => set.size) + diposeAutorun() + disposeOBO() + disposeOBU() + + expect(onSetObserved).toBeCalledTimes(1) + expect(onSetUnobserved).toBeCalledTimes(1) +}) diff --git a/packages/mobx/src/types/observableset.ts b/packages/mobx/src/types/observableset.ts index 05171775a..16c797b4f 100644 --- a/packages/mobx/src/types/observableset.ts +++ b/packages/mobx/src/types/observableset.ts @@ -65,7 +65,7 @@ export type ISetWillChange = export class ObservableSet implements Set, IInterceptable, IListenable { [$mobx] = ObservableSetMarker private data_: Set = new Set() - private atom_: IAtom + atom_: IAtom changeListeners_ interceptors_ dehancer: any diff --git a/packages/mobx/src/types/type-utils.ts b/packages/mobx/src/types/type-utils.ts index a85dd327d..161ca8bc3 100644 --- a/packages/mobx/src/types/type-utils.ts +++ b/packages/mobx/src/types/type-utils.ts @@ -22,7 +22,7 @@ export function getAtom(thing: any, property?: PropertyKey): IDepTreeNode { return (thing as any)[$mobx].atom_ } if (isObservableSet(thing)) { - return (thing as any)[$mobx] + return thing.atom_ } if (isObservableMap(thing)) { if (property === undefined) {