Skip to content

Instantly share code, notes, and snippets.

@vasco-santos
Last active August 24, 2019 16:23
Show Gist options
  • Save vasco-santos/748c49fe07a2b1d2fa85f25a093501bf to your computer and use it in GitHub Desktop.
Save vasco-santos/748c49fe07a2b1d2fa85f25a093501bf to your computer and use it in GitHub Desktop.
const promiseToCallback = require('promise-to-callback')
const toPull = require('async-iterator-to-pull-stream')
const encrypt = async (localId, conn, remoteId) => {
// TODO implementation
// ...
pull(
conn,
toPull.duplex(handshake(state, finish)),
conn
)
// ...
}
module.exports = {
tag: '...',
encrypt (localId, conn, remoteId, callback) {
promiseToCallback(encrypt(localId, conn, remoteId))(callback)
},
encryptAsync: encrypt
}
@vasco-santos
Copy link
Author

This will allow you to do all the work using promise based code, as well as async iterators inside the handshake logic 😄

After js-libp2p fully migrates to async await and async iterators, we would only need to change:

  • exported encrypt function should call the async encrypt function directly (remove the promiseToCallback and the exported encryptAsyncfunction
  • remove the converter toPull.duplex and change pull-stream in favour of it-pipe

@vasco-santos
Copy link
Author

'use strict'

const pipe = require('it-pipe')

const main = async () => {
  const oneTwoThree = () => [1, 2, 3]

  const collect = async (source) => {
    for await (const val of source) {
      console.log('val', val)
    }
  }

  const connection = {
    sink: collect,
    source: oneTwoThree()
  }

  await pipe(['first'], connection)

  await pipe(
    connection,
    function transform(source) {
      return (async function* () { // A generator is async iterable
        for await (const val of source) yield val * 2
      })()
    },
    connection)
}


main()

@vasco-santos
Copy link
Author

'use strict'

const pipe = require('it-pipe')

const mainIniator = async () => {
  const numberSource = () => [1, 2, 3, 4, 5]

  const collect = async (source) => {
    for await (const val of source) {
      console.log('val', val)
    }
  }

  const connection = {
    sink: collect,
    source: numberSource()
  }

  await pipe(['first message'], connection)

  await pipe(
    connection,
    function transform(source) {
      return (async function* () { // A generator is async iterable
        let step = 1
        for await (const val of source) {
          if (step % 2 === 0) {
            yield val * 2
          } else {
            yield step
          }
          step += 1
        }
      })()
    },
    connection)
}

const mainReceiver = async () => {
  const numberSource = () => [10, 20, 30, 40, 50]

  const collect = async (source) => {
    for await (const val of source) {
      console.log('val', val)
    }
  }

  const connection = {
    sink: collect,
    source: numberSource()
  }

  await pipe(
    connection,
    function transform(source) {
      return (async function* () { // A generator is async iterable
        let step = 0
        for await (const val of source) {
          if (step % 2 === 0) {
            yield val * 2
          } else {
            yield step
          }
          step += 1
        }
      })()
    },
    connection)
}

mainIniator()
mainReceiver()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment