From b8cc6a85b8afc1014c02c8b70fb7190c3614a142 Mon Sep 17 00:00:00 2001 From: Andrew Armbruster Date: Sun, 27 Aug 2023 17:46:35 +0200 Subject: [PATCH 1/2] Export supported change types alias with more specific name Export the type alias that contains information on the types of changes currently supported by `deepObserve`. Also rename it to be more specific and reduce the risk that it is mistaken for something coming from the main MobX library. --- src/deepObserve.ts | 8 ++++---- test/deepObserve.ts | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/deepObserve.ts b/src/deepObserve.ts index 316b63c..432e695 100644 --- a/src/deepObserve.ts +++ b/src/deepObserve.ts @@ -11,7 +11,7 @@ import { } from "mobx" import { IDisposer } from "./utils" -type IChange = IObjectDidChange | IArrayDidChange | IMapDidChange +export type IDeepDidChange = IObjectDidChange | IArrayDidChange | IMapDidChange type Entry = { dispose: IDisposer @@ -53,17 +53,17 @@ function isRecursivelyObservable(thing: any) { */ export function deepObserve( target: T, - listener: (change: IChange, path: string, root: T) => void + listener: (change: IDeepDidChange, path: string, root: T) => void ): IDisposer { const entrySet = new WeakMap() - function genericListener(change: IChange) { + function genericListener(change: IDeepDidChange) { const entry = entrySet.get(change.object)! processChange(change, entry) listener(change, buildPath(entry), target) } - function processChange(change: IChange, parent: Entry) { + function processChange(change: IDeepDidChange, parent: Entry) { switch (change.type) { // Object changes case "add": // also for map diff --git a/test/deepObserve.ts b/test/deepObserve.ts index 350ccb9..40986b1 100644 --- a/test/deepObserve.ts +++ b/test/deepObserve.ts @@ -1,8 +1,8 @@ -import { deepObserve } from "../src/mobx-utils" -import { observable, $mobx, IObjectDidChange } from "mobx" +import { deepObserve, IDeepDidChange } from "../src/mobx-utils" +import { observable, $mobx } from "mobx" import * as cloneDeepWith from "lodash.clonedeepwith" -function cleanChange(change, includeObject = true) { +function cleanChange(change: IDeepDidChange, includeObject = true) { return cloneDeepWith(change, (value, key) => { if (key === $mobx) return null if (key === "object" && !includeObject) return null From f3a3579b5ad650fb63f029f6d0cfd8598e9a01bc Mon Sep 17 00:00:00 2001 From: Andrew Armbruster Date: Sun, 27 Aug 2023 18:26:51 +0200 Subject: [PATCH 2/2] Document deepObserve parameters Document types and descriptions, extend example. --- src/deepObserve.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/deepObserve.ts b/src/deepObserve.ts index 432e695..3844a94 100644 --- a/src/deepObserve.ts +++ b/src/deepObserve.ts @@ -46,8 +46,11 @@ function isRecursivelyObservable(thing: any) { * * deepObserve cannot be used on computed values. * + * @param {Object} target The object to observe. + * @param {function} listener Invoked when a change is detected. + * * @example - * const disposer = deepObserve(target, (change, path) => { + * const disposer = deepObserve(target, (change, path, root) => { * console.dir(change) * }) */