Skip to content

Commit 036323f

Browse files
committed
docs: modernize classes in code snippets
1 parent 61e8d30 commit 036323f

File tree

2 files changed

+73
-76
lines changed

2 files changed

+73
-76
lines changed

locale/en/docs/guides/domain-postmortem.md

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,9 @@ basic operations:
162162
'use strict';
163163

164164
const domain = require('domain');
165-
const EE = require('events');
165+
const EventEmitter = require('events');
166166
const fs = require('fs');
167167
const net = require('net');
168-
const util = require('util');
169168
const print = process._rawDebug;
170169

171170
const pipeList = [];
@@ -180,38 +179,40 @@ for (let i = 0; i < buf.length; i++)
180179
buf[i] = ((Math.random() * 1e3) % 78) + 48; // Basic ASCII
181180
fs.writeFileSync(FILENAME, buf);
182181

183-
function ConnectionResource(c) {
184-
EE.call(this);
185-
this._connection = c;
186-
this._alive = true;
187-
this._domain = domain.create();
188-
this._id = Math.random().toString(32).substr(2).substr(0, 8) + (++uid);
182+
class ConnectionResource extends EventEmitter {
183+
constructor(c) {
184+
super();
189185

190-
this._domain.add(c);
191-
this._domain.on('error', () => {
192-
this._alive = false;
193-
});
194-
}
195-
util.inherits(ConnectionResource, EE);
196-
197-
ConnectionResource.prototype.end = function end(chunk) {
198-
this._alive = false;
199-
this._connection.end(chunk);
200-
this.emit('end');
201-
};
186+
this._connection = c;
187+
this._alive = true;
188+
this._domain = domain.create();
189+
this._id = Math.random().toString(32).substr(2).substr(0, 8) + (++uid);
202190

203-
ConnectionResource.prototype.isAlive = function isAlive() {
204-
return this._alive;
205-
};
206-
207-
ConnectionResource.prototype.id = function id() {
208-
return this._id;
209-
};
191+
this._domain.add(c);
192+
this._domain.on('error', () => {
193+
this._alive = false;
194+
});
195+
}
210196

211-
ConnectionResource.prototype.write = function write(chunk) {
212-
this.emit('data', chunk);
213-
return this._connection.write(chunk);
214-
};
197+
end(chunk) {
198+
this._alive = false;
199+
this._connection.end(chunk);
200+
this.emit('end');
201+
}
202+
203+
isAlive() {
204+
return this._alive;
205+
}
206+
207+
id() {
208+
return this._id;
209+
}
210+
211+
write(chunk) {
212+
this.emit('data', chunk);
213+
return this._connection.write(chunk);
214+
}
215+
}
215216

216217
// Example begin
217218
net.createServer((c) => {
@@ -364,31 +365,33 @@ function dataTransformed(chunk) {
364365
domain.active.data.connection.write(chunk);
365366
}
366367

367-
function DataStream(cb) {
368-
this.cb = cb;
369-
// DataStream wants to use domains for data propagation too!
370-
// Unfortunately this will conflict with any domain that
371-
// already exists.
372-
this.domain = domain.create();
373-
this.domain.data = { inst: this };
374-
}
375-
376-
DataStream.prototype.data = function data(chunk) {
377-
// This code is self contained, but pretend it's a complex
378-
// operation that crosses at least one other module. So
379-
// passing along "this", etc., is not easy.
380-
this.domain.run(() => {
381-
// Simulate an async operation that does the data transform.
382-
setImmediate(() => {
383-
for (let i = 0; i < chunk.length; i++)
384-
chunk[i] = ((chunk[i] + Math.random() * 100) % 96) + 33;
385-
// Grab the instance from the active domain and use that
386-
// to call the user's callback.
387-
const self = domain.active.data.inst;
388-
self.cb(chunk);
368+
class DataStream {
369+
constructor(cb) {
370+
this.cb = cb;
371+
// DataStream wants to use domains for data propagation too!
372+
// Unfortunately this will conflict with any domain that
373+
// already exists.
374+
this.domain = domain.create();
375+
this.domain.data = { inst: this };
376+
}
377+
378+
data(chunk) {
379+
// This code is self contained, but pretend it's a complex
380+
// operation that crosses at least one other module. So
381+
// passing along "this", etc., is not easy.
382+
this.domain.run(() => {
383+
// Simulate an async operation that does the data transform.
384+
setImmediate(() => {
385+
for (let i = 0; i < chunk.length; i++)
386+
chunk[i] = ((chunk[i] + Math.random() * 100) % 96) + 33;
387+
// Grab the instance from the active domain and use that
388+
// to call the user's callback.
389+
const self = domain.active.data.inst;
390+
self.cb(chunk);
391+
});
389392
});
390-
});
391-
};
393+
}
394+
}
392395
```
393396

394397
The above shows that it is difficult to have more than one asynchronous API

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

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -441,19 +441,18 @@ the event loop to proceed, it must hit the **poll** phase, which means
441441
there is a non-zero chance that a connection could have been received
442442
allowing the connection event to be fired before the listening event.
443443

444-
Another example is running a function constructor that was to, say,
445-
inherit from `EventEmitter` and it wanted to call an event within the
446-
constructor:
444+
Another example is a class that extends an `EventEmitter` and emits an
445+
event within the constructor:
447446

448447
```js
449448
const EventEmitter = require('events');
450-
const util = require('util');
451449

452-
function MyEmitter() {
453-
EventEmitter.call(this);
454-
this.emit('event');
450+
class MyEmitter extends EventEmitter {
451+
constructor() {
452+
super();
453+
this.emit('event');
454+
}
455455
}
456-
util.inherits(MyEmitter, EventEmitter);
457456

458457
const myEmitter = new MyEmitter();
459458
myEmitter.on('event', () => {
@@ -469,22 +468,17 @@ after the constructor has finished, which provides the expected results:
469468

470469
```js
471470
const EventEmitter = require('events');
472-
const util = require('util');
473471

474-
function MyEmitter() {
475-
EventEmitter.call(this);
472+
class MyEmitter extends EventEmitter {
473+
constructor() {
474+
super();
476475

477-
// use nextTick to emit the event once a handler is assigned
478-
process.nextTick(() => {
479-
this.emit('event');
480-
});
476+
// use nextTick to emit the event once a handler is assigned
477+
process.nextTick(() => {
478+
this.emit('event');
479+
});
480+
}
481481
}
482-
util.inherits(MyEmitter, EventEmitter);
483-
484-
const myEmitter = new MyEmitter();
485-
myEmitter.on('event', () => {
486-
console.log('an event occurred!');
487-
});
488482
```
489483

490484
[libuv]: https://libuv.org/

0 commit comments

Comments
 (0)