JS to monitor the change of class attributes, generally can use the following callback function or event listener, but the callback function can only exist a listener, event listener is very cumbersome. So, we use RXJS for reactive programming
player.roleLevelChange = value= > {}
Copy the code
EventEmitter.addListener('RoleLevelChange'.value= > {})
Copy the code
Rxjs
yarn add rxjs
Copy the code
import Rx from 'rxjs'
Copy the code
class Player {
constructor() {
this.roleLevelObservable = new Rx.BehaviorSubject(undefined) // The parameter is the initial value
}
set roleLevel(value) {
this.roleLevelObservable.next(value)
}
}
Copy the code
player.roleLevelObservable.subscribe(value= > {
console.log('Subscribing current role level is 1:${value}`)
})
player.roleLevelObservable.subscribe(value= > {
console.log('Subscribe current role level is 2:${value}`)})Copy the code
See: RxJS Asynchronous communication Subject and BehaviorSubject