Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export deepObserve's IChange with more specific name #322

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/deepObserve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -46,24 +46,27 @@ 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)
* })
*/
export function deepObserve<T = any>(
target: T,
listener: (change: IChange, path: string, root: T) => void
listener: (change: IDeepDidChange, path: string, root: T) => void
): IDisposer {
const entrySet = new WeakMap<any, Entry>()

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
Expand Down
6 changes: 3 additions & 3 deletions test/deepObserve.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down