Skip to content

observe javascript object and notify modifications

License

Notifications You must be signed in to change notification settings

lv-saharan/observing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

observing

observe javascript object and notify modifications

observe a object

let user = {
    name: "user",
    age: 18,
    likes: ["footboall", "video", "game"],
    girlFriend: {
        name: "sara",
        age: 17
    }
}
//root observed
const observedUser = observe(user, ({ path, oldVal, val }) => {
    console.log("user changed", path, oldVal, val)
})

//observe child object then 


const observedGrilFriend = observedUser.girlFriend


observe(observedGrilFriend, ({ path, oldVal, val }) => {
    console.log("girl friend changed", path, oldVal, val)

})

//proxy can use addCallbacks function  to add new callback!
observedGrilFriend.addCallbacks(({ path, oldVal, val }) => {
    console.log("proxy girl friend changed!!!", path, oldVal, val)

})

unobserve

unobserve(observedUser)

//|or just remove some callbacks
 unobserve(callback1,callback2,...)

About

observe javascript object and notify modifications

Resources

License

Stars

Watchers

Forks

Packages

No packages published