Skip to content

Commit

Permalink
implement getDocPath
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Apr 27, 2024
1 parent 8b55042 commit 65d0441
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,28 @@ export const getDocChildren = async (ystream, owner, collection, parent) => {
return keys.map(k => k.child)
}

/**
* @param {Ystream} ystream
* @param {Uint8Array} owner
* @param {string} collection
* @param {string} doc
* @return {Promise<Array<string>>}
*/
export const getDocPath = async (ystream, owner, collection, doc) => {
let currDoc = doc
/**
* @type {Array<string>}
*/
const path = []
while (currDoc != null) {
const parentOp = await getDocOpsMerged(ystream, owner, collection, currDoc, operations.OpChildOfType)
if (parentOp == null) break
currDoc = parentOp.op.parent
path.unshift(currDoc)
}
return path
}

/**
* @template {operations.OpTypeIds} TYPEID
* @template {InstanceType<operations.typeMap[TYPEID]>} TYPE
Expand Down
7 changes: 7 additions & 0 deletions src/ystream.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ export class Collection extends ObservableV2 {
return co?.op.parent
}

/**
* @param {string} docname
*/
getDocPath (docname) {
return actions.getDocPath(this.ystream, this.ownerBin, this.collection, docname)
}

/**
* @param {string} docname
* @param {string} parentDoc
Expand Down
3 changes: 3 additions & 0 deletions tests/ystream.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,7 @@ export const testFolderStructure = async tc => {
t.assert(await collection1.getParent('C') === 'B')
t.compareArrays(await collection1.getChildren('A'), ['B'])
t.compareArrays(await collection1.getChildren('B'), ['C', 'D']) // should return in alphabetical order
t.compareArrays(await collection1.getDocPath('A'), [])
t.compareArrays(await collection1.getDocPath('B'), ['A'])
t.compareArrays(await collection1.getDocPath('D'), ['A', 'B'])
}

0 comments on commit 65d0441

Please sign in to comment.