From 8029e6210d0bae27fc599f81274455afbfc5dcdd Mon Sep 17 00:00:00 2001 From: DongWei Date: Thu, 13 Oct 2022 10:05:58 +0800 Subject: [PATCH] zh-CN: update snippets of codes and some translations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs:https://github.com/nodejs/nodejs.org/pull/3527. --- locale/zh-cn/docs/guides/domain-postmortem.md | 113 +++++++++--------- .../guides/event-loop-timers-and-nexttick.md | 2 +- 2 files changed, 59 insertions(+), 56 deletions(-) diff --git a/locale/zh-cn/docs/guides/domain-postmortem.md b/locale/zh-cn/docs/guides/domain-postmortem.md index 298b6307c5d8b..4b1ffc757c854 100644 --- a/locale/zh-cn/docs/guides/domain-postmortem.md +++ b/locale/zh-cn/docs/guides/domain-postmortem.md @@ -125,10 +125,9 @@ d1.run(() => 'use strict'; const domain = require('domain'); -const EE = require('events'); +const EventEmitter = require('events'); const fs = require('fs'); const net = require('net'); -const util = require('util'); const print = process._rawDebug; const pipeList = []; @@ -142,38 +141,40 @@ const buf = Buffer.alloc(FILESIZE); for (let i = 0; i < buf.length; i++) buf[i] = ((Math.random() * 1e3) % 78) + 48; // Basic ASCII fs.writeFileSync(FILENAME, buf); -function ConnectionResource(c) { - EE.call(this); - this._connection = c; - this._alive = true; - this._domain = domain.create(); - this._id = Math.random().toString(32).substr(2).substr(0, 8) + ++uid; +class ConnectionResource extends EventEmitter { + constructor(c) { + super(); - this._domain.add(c); - this._domain.on('error', () => { - this._alive = false; - }); -} -util.inherits(ConnectionResource, EE); - -ConnectionResource.prototype.end = function end(chunk) { - this._alive = false; - this._connection.end(chunk); - this.emit('end'); -}; + this._connection = c; + this._alive = true; + this._domain = domain.create(); + this._id = Math.random().toString(32).substr(2).substr(0, 8) + ++uid; -ConnectionResource.prototype.isAlive = function isAlive() { - return this._alive; -}; - -ConnectionResource.prototype.id = function id() { - return this._id; -}; + this._domain.add(c); + this._domain.on('error', () => { + this._alive = false; + }); + } -ConnectionResource.prototype.write = function write(chunk) { - this.emit('data', chunk); - return this._connection.write(chunk); -}; + end(chunk) { + this._alive = false; + this._connection.end(chunk); + this.emit('end'); + } + + isAlive() { + return this._alive; + } + + id() { + return this._id; + } + + write(chunk) { + this.emit('data', chunk); + return this._connection.write(chunk); + } +} // Example begin net @@ -321,31 +322,33 @@ function dataTransformed(chunk) { domain.active.data.connection.write(chunk); } -function DataStream(cb) { - this.cb = cb; - // DataStream wants to use domains for data propagation too! - // Unfortunately this will conflict with any domain that - // already exists. - this.domain = domain.create(); - this.domain.data = { inst: this }; -} - -DataStream.prototype.data = function data(chunk) { - // This code is self contained, but pretend it's a complex - // operation that crosses at least one other module. So - // passing along "this", etc., is not easy. - this.domain.run(() => { - // Simulate an async operation that does the data transform. - setImmediate(() => { - for (let i = 0; i < chunk.length; i++) - chunk[i] = ((chunk[i] + Math.random() * 100) % 96) + 33; - // Grab the instance from the active domain and use that - // to call the user's callback. - const self = domain.active.data.inst; - self.cb(chunk); +class DataStream { + constructor(cb) { + this.cb = cb; + // DataStream wants to use domains for data propagation too! + // Unfortunately this will conflict with any domain that + // already exists. + this.domain = domain.create(); + this.domain.data = { inst: this }; + } + + data(chunk) { + // This code is self contained, but pretend it's a complex + // operation that crosses at least one other module. So + // passing along "this", etc., is not easy. + this.domain.run(() => { + // Simulate an async operation that does the data transform. + setImmediate(() => { + for (let i = 0; i < chunk.length; i++) + chunk[i] = ((chunk[i] + Math.random() * 100) % 96) + 33; + // Grab the instance from the active domain and use that + // to call the user's callback. + const self = domain.active.data.inst; + self.cb(chunk); + }); }); - }); -}; + } +} ``` 以上表明,尝试使用一个以上的异步 API 借助域来传播数据是困难的。这个例子是固定假设通过在 `DataStream` 构造函数中赋值 `parent: domain.active`。然后通过 `domain.active = domain.active.data.parent` 在用户的回调函数被调用前恢复它。 `DataStream` 的实例化`'连接'`回调必须在 `d.run()` 中运行,而不是简单地使用 `d.add(c)`,否则将没有活动域。 diff --git a/locale/zh-cn/docs/guides/event-loop-timers-and-nexttick.md b/locale/zh-cn/docs/guides/event-loop-timers-and-nexttick.md index c0fc75d0c9744..e5ad1d9873cf1 100644 --- a/locale/zh-cn/docs/guides/event-loop-timers-and-nexttick.md +++ b/locale/zh-cn/docs/guides/event-loop-timers-and-nexttick.md @@ -301,7 +301,7 @@ server.on('listening', () => {}); 假设 `listen()` 在事件循环开始时运行,但 listening 的回调被放置在 `setImmediate()` 中。除非传递过主机名,才会立即绑定到端口。为使事件循环继续进行,它必须命中 **轮询** 阶段,这意味着有可能已经接收了一个连接,并在侦听事件之前触发了连接事件。 -另外一个示例直接从 `EventEmitter` 继承,并且在构造函数内部触发一个事件: +另外一个示例直接扩展了 `EventEmitter`,并且在构造函数内部触发一个事件: ```js const EventEmitter = require('events');