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
This article example shares the specific code of ...
Table of contents 1. What is Javascript? 2. What ...
Table of contents What is a skeleton screen? Demo...
Table of contents Overview console.log console.in...
1. Change the virtual machine hardware version in...
BEM from QQtabBar First of all, what does BEM mea...
Create a container [root@server1 ~]# docker run -...
Introduction MySQL 5.7 aims to be the most secure...
In the project, you will encounter custom public ...
Table of contents 2. Purpose 2.1 Adding propertie...
HTML5 and jQuery implement the preview of local i...
Table of contents What is ReactHook? React curren...
Idea imports an existing web project and publishe...
Table of contents Basic Introduction Getting Star...
A transaction is a logical group of operations. E...