mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-18 04:24:30 +08:00
Added definitions for async-writer
This commit is contained in:
66
async-writer/async-writer-tests.ts
Normal file
66
async-writer/async-writer-tests.ts
Normal file
@@ -0,0 +1,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');
|
||||
});
|
||||
});
|
||||
78
async-writer/async-writer.d.ts
vendored
Normal file
78
async-writer/async-writer.d.ts
vendored
Normal file
@@ -0,0 +1,78 @@
|
||||
// Type definitions for async-writer 1.4.1
|
||||
// Project: https://github.com/marko-js/async-writer
|
||||
// Definitions by: Yuce Tekol <http://yuce.me/>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
/// <reference path="../node/node.d.ts" />
|
||||
|
||||
declare module 'async-writer' {
|
||||
import stream = require('stream');
|
||||
import events = require('events');
|
||||
|
||||
module async_writer {
|
||||
interface EventFunction {
|
||||
(event: string, callback: Function): void;
|
||||
}
|
||||
|
||||
class StringWriter {
|
||||
constructor(events: events.EventEmitter);
|
||||
end(): void;
|
||||
write(what: string): StringWriter;
|
||||
toString(): string;
|
||||
}
|
||||
|
||||
class BufferedWriter {
|
||||
constructor(wrappedStream: stream.Stream);
|
||||
flush(): void;
|
||||
on(event: string, callback: Function): BufferedWriter;
|
||||
once(event: string, callback: Function): BufferedWriter;
|
||||
clear(): void;
|
||||
end(): void;
|
||||
write(what: string): BufferedWriter;
|
||||
}
|
||||
|
||||
interface BeginAsyncOptions {
|
||||
last?: boolean;
|
||||
timeout?: number;
|
||||
name?: string;
|
||||
}
|
||||
|
||||
class AsyncWriter {
|
||||
static enableAsyncStackTrace():void;
|
||||
|
||||
constructor(writer?: any, global?: {[s: string]: any}, async?: boolean, buffer?: boolean);
|
||||
isAsyncWriter: AsyncWriter;
|
||||
sync(): void;
|
||||
getAttributes(): {[s: string]: any};
|
||||
getAttribute(): any;
|
||||
write(str: string): AsyncWriter;
|
||||
getOutput(): string;
|
||||
captureString(func: Function, thisObj: Object): string;
|
||||
swapWriter(newWriter: StringWriter | BufferedWriter, func: Function, thisObj: Object): void;
|
||||
createNestedWriter(writer: StringWriter | BufferedWriter): AsyncWriter;
|
||||
beginAsync(options?: number | BeginAsyncOptions): AsyncWriter;
|
||||
handleBeginAsync(options: number | BeginAsyncOptions, parent: AsyncWriter): void;
|
||||
on(event: string, callback: Function): AsyncWriter;
|
||||
once(event: string, callback: Function): AsyncWriter;
|
||||
onLast(callback: Function): AsyncWriter;
|
||||
emit(arg: any): AsyncWriter;
|
||||
removeListener(): AsyncWriter;
|
||||
pipe(stream: stream.Stream): AsyncWriter;
|
||||
error(e: Error): void;
|
||||
end(data?: any): AsyncWriter;
|
||||
handleEnd(isAsync: boolean): void;
|
||||
_finish(): void;
|
||||
flush(): void;
|
||||
}
|
||||
|
||||
interface AsyncWriterOptions {
|
||||
global?: {[s: string]: any};
|
||||
buffer?: boolean;
|
||||
}
|
||||
|
||||
function create(writer?: any, options?: AsyncWriterOptions): AsyncWriter;
|
||||
function enableAsyncStackTrace(): void;
|
||||
}
|
||||
|
||||
export = async_writer;
|
||||
}
|
||||
Reference in New Issue
Block a user