Skip to content

Commit

Permalink
feat(reactive): support observe dynamic tree (#2965)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Mar 20, 2022
1 parent 060cce8 commit 631385d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 28 deletions.
5 changes: 2 additions & 3 deletions packages/reactive/src/__tests__/observable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ test('observable contains', () => {
expect(contains(obj, obj.other)).toBe(true)
expect(contains(obj, other)).toBe(true)

// Are these expected behaviors?
expect(contains(obj, obj.arr)).toBe(false)
expect(contains(obj, arr)).toBe(false)
expect(contains(obj, obj.arr)).toBe(true)
expect(contains(obj, arr)).toBe(true)
})
35 changes: 10 additions & 25 deletions packages/reactive/src/__tests__/observe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,6 @@ test('shallow observe', () => {
expect(handler).toHaveBeenCalledTimes(2)
})

// test('auto dispose observe', () => {
// const obs = observable<any>({
// aa: {
// bb: {
// cc: [11, 22, 33],
// },
// },
// })
// const handler = jest.fn()
// observe(obs, handler)
// observe(obs.aa, handler)
// observe(obs.aa.bb, handler)
// expect(getDeepObservers(obs.aa).length).toEqual(1)
// expect(getDeepObservers(obs.aa.bb).length).toEqual(1)
// obs.aa.bb = { kk: 'mm' }
// expect(getDeepObservers(obs.aa.bb).length).toEqual(1)
// expect(getDeepObservers(obs.aa).length).toEqual(1)
// expect(getObservers(obs.aa).length).toEqual(0)
// expect(handler).toBeCalledTimes(3)
// observe(obs.aa, handler)
// expect(getObservers(obs.aa).length).toEqual(0)
// expect(getDeepObservers(obs.aa).length).toEqual(2)
// delete obs.aa
// })

test('root replace observe', () => {
const obs = observable<any>({
aa: {
Expand Down Expand Up @@ -160,3 +135,13 @@ test('array delete', () => {

dispose()
})

test('observe dynamic tree', () => {
const handler = jest.fn()
const tree = observable<any>({})
const childTree = observable({})
tree.children = childTree
observe(tree, handler)
tree.children.aa = 123
expect(handler).toBeCalledTimes(1)
})
1 change: 1 addition & 0 deletions packages/reactive/src/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const createObservable = (
const raw = ProxyRaw.get(value)
if (!!raw) {
const node = getDataNode(raw)
if (!node.target) node.target = target
node.key = key
return value
}
Expand Down

0 comments on commit 631385d

Please sign in to comment.