简体   繁体   English

处理流时进行异步操作

[英]Async operation while processing stream

I need some help, 我需要协助,

I need to read a file as a stream and on the first chunk I need to create a database table which is an async operation. 我需要将文件读取为流,并在第一个块上创建一个异步操作的数据库表。 So what I'd like to do is creating a duplex/transformer stream which would create the table when we are processing the start of the file but otherwise work as a normal stream. 所以我想做的是创建一个双工/变压器流,该流将在我们处理文件的开头时创建表,否则将作为普通流工作。

As a pseudocode I thought something like this 作为伪代码,我认为是这样的

fileLoaderService
  .loadfileAsStream()
  .pipe(parser)
  .pipe(
    if !headerProcessed
      createTable
    passThrough 
  )
  .pipe(insertToTable)

Is this possible? 这可能吗?

You can use start function of ReadableStream controller to perform tasks before processing stream at pull 您可以使用ReadableStream控制器的start功能在pull处理流之前执行任务

let stream = new ReadableStream({
  start(controller) {
     // create table
  },
  pull(controller) {
     // enqueue next chunk
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM