Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
refactor: make tree() a generator
Browse files Browse the repository at this point in the history
Instead of manually coding an iterator, use an ES2015 generator.
  • Loading branch information
vmx committed Mar 21, 2019
1 parent 3b410f2 commit c271bda
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 57 deletions.
90 changes: 42 additions & 48 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const ipldDagPb = require('ipld-dag-pb')
const ipldRaw = require('ipld-raw')
const multicodec = require('multicodec')
const typical = require('typical')
const { extendIterator, fancyIterator } = require('./util')
const { extendIterator } = require('./util')

class IPLDResolver {
constructor (userOptions) {
Expand Down Expand Up @@ -266,63 +266,57 @@ class IPLDResolver {
}
}

// The list of paths that will get returned
let treePaths = []
// The current block, needed to call `isLink()` on every interation
let block
// The list of items we want to follow recursively. The items are
// an object consisting of the CID and the currently already resolved
// path
const queue = [{ cid, basePath: '' }]
// The path that was already traversed
let basePath

const next = async () => {
const generator = async function * () {
// The list of paths that will get returned
const treePaths = []
// The current block, needed to call `isLink()` on every interation
let block
// The list of items we want to follow recursively. The items are
// an object consisting of the CID and the currently already resolved
// path
const queue = [{ cid, basePath: '' }]
// The path that was already traversed
let basePath

// End of iteration if there aren't any paths left to return or
// if we don't want to traverse recursively and have already
// returne the first level
if (treePaths.length === 0 && queue.length === 0) {
return { done: true }
}

// There aren't any paths left, get them from the given CID
if (treePaths.length === 0 && queue.length > 0) {
({ cid, basePath } = queue.shift())
const format = await this._getFormat(cid.codec)
block = await promisify(this.bs.get.bind(this.bs))(cid)

const paths = await promisify(format.resolver.tree)(block.data)
treePaths.push(...paths)
}
while (treePaths.length > 0 || queue.length > 0) {
// There aren't any paths left, get them from the given CID
if (treePaths.length === 0 && queue.length > 0) {
({ cid, basePath } = queue.shift())
const format = await this._getFormat(cid.codec)
block = await promisify(this.bs.get.bind(this.bs))(cid)

const paths = await promisify(format.resolver.tree)(block.data)
treePaths.push(...paths)
}

const treePath = treePaths.shift()
let fullPath = basePath + treePath
const treePath = treePaths.shift()
let fullPath = basePath + treePath

// Only follow links if recursion is intended
if (options.recursive) {
cid = await maybeRecurse(block, treePath)
if (cid !== null) {
queue.push({ cid, basePath: fullPath + '/' })
// Only follow links if recursion is intended
if (options.recursive) {
cid = await maybeRecurse(block, treePath)
if (cid !== null) {
queue.push({ cid, basePath: fullPath + '/' })
}
}
}

// Return it if it matches the given offset path, but is not the
// offset path itself
if (fullPath.startsWith(offsetPath) &&
fullPath.length > offsetPath.length) {
if (offsetPath.length > 0) {
fullPath = fullPath.slice(offsetPath.length + 1)
}
return {
done: false,
value: fullPath
// Return it if it matches the given offset path, but is not the
// offset path itself
if (fullPath.startsWith(offsetPath) &&
fullPath.length > offsetPath.length) {
if (offsetPath.length > 0) {
fullPath = fullPath.slice(offsetPath.length + 1)
}

yield fullPath
}
} else { // Else move on to the next iteration before returning
return next()
}
}
}.bind(this)

return fancyIterator(next)
return extendIterator(generator())
}

/* */
Expand Down
9 changes: 0 additions & 9 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,6 @@ exports.all = async (iterator) => {
return values
}

exports.fancyIterator = (next) => {
const iterator = { next }
iterator[Symbol.asyncIterator] = function () { return this }
iterator.first = () => exports.first(iterator)
iterator.last = () => exports.last(iterator)
iterator.all = () => exports.all(iterator)
return iterator
}

exports.extendIterator = (iterator) => {
iterator.first = () => exports.first(iterator)
iterator.last = () => exports.last(iterator)
Expand Down

0 comments on commit c271bda

Please sign in to comment.