Skip to content

Latest commit

 

History

History
37 lines (26 loc) · 955 Bytes

File metadata and controls

37 lines (26 loc) · 955 Bytes

1.3 Mini Observer

Goal

Combine the previous two functions, renaming convert() to observe() and keeping autorun():

  • observe() converts the properties in the received object and make them reactive. For each converted property, it gets assigned a Dep instance which keeps track of a list of subscribing update functions, and triggers them to re-run when its setter is invoked.

  • autorun() takes an update function and re-runs it when properties that the update function subscribes to have been mutated. An update function is said to be "subscribing" to a property if it relies on that property during its evaluation.

They should support the following usage:

const state = {
  count: 0
}

observe(state)

autorun(() => {
  console.log(state.count)
})
// should immediately log "count is: 0"

state.count++
// should log "count is: 1"

To test if your implementation is correct, run:

npm test -- -t 1.3