当前位置:网站首页>Little sister's notes: how do I learn simple source code to expand my horizons
Little sister's notes: how do I learn simple source code to expand my horizons
2022-07-27 02:34:00 【51CTO】
Chuange's source code interpretation article : It is said that 99% People don't know vue-devtools You can also directly open the corresponding component file ? The principle of this paper is revealed
???? Thank you very much for the source code reading activity organized by brother Chuan
1. Preparation before interpretation
1.1 Read the source code interpretation article of brother Chuan roughly , Make clear the main idea of the article : To explore the vue-devtools「 Open the component in the editor 」 Function realization principle **, Its core implementation is launch-editor**.
1.2 Make clear what you want to learn :
1) Learn how to debug the source code ;
2) Explore during debugging launch-editor The source code is how to open the corresponding file in the editor ;
The goal is : Follow brother Chuan's article to complete the debugging process , And output record documents .
1.3 resources :
- Download the source code of brother Chuan :
git clone https://github.com/lxchuan12/open-in-editor.git, Get into vue3-project Catalog , Installation dependency yarn install - install vue-devtools Google extension : Go over the wall to the app store to download and install ( download 6.0.0 beta edition )
- understand launch-editor[1]: The main function is to open the file with line number in the editor
2. Begin to learn , Be tasted
After the above preparatory work , Let's do it .
2.1 start
The editor I use is VSCode.
open vue3-project The directory package.json, Click debug , choice serve. This step of operation , Make us with debug In the form of , It's running vue-cli-service serve This command .
img
Follow the article to achieve here , I'm a little confused , Because I don't know why I suddenly want to search next 【launch-editor-middleware】 This library .
Until I read through the article again , I found that brother Chuan mentioned vue-devtools Of Open component in editor[2] This document , This document describes the reference 【launch-editor-middleware】 This library realizes the function of opening documents . And I had a preconceived idea , This issue is about interpretation vue-devtools Source code , In fact, this is just to interpret the source code that realizes the function of opening documents .
Understand this layer , We can search the project directly ( Include node_modules) Inside 【launch-editor-middleware】 keyword , You can find the source code location of this library .
2.2 Commissioning trip
The process of debugging is the breaking point , Click the debug process panel , After constant debugging , Watch the data change .
The figure below 【launch-editor-middleware】 Source code , In this source code, we can easily analyze , The final thing to run is launch function , We can make a break here , And then into 【launch-editor】 Source code , What actually works is launchEditor function .
img
img
Take a cursory look launchEditor function , I found that it actually did four things :
- obtain fileName,lineNumber,columnNumber
- exception handling : Whether there is a file ,onErrorCallback, Whether there is editor
- Guess which editor you are currently using :guessEditor
- Use child_process.spwan Open a sub process module asynchronously , It's up cmd.exe Tools open our editor , And opened the file (args Is the parameter of the file )
After reading this function , In fact, the principle of implementation is also out , The core code is as follows :
But we must have a lot of doubts , such as :
- Click the button on the browser console , How does the editor receive its request information ?
- What's used API/ Programming skills ?
- If we can realize this function , How to achieve it ( Retell ideas )?
3. Hands-On Activities , Go deep into practice
In the previous disassembly , Although many places seem to understand , But I don't fully understand , Let's answer the question when looking at the source code :
3.1 How the editor receives the request information from the browser
Click on vue-devtools When the button of , We will find that it sent a request :http://localhost:8080/__open-in-editor?file=src/components/HelloWorld.vue
img
How does the editor receive this request ? Search for 【launch-editor-middleware】 keyword , We will find that , stay @vue/cli-service Of serve.js In file , Used app.use("/__open-in-editor"), Used to express My friends will be familiar , This is a express Introduce the usage of middleware . When the browser sends http://localhost:8080/__open-in-editor?file=src/components/HelloWorld.vue When this request is made , Enter the following code .
3.2 What's used API/ Programming skills
3.2.1 Function overloading
stay 【launch-editor-middleware】 Here is the entry function of , The writing method of function overloading is used , This kind of writing is very common in many source codes , The purpose is to facilitate the transfer of parameters when users call , For non quantitative parameters, corresponding to different operation contents .
3.2.2 Decorator mode
This code wrapErrorCallback Execute other code first , To perform onErrorCallback, This form of wrapped function is also common in many source codes , It can be understood as a decorator , hold onErrorCallback Packed up , The original function is enhanced .
This is also the decorator design pattern in the design pattern :
3.2.3 apply
apply grammar :func.apply(thisArg, [argsArray]), You can often see it in the source code . Use here apply It's a extraArgs As push Methodical arguments Pass in .
3.2.4 child_process
child_process yes Node.js A module of , It provides the ability to spawn child processes , By default , Will be in the father Node.js Between the process and the derived child process stdin、stdout and stderr The pipe .
3.2.5 process.platform
Used to identify the operation Node.js The operating system platform of the process , Return string , At present, the possible values are : "aix" | "darwin" | "freebsd" | "linux" | "openbsd" | "sunos" | "win32"
3.3 How to achieve ( Retell ideas )
- Communication between browser and editor : With the help of Node.js process , Communicate with the browser
- The browser passes the path of the file to be opened to the editor through parameters
- Determine the operating system platform and the editor used ( The command line program of each platform is different , The environment variables of each editor are also different )
- With the help of Node Tune up cmd.exe Tools open our editor , Open the file in the corresponding path
4. feeling
- Coding ability : By reading launch-editor Source code , Study / Revisited 【 Function overloading 】【 Decorator mode 】【apply Usage method 】, The organizational structure of the source code is also very worthy of our study , For example, many function codes are encapsulated separately , Encapsulated into functions or modules , Make the structure of the whole source code very clear , The core is easy to understand , Easy to interpret and maintain .( This can also be understood as a top-down programming method )
- Expand your horizons : The source code contains many and Node.js Related methods , Many of them are unfamiliar to me , In the process of interpreting the source code, I also learn Node.js The process of .
- May be used at work :
- Development VSCode The plug-in can communicate with the outside world with Node.js process
- The application of decorator mode
- Determine the operating system platform
Reference material
[1]
launch-editor: https://github.com/yyx990803/launch-editor
[2]
Open component in editor: https://github.com/vuejs/devtools/blob/legacy/docs/open-in-editor.md

················· Introduction to ruokawa ·················
Hello , I am a If Chuan , Graduated from Jiangxi University . Now I'm a front-end Developer “ The engineer ”. It says 《 Learn the whole source architecture series 》 Multiple articles , In Zhihu 、 Nuggets reap millions of reading .
from 2014 From the year onwards , I write an annual summary every year , Has written 7 piece , Click to see Annual summary . meanwhile , Active in Zhihu @ If Chuan , Nuggets @ If Chuan . Committed to sharing front end development experience , vision : help 5 In this year, the front-end people are in the forefront .
边栏推荐
- 从初级程序员到架构师学习路线+配套学习资源完整版
- What is the principle of synchronized lock escalation in multithreading?
- [C language] relevant distinction between strlen and sizeof
- 静态路由实验配置
- Is it useful to lie down with your eyes closed when you can't sleep?
- 【自此文之后,学习链表一片坦途】
- php+swoole
- 【C语言】阶乘实现
- Prometheus 运维工具 Promtool (三) Debug 功能
- MGRE、PPP、HDLC综合实验
猜你喜欢
随机推荐
面试必问 | 一个线程从创建到消亡要经历哪些阶段?
HCIP oSPF知识总结
[draw rectangular coordinate system in C language]
【在Visual Studio 2019中使用SQLite3库实现学生信息管理系统】
NAT network address translation protocol topology experiment
oSPF基础实验配置
NPM reports an error, error: eperm: operation not permitted, MKDIR
蚂蚁京东新浪10位架构师424页佳作深入分布式缓存从原理到实践pdf
Hcip day 3 Wan topology experiment
小姐姐笔记:我是如何学习简单源码拓展视野的
有趣的C语言
JMeter下载安装
Golang -- parse yaml file
【洋哥带你玩转线性表(三)——双向链表】
【封神演绎、十五分钟让你彻底学会栈的使用!!!】
【自此文之后,学习链表一片坦途】
Hcip day 4 OSPF routing protocol
The problem of storing elements in TreeSet collection
PPOCRLabel格式的数据集操作总结。
OSPF basic experimental configuration








