本文整理汇总了TypeScript中stream.Readable.on方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Readable.on方法的具体用法?TypeScript Readable.on怎么用?TypeScript Readable.on使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stream.Readable
的用法示例。
在下文中一共展示了Readable.on方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: error
return new Promise<Buffer>((complete, error) => {
let done = false;
let buffer = Buffer.allocUnsafe(bytes);
let bytesRead = 0;
stream.on('data', (data: Buffer) => {
let bytesToRead = Math.min(bytes - bytesRead, data.length);
data.copy(buffer, bytesRead, 0, bytesToRead);
bytesRead += bytesToRead;
if (bytesRead === bytes) {
(stream as any).destroy(); // Will trigger the close event eventually
}
});
stream.on('error', (e: Error) => {
if (!done) {
done = true;
error(e);
}
});
stream.on('close', () => {
if (!done) {
done = true;
complete(buffer.slice(0, bytesRead));
}
});
});
示例2: Promise
return new Promise(resolve => {
let result: string = "";
stream.on("data", (chunk: Buffer) => {
result += chunk.toString();
});
stream.on("end", () => {
resolve(result);
});
});
示例3: resolve
return new Promise<string>((resolve, reject) => {
let data = [];
stream.on('data', (chunk) => {
data.push(chunk);
});
stream.on('end', () => {
resolve(Buffer.concat(data).toString());
});
stream.on('error', (error) => {
reject(error);
});
});
示例4: function
return Observable.create(function (observer: Observer<T>) {
const dataHandler = (data: T) => observer.next(data);
const errorHandler = (err: any) => observer.error(err);
const endHandler = () => observer.complete();
stream.on("data", dataHandler);
stream.on("error", errorHandler);
stream.on("end", endHandler);
stream.resume();
return function() {
stream.removeListener("data", dataHandler);
stream.removeListener("error", errorHandler);
stream.removeListener("end", endHandler);
};
}).share();
示例5: collectData
private collectData(stream: Readable): Buffer[] {
const buffers: Buffer[] = [];
stream.on('data', (data: Buffer) => {
buffers.push(data);
});
return buffers;
}
示例6: forwardData
private forwardData(stream: Readable, encoding: string, cb: (err: Error, stdout?: string) => void): NodeStringDecoder {
const decoder = new StringDecoder(encoding);
stream.on('data', (data: Buffer) => {
cb(null, decoder.write(data));
});
return decoder;
}
示例7:
return new Promise<void>(function(resolve, reject) {
var ws = fs.createWriteStream(path);
ws.on('finish', resolve)
.on('error', reject);
stream.on('error', reject);
stream.pipe(ws);
});
示例8: c
return new Promise<string>((c, e) => {
const shasum = crypto.createHash(hashName);
stream
.on('data', shasum.update.bind(shasum))
.on('error', e)
.on('close', () => c(shasum.digest('hex')));
});
示例9: readToEnd
export function readToEnd(stream: Readable,
callback: (error: Error, chunks?: any[]) => void) {
const chunks: Array<Buffer | string> = [];
return stream
.on('error', callback)
.on('data', chunk => chunks.push(chunk))
.on('end', () => callback(null, chunks));
}
示例10: subscribeToDataEvent
function subscribeToDataEvent(readable: Readable, outputChannel: OutputChannel, saveData: boolean, dataStorage: string): void {
readable.on('data', chunk => {
const chunkAsString = typeof chunk === 'string' ? chunk : chunk.toString();
outputChannel.append(chunkAsString);
if (saveData) {
dataStorage += chunkAsString;
}
});
}