forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-writer-tests.ts
66 lines (56 loc) · 1.22 KB
/
async-writer-tests.ts
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
/// <reference path="async-writer.d.ts" />
import asyncWriter = require('async-writer');
import stream = require('stream');
class TestStream extends stream.Writable {
constructor(public output: string) {
super();
}
_write(data: string, encoding: string, callback: Function) {
this.output += data;
callback();
}
}
// Simple usage
function simpleUsage(callback: () => void) {
var output = '';
let testStream = new TestStream(output);
let out = asyncWriter.create(testStream)
.on('error', (err: Error) => {
console.error(err);
})
.on('finish', () => {
console.log(testStream.output);
callback();
})
out.write('A');
out.write('B');
out.write('C');
out.end();
}
// Asynchronous, out-of-order writing
function asyncUsage(callback: () => void) {
var output = '';
let testStream = new TestStream(output);
let out = asyncWriter.create(testStream)
.on('error', (err: Error) => {
console.error(err);
})
.on('finish', () => {
console.log(testStream.output);
callback();
})
out.write('A');
let asyncOut = out.beginAsync();
setTimeout(() => {
asyncOut.write('B');
asyncOut.end();
}, 1000);
out.write('C');
out.end();
}
// run test
simpleUsage(() => {
asyncUsage(() => {
console.log('DONE');
});
});