@@ -125,10 +125,9 @@ d1.run(() =>
125
125
' use strict' ;
126
126
127
127
const domain = require (' domain' );
128
- const EE = require (' events' );
128
+ const EventEmitter = require (' events' );
129
129
const fs = require (' fs' );
130
130
const net = require (' net' );
131
- const util = require (' util' );
132
131
const print = process ._rawDebug ;
133
132
134
133
const pipeList = [];
@@ -142,38 +141,40 @@ const buf = Buffer.alloc(FILESIZE);
142
141
for (let i = 0 ; i < buf .length ; i++ ) buf[i] = ((Math .random () * 1e3 ) % 78 ) + 48 ; // Basic ASCII
143
142
fs .writeFileSync (FILENAME , buf);
144
143
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 ();
151
147
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;
164
152
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
+ }
172
158
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
+ }
177
178
178
179
// Example begin
179
180
net
@@ -321,31 +322,33 @@ function dataTransformed(chunk) {
321
322
domain .active .data .connection .write (chunk);
322
323
}
323
324
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
+ });
346
349
});
347
- });
348
- };
350
+ }
351
+ }
349
352
```
350
353
351
354
以上表明,尝试使用一个以上的异步 API 借助域来传播数据是困难的。这个例子是固定假设通过在 ` DataStream ` 构造函数中赋值 ` parent: domain.active ` 。然后通过 ` domain.active = domain.active.data.parent ` 在用户的回调函数被调用前恢复它。 ` DataStream ` 的实例化` '连接' ` 回调必须在 ` d.run() ` 中运行,而不是简单地使用 ` d.add(c) ` ,否则将没有活动域。
0 commit comments