Skip to content

Commit

Permalink
fix(observer): attached & detached in component lifetimes
Browse files Browse the repository at this point in the history
  • Loading branch information
cicec committed Aug 18, 2021
1 parent 2d9cbc0 commit fb072f8
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,39 @@ const observer = {
) => {
let dispose: IReactionDisposer

const { data = {}, attached, detached } = options
const { data = {}, lifetimes } = options

const attached = lifetimes?.attached ?? options.attached
const detached = lifetimes?.attached ?? options.detached

return (observedStores: AnyObject = {}) =>
Component({
...options,
data: { ...data, ...toData(observedStores) },

attached() {
dispose = autorun(() => {
if (this.data) {
const diffs: AnyObject = diff({ ...this.data, ...toData(observedStores) }, this.data)
lifetimes: {
...lifetimes,

this.setData(diffs)
}
})
attached() {
dispose = autorun(() => {
if (this.data) {
const diffs: AnyObject = diff(
{ ...this.data, ...toData(observedStores) },
this.data
)

if (is.fun(attached)) attached.call(this)
},
this.setData(diffs)
}
})

detached() {
if (dispose) dispose()
if (is.fun(attached)) attached.call(this)
},

detached() {
if (dispose) dispose()

if (is.fun(detached)) detached.call(this)
if (is.fun(detached)) detached.call(this)
},
},
})
},
Expand Down

0 comments on commit fb072f8

Please sign in to comment.