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
To implement the "Enter != Submit" probl...
Before reading this article, I hope you have a ba...
Table of contents Multiple uses of MySQL Load Dat...
Table of contents 1. Install and create an instan...
By default, the width and height of the header ar...
1. Download mysql-5.7.21-windowx64.zip from the o...
Table of contents MySQL query tree structure 1. A...
1. Purpose: Make the code easier to maintain and ...
This article describes the MySQL integrity constr...
Introduction to CentOS CentOS is an enterprise-cl...
During the project development yesterday, I encoun...
This article example shares the specific code of ...
1. Virtual Machine Side 1. Find the mysql configu...
1. Always use :key in v-for Using the key attribu...
Confluence is paid, but it can be cracked for use...