@@ -174,10 +174,9 @@ basic operations:
174
174
' use strict' ;
175
175
176
176
const domain = require (' domain' );
177
- const EE = require (' events' );
177
+ const EventEmitter = require (' events' );
178
178
const fs = require (' fs' );
179
179
const net = require (' net' );
180
- const util = require (' util' );
181
180
const print = process ._rawDebug ;
182
181
183
182
const pipeList = [];
@@ -191,38 +190,40 @@ const buf = Buffer.alloc(FILESIZE);
191
190
for (let i = 0 ; i < buf .length ; i++ ) buf[i] = ((Math .random () * 1e3 ) % 78 ) + 48 ; // Basic ASCII
192
191
fs .writeFileSync (FILENAME , buf);
193
192
194
- function ConnectionResource (c ) {
195
- EE .call (this );
196
- this ._connection = c;
197
- this ._alive = true ;
198
- this ._domain = domain .create ();
199
- this ._id = Math .random ().toString (32 ).substr (2 ).substr (0 , 8 ) + ++ uid;
193
+ class ConnectionResource extends EventEmitter {
194
+ constructor (c ) {
195
+ super ();
200
196
201
- this ._domain .add (c);
202
- this ._domain .on (' error' , () => {
203
- this ._alive = false ;
204
- });
205
- }
206
- util .inherits (ConnectionResource, EE );
207
-
208
- ConnectionResource .prototype .end = function end (chunk ) {
209
- this ._alive = false ;
210
- this ._connection .end (chunk);
211
- this .emit (' end' );
212
- };
197
+ this ._connection = c;
198
+ this ._alive = true ;
199
+ this ._domain = domain .create ();
200
+ this ._id = Math .random ().toString (32 ).substr (2 ).substr (0 , 8 ) + ++ uid;
213
201
214
- ConnectionResource .prototype .isAlive = function isAlive () {
215
- return this ._alive ;
216
- };
217
-
218
- ConnectionResource .prototype .id = function id () {
219
- return this ._id ;
220
- };
202
+ this ._domain .add (c);
203
+ this ._domain .on (' error' , () => {
204
+ this ._alive = false ;
205
+ });
206
+ }
221
207
222
- ConnectionResource .prototype .write = function write (chunk ) {
223
- this .emit (' data' , chunk);
224
- return this ._connection .write (chunk);
225
- };
208
+ end (chunk ) {
209
+ this ._alive = false ;
210
+ this ._connection .end (chunk);
211
+ this .emit (' end' );
212
+ }
213
+
214
+ isAlive () {
215
+ return this ._alive ;
216
+ }
217
+
218
+ id () {
219
+ return this ._id ;
220
+ }
221
+
222
+ write (chunk ) {
223
+ this .emit (' data' , chunk);
224
+ return this ._connection .write (chunk);
225
+ }
226
+ }
226
227
227
228
// Example begin
228
229
net
@@ -391,31 +392,33 @@ function dataTransformed(chunk) {
391
392
domain .active .data .connection .write (chunk);
392
393
}
393
394
394
- function DataStream (cb ) {
395
- this .cb = cb;
396
- // DataStream wants to use domains for data propagation too!
397
- // Unfortunately this will conflict with any domain that
398
- // already exists.
399
- this .domain = domain .create ();
400
- this .domain .data = { inst: this };
401
- }
402
-
403
- DataStream .prototype .data = function data (chunk ) {
404
- // This code is self contained, but pretend it's a complex
405
- // operation that crosses at least one other module. So
406
- // passing along "this", etc., is not easy.
407
- this .domain .run (() => {
408
- // Simulate an async operation that does the data transform.
409
- setImmediate (() => {
410
- for (let i = 0 ; i < chunk .length ; i++ )
411
- chunk[i] = ((chunk[i] + Math .random () * 100 ) % 96 ) + 33 ;
412
- // Grab the instance from the active domain and use that
413
- // to call the user's callback.
414
- const self = domain .active .data .inst ;
415
- self .cb (chunk);
395
+ class DataStream {
396
+ constructor (cb ) {
397
+ this .cb = cb;
398
+ // DataStream wants to use domains for data propagation too!
399
+ // Unfortunately this will conflict with any domain that
400
+ // already exists.
401
+ this .domain = domain .create ();
402
+ this .domain .data = { inst: this };
403
+ }
404
+
405
+ data (chunk ) {
406
+ // This code is self contained, but pretend it's a complex
407
+ // operation that crosses at least one other module. So
408
+ // passing along "this", etc., is not easy.
409
+ this .domain .run (() => {
410
+ // Simulate an async operation that does the data transform.
411
+ setImmediate (() => {
412
+ for (let i = 0 ; i < chunk .length ; i++ )
413
+ chunk[i] = ((chunk[i] + Math .random () * 100 ) % 96 ) + 33 ;
414
+ // Grab the instance from the active domain and use that
415
+ // to call the user's callback.
416
+ const self = domain .active .data .inst ;
417
+ self .cb (chunk);
418
+ });
416
419
});
417
- });
418
- };
420
+ }
421
+ }
419
422
```
420
423
421
424
The above shows that it is difficult to have more than one asynchronous API
0 commit comments