@@ -162,10 +162,9 @@ basic operations:
162162' use strict' ;
163163
164164const domain = require (' domain' );
165- const EE = require (' events' );
165+ const EventEmitter = require (' events' );
166166const fs = require (' fs' );
167167const net = require (' net' );
168- const util = require (' util' );
169168const print = process ._rawDebug ;
170169
171170const pipeList = [];
@@ -180,38 +179,40 @@ for (let i = 0; i < buf.length; i++)
180179 buf[i] = ((Math .random () * 1e3 ) % 78 ) + 48 ; // Basic ASCII
181180fs .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
217218net .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
394397The above shows that it is difficult to have more than one asynchronous API
0 commit comments