Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.js
More file actions
Latest commit
80 lines (64 loc) · 2.6 KB
/
Copy pathdebug.js
File metadata and controls
80 lines (64 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
constStream=require('stream');
constDD=require('.');
constpipeline=newDD();
// create one source, one drain and n transformers.
// everything has to be in Object Mode, so we cannot simply use stdin and stdout.
constsource=newStream.Readable({objectMode: true,});
constdrain=newStream.Writable({objectMode: true,});
consttrans=newStream.Transform({objectMode: true,});
consttrans2=newStream.Transform({objectMode: true,});
// the source has to emit an object you want to use in your pipeline
source._read=function(){
constinput=process.stdin.read();
if(input!==null){
this.push({
state: {},
data: input,
});
}
};
process.stdin.on('data',data=>{
source.push({
state: {
timestamp: newDate(),
},
data: data.toString(),
});
});
// don't forget to always pass the initial object
trans._transform=function(data,_,cb){
data.data=`You just inputted "${data.data.replace(/\r?\n?$/,'')}"!`;
cb(null,data);
};
trans2._transform=function(data,_,cb){
data.data=data.data.replace(/\r?\n?$/,'');
data.data+=' ~ ';
data.state.foo='FOO';
cb(null,data);
};
// the drain can consume the object in any way it wants,
// for example write it to your HTTP server as response.
drain._write=function(data,_,cb){
process.stdout.write(`Data: "${data.data}" State: ${JSON.stringify(data.state)}\n`);
cb();
};
drain.on('close',$d=>{console.log('drain event '+JSON.stringify($d));});
// when using data-drift, you have to register all pieces
// you can register new workers any time you want
// however, there can only be one source and one drain at a time!
pipeline.registerSegment(DD.SegmentType.SOURCE,source);
pipeline.registerSegment(DD.SegmentType.DRAIN,drain);
consttransformer1=pipeline.registerSegment(DD.SegmentType.WORKER,trans).unwrap();
consttransformer2=pipeline.registerSegment(DD.SegmentType.WORKER,trans2).unwrap();
// add as many transformers as you like and hot-re-order them later on :)
// then start the pipeline
pipeline.buildPipeline();
// type something, wait 10s, type again to see the difference
setTimeout(()=>{
console.log('Swap transformers...');
// the first position (after a source, if available) has the index 0
pipeline.setSegmentPosition(transformer2,0);
// the next line is implicit, since all subsequent segments are pushed to the next position
//pipeline.setSegmentPosition(transformer1, 1);
},10000);