1. Dep In fact, this is a container for dependency collection. class Dep{ constructor() { this._subs = []; } depend() { this._subs.push(Dep.target) } notify() { this._subs.forEach(item => { item.fn(); }) } } // Actually, this is the beginning of the love between dep and watcher. // A global property is used in watcher to store watcher Dep.target = null; function pushTarget(watch) { Dep.target = watch; } function popTarget() { Dep.target = null; } 2. Understand obverser
// Convert to accessor property function defineReactive (obj, key, val, shallow) { // Create a dependency collection container let dep = new Dep(); let childOb = !shallow && observe(val) Object.defineProperty(obj, key, { enumerable: true, configurable: true, get: function reactiveGetter () { if(Dep.target) { // Collect dependencies dep.depend(); } return val; // ... }, set: function reactiveSetter (newVal) { if(newVal === val) return; // Continue recursively traversing observe(newVal); //Trigger dependency dep.notify(); // ... } }) } class Observer{ constructor(data) { this.walk(data); } walk(data) { const keys = Object.keys(data) for (let i = 0; i < keys.length; i++) { defineReactive(data, keys[i], data[keys[i]]) } } } // Recursively convert all properties of the data object into accessor properties function observe (data) { if(Object.prototype.toString.call(data) !== '[object Object]') return; new Observer(data); } At this point, you can convert all properties of any object into accessors 3. Understand watch and observerconst data = { a: 1, b: 2 } //First monitor an object observe(data); The main function of class Watcher{ /** * @params {Function} exp a property expression * @params {Function} fn callback */ constructor(exp, fn) { this.exp = exp; this.fn = fn; //Save watcher // Dep.target = this; pushTarget(this); // Execute the expression function once first, and in the calling process, // Trigger the accessor of data.a, and the get of data.a is executed. // Trigger dep.depend() to start collecting dependencies this.exp(); // Release Dep.target popTarget(); } } // new Watcher Such a dependency is collected new Watcher(() => { return data.a + data.b; }, () => { console.log('change') }) 4. Trigger Dependencydata.a = 3; // change data.b = 3; // change 5. Summarize the process
This is the end of this article about implementing a simple data response system. For more relevant data response system content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Introduction to the use of common Dockerfile commands
>>: How to control the proportion of Flex child elements on the main axis
There are two ways to run .sh files in Linux syst...
Table of contents 1. Basic use of css(): 1.1 Get ...
Translucent border Result: Implementation code: &...
Carousel animation can improve the appearance and...
Install MySQL and keep a note. I don’t know if it...
As the computer is used, a lot of garbage will be...
Table of contents Preface What is DrawCall How do...
1. Create a sequence table CREATE TABLE `sequence...
Table of contents 1 Background 2 Create a contain...
1. Docker mounts the local directory Docker can s...
Traditionally, developers create properties in Ja...
1. left(name,4) intercepts the 4 characters on th...
Note: Currently, the more popular front-end frame...
In projects, batch operation statements are often...
1. Documentation Rules 1. Case sensitive. 2. The a...