当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript thunks.thunk类代码示例

本文整理汇总了TypeScript中thunks.thunk的典型用法代码示例。如果您正苦于以下问题:TypeScript thunk类的具体用法?TypeScript thunk怎么用?TypeScript thunk使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了thunk类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: it


//.........这里部分代码省略.........
          if (rand < 0.2) {
            setTimeout(() => serverListener.call(this, msg, rinfo), rand * 10) // out-of-order
          } else {
            serverListener.call(this, msg, rinfo)
          }
        })

        const cli1 = new Client()
        cli1.setKeepAlive(true)
        cli1.on('error', (err) => { ilog(Object.assign(err, { class: 'client error' })) })

        await cli1.connect(server.address().port)
        const clientListener = cli1[kSocket].listeners('message')[0]
        cli1[kSocket].removeListener('message', clientListener)
        cli1[kSocket].addListener('message', function (msg: Buffer, rinfo: AddressInfo) {
          const rand = Math.random()
          if (rand < lossRatio) {
            return // packet loss
          }
          if (rand < 0.2) {
            setTimeout(() => clientListener.call(this, msg, rinfo), rand * 10) // out-of-order
          } else {
            clientListener.call(this, msg, rinfo)
          }
        })

        const stream1 = cli1.request()
        stream1.on('error', (err) => { throw Object.assign(err, { class: 'client stream error' })})
        const dataStream1 = new RandDataStream(bytes)
        stream1.on('drain', () => {
          dataStream1.resume()
        })
        // randDataStream.pipe(stream) // use paused mode for large stream
        dataStream1
          .on('data', (chunk) => {
            if (!stream1.write(chunk)) {
              dataStream1.pause()
            }
          })
          .on('end', () => stream1.end())

        const hash1 = createHash('sha256')
        stream1.pipe(hash1)

        const cli2 = await cli1.spawn(server.address().port)
        cli2.setKeepAlive(true)
        const stream2 = cli2.request()
        const stream3 = cli2.request()
        stream2.on('error', (err) => { throw Object.assign(err, { class: 'client stream error' })})
        stream3.on('error', (err) => { throw Object.assign(err, { class: 'client stream error' })})
        const dataStream2 = new RandDataStream(bytes)
        stream3.on('drain', () => {
          dataStream2.resume()
        })
        dataStream2
          .on('data', (chunk) => {
            stream2.write(chunk)
            if (!stream3.write(chunk)) {
              dataStream2.pause()
            }
          })
          .on('end', () => {
            stream2.end()
            stream3.end()
          })

        const hash2 = createHash('sha256')
        stream2.pipe(hash2)
        const hash3 = createHash('sha256')
        stream3.pipe(hash3)

        await Promise.all([
          onceEmit.call(stream1, 'end'),
          onceEmit.call(stream2, 'end'),
          onceEmit.call(stream3, 'end'),
        ])

        equal(dataStream1.readBytes, bytes)
        equal(dataStream1.totalSize, bytes)
        equal(stream1.bytesRead, bytes)
        equal(stream1.bytesWritten, bytes)
        const ret1 = hash1.read() as Buffer
        equal(ret1.toString('hex'), dataStream1.sum)

        equal(dataStream2.readBytes, bytes)
        equal(dataStream2.totalSize, bytes)
        equal(stream2.bytesRead, bytes)
        equal(stream2.bytesWritten, bytes)
        const ret2 = hash2.read() as Buffer
        equal(ret2.toString('hex'), dataStream2.sum)

        equal(stream3.bytesRead, bytes)
        equal(stream3.bytesWritten, bytes)
        const ret3 = hash3.read() as Buffer
        equal(ret3.toString('hex'), dataStream2.sum)

        await Promise.all([cli1.close(), cli2.close()])
        await server.close()
        await thunk.promise(thunk.delay(500))
      })
开发者ID:toajs,项目名称:quic,代码行数:101,代码来源:chaos.ts

示例2:

 tman.it('test level 2-2', function * () {
   yield thunk.delay(10)
   assert.strictEqual(count++, 4)
 })
开发者ID:thunks,项目名称:tman,代码行数:4,代码来源:typings.test.ts

示例3: function

 tman.it('test level 1-1', function () {
   this.timeout(1000)
   assert.strictEqual(count++, 21)
   return thunk.delay(100)
 })
开发者ID:thunks,项目名称:tman,代码行数:5,代码来源:typings.test.ts

示例4: return

 return (function *() { yield thunk.delay(20) })()
开发者ID:thunks,项目名称:tman,代码行数:1,代码来源:typings.test.ts


注:本文中的thunks.thunk类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。