Skip to content

Commit cfa4454

Browse files
author
Maledong
authored
zh-CN: update snippets of codes and some translations (#4877)
Refs:#3527.
1 parent 2965717 commit cfa4454

File tree

2 files changed

+59
-56
lines changed

2 files changed

+59
-56
lines changed

locale/zh-cn/docs/guides/domain-postmortem.md

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,9 @@ d1.run(() =>
125125
'use strict';
126126

127127
const domain = require('domain');
128-
const EE = require('events');
128+
const EventEmitter = require('events');
129129
const fs = require('fs');
130130
const net = require('net');
131-
const util = require('util');
132131
const print = process._rawDebug;
133132

134133
const pipeList = [];
@@ -142,38 +141,40 @@ const buf = Buffer.alloc(FILESIZE);
142141
for (let i = 0; i < buf.length; i++) buf[i] = ((Math.random() * 1e3) % 78) + 48; // Basic ASCII
143142
fs.writeFileSync(FILENAME, buf);
144143

145-
function ConnectionResource(c) {
146-
EE.call(this);
147-
this._connection = c;
148-
this._alive = true;
149-
this._domain = domain.create();
150-
this._id = Math.random().toString(32).substr(2).substr(0, 8) + ++uid;
144+
class ConnectionResource extends EventEmitter {
145+
constructor(c) {
146+
super();
151147

152-
this._domain.add(c);
153-
this._domain.on('error', () => {
154-
this._alive = false;
155-
});
156-
}
157-
util.inherits(ConnectionResource, EE);
158-
159-
ConnectionResource.prototype.end = function end(chunk) {
160-
this._alive = false;
161-
this._connection.end(chunk);
162-
this.emit('end');
163-
};
148+
this._connection = c;
149+
this._alive = true;
150+
this._domain = domain.create();
151+
this._id = Math.random().toString(32).substr(2).substr(0, 8) + ++uid;
164152

165-
ConnectionResource.prototype.isAlive = function isAlive() {
166-
return this._alive;
167-
};
168-
169-
ConnectionResource.prototype.id = function id() {
170-
return this._id;
171-
};
153+
this._domain.add(c);
154+
this._domain.on('error', () => {
155+
this._alive = false;
156+
});
157+
}
172158

173-
ConnectionResource.prototype.write = function write(chunk) {
174-
this.emit('data', chunk);
175-
return this._connection.write(chunk);
176-
};
159+
end(chunk) {
160+
this._alive = false;
161+
this._connection.end(chunk);
162+
this.emit('end');
163+
}
164+
165+
isAlive() {
166+
return this._alive;
167+
}
168+
169+
id() {
170+
return this._id;
171+
}
172+
173+
write(chunk) {
174+
this.emit('data', chunk);
175+
return this._connection.write(chunk);
176+
}
177+
}
177178

178179
// Example begin
179180
net
@@ -321,31 +322,33 @@ function dataTransformed(chunk) {
321322
domain.active.data.connection.write(chunk);
322323
}
323324

324-
function DataStream(cb) {
325-
this.cb = cb;
326-
// DataStream wants to use domains for data propagation too!
327-
// Unfortunately this will conflict with any domain that
328-
// already exists.
329-
this.domain = domain.create();
330-
this.domain.data = { inst: this };
331-
}
332-
333-
DataStream.prototype.data = function data(chunk) {
334-
// This code is self contained, but pretend it's a complex
335-
// operation that crosses at least one other module. So
336-
// passing along "this", etc., is not easy.
337-
this.domain.run(() => {
338-
// Simulate an async operation that does the data transform.
339-
setImmediate(() => {
340-
for (let i = 0; i < chunk.length; i++)
341-
chunk[i] = ((chunk[i] + Math.random() * 100) % 96) + 33;
342-
// Grab the instance from the active domain and use that
343-
// to call the user's callback.
344-
const self = domain.active.data.inst;
345-
self.cb(chunk);
325+
class DataStream {
326+
constructor(cb) {
327+
this.cb = cb;
328+
// DataStream wants to use domains for data propagation too!
329+
// Unfortunately this will conflict with any domain that
330+
// already exists.
331+
this.domain = domain.create();
332+
this.domain.data = { inst: this };
333+
}
334+
335+
data(chunk) {
336+
// This code is self contained, but pretend it's a complex
337+
// operation that crosses at least one other module. So
338+
// passing along "this", etc., is not easy.
339+
this.domain.run(() => {
340+
// Simulate an async operation that does the data transform.
341+
setImmediate(() => {
342+
for (let i = 0; i < chunk.length; i++)
343+
chunk[i] = ((chunk[i] + Math.random() * 100) % 96) + 33;
344+
// Grab the instance from the active domain and use that
345+
// to call the user's callback.
346+
const self = domain.active.data.inst;
347+
self.cb(chunk);
348+
});
346349
});
347-
});
348-
};
350+
}
351+
}
349352
```
350353

351354
以上表明,尝试使用一个以上的异步 API 借助域来传播数据是困难的。这个例子是固定假设通过在 `DataStream` 构造函数中赋值 `parent: domain.active`。然后通过 `domain.active = domain.active.data.parent` 在用户的回调函数被调用前恢复它。 `DataStream` 的实例化`'连接'`回调必须在 `d.run()` 中运行,而不是简单地使用 `d.add(c)`,否则将没有活动域。

locale/zh-cn/docs/guides/event-loop-timers-and-nexttick.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ server.on('listening', () => {});
301301

302302
假设 `listen()` 在事件循环开始时运行,但 listening 的回调被放置在 `setImmediate()` 中。除非传递过主机名,才会立即绑定到端口。为使事件循环继续进行,它必须命中 **轮询** 阶段,这意味着有可能已经接收了一个连接,并在侦听事件之前触发了连接事件。
303303

304-
另外一个示例直接从 `EventEmitter` 继承,并且在构造函数内部触发一个事件:
304+
另外一个示例直接扩展了 `EventEmitter`,并且在构造函数内部触发一个事件:
305305

306306
```js
307307
const EventEmitter = require('events');

0 commit comments

Comments
 (0)