Skip to content

Commit 8c0b44a

Browse files
committed
GitBook: [master] 20 pages and 12 assets modified
1 parent b28fed0 commit 8c0b44a

28 files changed

+123
-109
lines changed
106 KB
Loading

.gitbook/assets/4-types-stream.png

106 KB
Loading

.gitbook/assets/child_process.png

115 KB
Loading

.gitbook/assets/demo-chat-app (1).png

118 KB
Loading

.gitbook/assets/demo-chat-app.png

118 KB
Loading

.gitbook/assets/event-loop-2.png

91.6 KB
Loading

.gitbook/assets/event.png

505 KB
Loading

.gitbook/assets/micro-marco.png

76.9 KB
Loading

.gitbook/assets/node-core-2.png

176 KB
Loading

.gitbook/assets/node-core.png

297 KB
Loading

.gitbook/assets/stream-3 (1).png

192 KB
Loading

.gitbook/assets/stream-3.png

192 KB
Loading

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,5 @@ Node advance
1313
* C++ Addon
1414
* Async/Non-blocking IO
1515

16-
17-
1816
![](.gitbook/assets/image.png)
1917

SUMMARY.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# Summary
1+
# Table of contents
22

33
* [Introduction](README.md)
44
* [Node Core](node-core.md)
5-
* [Event loop](event-loop.md)
6-
* [micro-macro queue](micro-macro-queue.md)
7-
* [Event](event.md)
8-
* [Event Emitter 1](event-emitter.md)
9-
* [Event Emitter 2](event-emitter-2.md)
5+
* [Async](event-loop/README.md)
6+
* [micro-macro queue](event-loop/micro-macro-queue.md)
7+
* [Event](event/README.md)
8+
* [Event Emitter 1](event/event-emitter-1.md)
9+
* [Event Emitter 2](event/event-emitter-2.md)
1010
* [TCP với net module](tcp-voi-net-module.md)
11-
* [Stream](stream.md)
11+
* [Stream](stream/README.md)
12+
* [Buffer](stream/buffer.md)
1213
* [child process](child-process.md)
1314
* [C++ Addon](c++-addon.md)
14-
* [Buffer](buffer.md)
1515
* [Demo chat-app](demo-chat-app.md)
1616

buffer.md

-15
This file was deleted.

child-process.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
spawn, fork, ...
1+
# child process
22

3-
![](/assets/child_process.png)
3+
spawn, fork, ...
4+
5+
![](.gitbook/assets/child_process.png)
46

demo-chat-app.md

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
1+
# Demo chat-app
22

33
Tạo 1 TCP server và lắng nghe các kết nối tới
44

5-
6-
7-
```js
5+
```javascript
86
var server = require('net').createServer()
97

108
let counter = 0
@@ -44,9 +42,7 @@ process.on('uncaughtException', function (err) {
4442
});
4543
```
4644

47-
48-
4945
## DEMO
5046

51-
![](/assets/demo-chat-app.png)
47+
![](.gitbook/assets/demo-chat-app.png)
5248

event-emitter-2.md

-2
This file was deleted.

event-loop.md renamed to event-loop/README.md

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
# \# đặt vấn đề
1+
# Async
22

3-
1. ## vì sao setTimeout\( \(\), 0\) hay setImmediate\(\) vẫn chạy sau ham
3+
## \# đặt vấn đề
44

5-
```
5+
1. **vì sao setTimeout\( \(\), 0\) hay setImmediate\(\) vẫn chạy sau ham**
6+
7+
```text
68
setImmediate(function () {
79
console.log('first iteration');
810
})
@@ -13,9 +15,9 @@ second iteration // chay truc tiep tu call stack
1315
first iteration // duoc day vao node roi day qua callback queue truoc khi thuc thi o call stack
1416
```
1517

16-
## 2. `process.nextTick()`
18+
### 2. `process.nextTick()`
1719

18-
```
20+
```text
1921
function cb() {
2022
console.log('Processed in nextTick');
2123
}
@@ -34,7 +36,7 @@ Processed in nextTick
3436
Processed immediate
3537
```
3638

37-
## 3. `setImmediate()` vs `setTimeout()` vs `process.nextTick()`
39+
### 3. `setImmediate()` vs `setTimeout()` vs `process.nextTick()`
3840

3941
nếu chỉ thuần `setImmediate()`vs `setTimeout()` thì kết quả là ko xác định được\(non-deterministic\).
4042

@@ -43,13 +45,13 @@ Trong khi setTimeout\(\) thì xác định đoạn mã sẽ được chạy
4345

4446
trong khi đó `process.nextTick()` có khả năng đẩy hàm callback vào vị trí đầu tiên trong hàng đợi.
4547

46-
# \# event loop
48+
## \# event loop
4749

4850
Là cơ chế quan trọng thực thi lập trình bất đồng bộ, nằm trong module libuv
4951

50-
Event loop sẽ liên tục kiểm tra call stack, nếu call stack trống sẽ kiểm tra callback queue. Nếu có hàm trong callback queue, thì đẩy qua call stack của V8 để thực thi.
52+
Event loop sẽ liên tục kiểm tra call stack, nếu call stack trống sẽ kiểm tra callback queue. Nếu có hàm trong callback queue, thì đẩy qua call stack của V8 để thực thi.
5153

52-
```
54+
```text
5355
┌───────────────────────┐
5456
┌─>│ timers │
5557
│ └──────────┬────────────┘
@@ -74,7 +76,9 @@ Quá trình thực hiện của code sẽ là code được đưa vào ca
7476
-> khi hàm callback được gọi sẽ đẩy qua event queue
7577
-> lúc này event loop sẽ xem call stack có trống không để đẩy hàm từ queue qua loop
7678

77-
# \# quan hệ giữa event-loop, V8, Node
79+
## \# quan hệ giữa event-loop, V8, Node
80+
81+
![](../.gitbook/assets/event-loop-2.png)
82+
7883

79-
![](/assets/event-loop-2.png)
8084

micro-macro-queue.md renamed to event-loop/micro-macro-queue.md

+10-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
```js
1+
# micro-macro queue
2+
3+
```javascript
24
const interval = setInterval(() => {
35
console.log('setInterval')
46
}, 0)
@@ -59,36 +61,25 @@ promise 6
5961
mirco-task sẽ được duyệt trước marco-task\(??\). Thứ tự ưu tiên là
6062

6163
* process.nextTick
62-
6364
* promise
6465

6566
Kết quả: promise 1 và promise 2 sẽ thực thi trước
6667

67-
2. Sau đó tới lượt marco-task setInterval\(\) và setTimeout\(\) được thực thi, một lệnh setInterval\(\) tiếp theo sẽ được đẩy ngay sau setTimeout\(\) vì time = 0.
68-
Kết quả: setInterval => setTimeout 1
69-
70-
3. Sau khi thực thi setTimeout\(\), promise 3, promise 4 và promise x\(setTimeout\(\)\) được đẩy vào micro-task và thực thi
71-
Kết quả: promise 3 và promise 4 + setTimeout\(\) được đẩy qua node
72-
73-
4. chương trình setTimeout\(\) sẽ được đẩy qua node rồi sau đó trở về queue lại nên nó sẽ xếp sau setInterval\(\)
74-
Kết quả: setInterval => setTimeout 2
75-
68+
2. Sau đó tới lượt marco-task setInterval\(\) và setTimeout\(\) được thực thi, một lệnh setInterval\(\) tiếp theo sẽ được đẩy ngay sau setTimeout\(\) vì time = 0. Kết quả: setInterval => setTimeout 1
69+
3. Sau khi thực thi setTimeout\(\), promise 3, promise 4 và promise x\(setTimeout\(\)\) được đẩy vào micro-task và thực thi Kết quả: promise 3 và promise 4 + setTimeout\(\) được đẩy qua node
70+
4. chương trình setTimeout\(\) sẽ được đẩy qua node rồi sau đó trở về queue lại nên nó sẽ xếp sau setInterval\(\) Kết quả: setInterval => setTimeout 2
7671
5. Phần 5 cùng tương tự phần 3
7772

78-
---
79-
8073
## hình dưới minh họa thứ tự thực hiện marco task và micro task
8174

82-
![](/assets/micro-marco.png)
75+
![](../.gitbook/assets/micro-marco.png)
8376

8477
### cần xem lại về process.nextTick\(\)
8578

86-
> the `nextTickQueue `will be **processed after the current operation completes**, regardless of the current phase of the event loop.
87-
> any time you call `process.nextTick() `in a given phase, all callbacks passed to `process.nextTick() `will be resolved before the event loop continues.
79+
> the `nextTickQueue`will be **processed after the current operation completes**, regardless of the current phase of the event loop.
80+
> any time you call `process.nextTick()`in a given phase, all callbacks passed to `process.nextTick()`will be resolved before the event loop continues.
8881
89-
90-
91-
```
82+
```text
9283
setTimeout(() => {
9384
console.log('setTimeout 1')
9485
process.nextTick(() => {
@@ -117,5 +108,3 @@ process.nextTick(() => {
117108
})
118109
```
119110

120-
121-

event.md renamed to event/README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,5 @@ event có thể tới từ hệ thống như kết nối tcp, đọc xon
44

55
hoặc tự viết bằng kế thừa từ module events, tạo phương thức on\(\) và emit\(\)
66

7-
![](/assets/event.png)
8-
9-
7+
![](../.gitbook/assets/event.png)
108

event-emitter.md renamed to event/event-emitter-1.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Event Emitter
1+
# Event Emitter 1
22

33
1. Tự viết event-emiiter
44
2. Kế thừa
@@ -53,9 +53,7 @@ for (var event of events) {
5353
}
5454
```
5555

56-
###
57-
58-
### 2. Kế thừa từ module event
56+
## 2. Kế thừa từ module event
5957

6058
```javascript
6159
// DÙNG CLASS
@@ -82,5 +80,3 @@ dialog.on('hello', function (data) {
8280
dialog.sayHello('nhat')
8381
```
8482

85-
86-

event/event-emitter-2.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Event Emitter 2
2+
3+
[https://medium.freecodecamp.org/understanding-node-js-event-driven-architecture-223292fcbc2d](https://medium.freecodecamp.org/understanding-node-js-event-driven-architecture-223292fcbc2d)
4+

node-core.md

+9-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
# ![](/assets/node-core.png)
1+
# Node Core
22

3-
# cụ thể hơn
3+
![](.gitbook/assets/node-core.png)
44

5-
# ![](/assets/node-core-2.png)
5+
## cụ thể hơn
66

7-
# Core module
7+
![](.gitbook/assets/node-core-2.png)
8+
9+
## Core module
810

911
* C++ core
1012
* JS core
1113

12-
---
13-
14-
# V8 Engine
14+
## V8 Engine
1515

1616
V8 là một javascrpit engine dùng để biên dịch ra mã thực thi trên vi xử lý. Do được viết bằng C++ nên nó có thể được dùng độc lập hoặc được [nhúng](https://github.com/v8/v8/wiki/Getting-Started-with-Embedding) vào các chương trình viết bằng C++ khác.
1717

@@ -25,20 +25,14 @@ thử thêm một chức năng\(lệnh\) greeting vào shell
2525

2626
### chú ý là dòng `#include <include/v8.h>`
2727

28-
#
29-
30-
Ở trên đã thấy cách dùng V8, thì việc thực thi code js giống như ta nhập vào shell, nó sẽ được biên dịch ra mã máy và chạy. Tuy nhiên không phải thư viện nào cũng cần binding JS với C++ mà chỉ thuần túy chứa các hàm.
28+
Ở trên đã thấy cách dùng V8, thì việc thực thi code js giống như ta nhập vào shell, nó sẽ được biên dịch ra mã máy và chạy. Tuy nhiên không phải thư viện nào cũng cần binding JS với C++ mà chỉ thuần túy chứa các hàm.
3129

3230
### chú ý dòng `const binding = process.binding('');`
3331

34-
---
35-
36-
# libuv
32+
## libuv
3733

3834
2 thành phần quan trọng nhất
3935

4036
* quản lý các kết nối IO bất đồng bộ
4137
* threadpool + event-loop
4238

43-
44-

stream.md

-17
This file was deleted.

stream/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Stream
2+
3+
stream kế thừa từ class event
4+
5+
![](../.gitbook/assets/import.png)
6+
7+
có 4 loại stream
8+
9+
![](../.gitbook/assets/4-types-stream%20%281%29.png)
10+
11+
\(
12+
13+
duplex va transform khac chi nha ???
14+
15+
\)
16+
17+
stream as a function with input are readable stream part and outut is writable stream
18+
19+
all stream are instance of EventEmitter
20+
pipe
21+
22+
![](../.gitbook/assets/stream-3.png)
23+

stream/buffer.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Buffer
2+
3+
Buffer liên quan nhiều tới Stream nhưng khác với stream thì buffer là global object, có thể tự allocate được. Xử lý như một mảng bình thường nhưng giá trị thể hiện bằng hex.
4+
5+
Bình thường hay đổi giá trị nhị phân thành kí tự để đọc bằng toString\(\)
6+
7+
```text
8+
var fs = require('fs')
9+
10+
fs.readFile('./text.txt', (err, buf) => {
11+
console.log(buf); // <Buffer 31 32 33 34 35 36 37 38 39>
12+
console.log(buf.toString()); // 123456789
13+
})
14+
```
15+
16+
Có thể làm việc trực tiếp với buffer mà không cần chuyển mã
17+
18+
```text
19+
fs.readFile('./test.txt', (err, buf) => {
20+
for (var i = 0; i < buf.length; i++) {
21+
buf[i] = buf[i] + (65 - 49)
22+
}
23+
console.log(buf.toString()); // ABCDEFGHI
24+
})
25+
26+
```
27+
28+
29+
30+
có 3 cách để tạo buffer là
31+
32+
* Buffer.from\(\)
33+
* Buffer.alloc\(\)
34+
* Buffer.allocUnsafe\(\)
35+
36+
```text
37+
var buffer = new ArrayBuffer(8)
38+
var view = new Int32Array(buffer)
39+
```
40+

tcp-voi-net-module.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# TCP với net module
2+
3+
4+

0 commit comments

Comments
 (0)