diff --git a/packages/objectloader/examples/browser/script.js b/packages/objectloader/examples/browser/script.js index 82831349e..fb50b4bcc 100644 --- a/packages/objectloader/examples/browser/script.js +++ b/packages/objectloader/examples/browser/script.js @@ -23,9 +23,11 @@ let loader = new ObjectLoader({ window.loadData = async function loadData() { + let obj = await loader.getAndConstructObject((e) =>{ - console.log(e) + console.log(e) // log progress! }) + console.log('Done!') console.log( obj ) } \ No newline at end of file diff --git a/packages/objectloader/examples/node/script.js b/packages/objectloader/examples/node/script.js index 92127e1d5..9267d5241 100644 --- a/packages/objectloader/examples/node/script.js +++ b/packages/objectloader/examples/node/script.js @@ -1,15 +1 @@ -import ObjectLoader from '../../index.js' - -// https://latest.speckle.dev/streams/92b620fb17/objects/7cd9d41b5b5f3c8908536aec2a05f1a1 -let loader = new ObjectLoader({ - serverUrl:"https://latest.speckle.dev", - streamId:"92b620fb17", - objectId:"878c426bb213ddb4d580da74922a2b16" -}) - -let obj = await loader.getAndConstructObject((e)=>{ - console.log(e) -}) - -console.log('Done!') -console.log(obj) \ No newline at end of file +// NOTE: This lib is not working in node, because node-fetch returns node-native readable streams - we need a workaround first. diff --git a/packages/objectloader/index.js b/packages/objectloader/index.js index d492f67d3..c7c5f0aae 100644 --- a/packages/objectloader/index.js +++ b/packages/objectloader/index.js @@ -7,6 +7,10 @@ export default class ObjectLoader { + /** + * Creates a new object loader instance. + * @param {*} param0 + */ constructor( { serverUrl, streamId, token, objectId, options = { fullyTraverseArrays: false, excludeProps: [ ] } } ) { this.INTERVAL_MS = 20 this.TIMEOUT_MS = 180000 // three mins @@ -40,6 +44,11 @@ export default class ObjectLoader { this.intervals.forEach( i => clearInterval( i.interval ) ) } + /** + * Use this method to receive and construct the object. It will return the full, de-referenced and de-chunked original object. + * @param {*} onProgress + * @returns + */ async getAndConstructObject( onProgress ) { ;( await this.downloadObjectsInBuffer( onProgress ) ) // Fire and forget; PS: semicolon of doom @@ -48,6 +57,10 @@ export default class ObjectLoader { return this.traverseAndConstruct( rootObject, onProgress ) } + /** + * Internal function used to download all the objects in a local buffer. + * @param {*} onProgress + */ async downloadObjectsInBuffer( onProgress ) { let first = true let downloadNum = 0 @@ -64,6 +77,12 @@ export default class ObjectLoader { this.isLoading = false } + /** + * Internal function used to recursively traverse an object and populate its references and dechunk any arrays. + * @param {*} obj + * @param {*} onProgress + * @returns + */ async traverseAndConstruct( obj, onProgress ) { if( !obj ) return if ( typeof obj !== 'object' ) return obj @@ -111,6 +130,11 @@ export default class ObjectLoader { return obj } + /** + * Internal function. Returns a promise that is resolved when the object id is loaded into the internal buffer. + * @param {*} id + * @returns + */ async getObject( id ){ if ( this.buffer[id] ) return this.buffer[id] diff --git a/packages/objectloader/readme.md b/packages/objectloader/readme.md index c30e56227..d9ad8f78f 100644 --- a/packages/objectloader/readme.md +++ b/packages/objectloader/readme.md @@ -36,6 +36,24 @@ async load( { serverUrl, token, streamId, objectId } ) { ``` +If you do not want to process the objects one by one as they are streamed to you, you can use the `getAndConstructObject()` method. Here's an example: + +```js + +let loader = new ObjectLoader( { + serverUrl: "https://latest.speckle.dev", + streamId: "3ed8357f29", + objectId: "0408ab9caaa2ebefb2dd7f1f671e7555", + options: { + fullyTraverseArrays: false, // Default: false. By default, if an array starts with a primitive type, it will not be traversed. Set it to true if you want to capture scenarios in which lists can have intersped objects and primitives, e.g. [ 1, 2, "a", { important object } ] + excludeProps: [ 'displayValue', 'displayMesh', '__closure' ] // Default: []. Any prop names that you pass in here will be ignored from object construction traversal. + } +} ) + +let obj = await loader.getAndConstructObject( ( e ) => console.log( 'Progress', e ) ) + +``` + ## Community If in trouble, the Speckle Community hangs out on [the forum](https://speckle.community). Do join and introduce yourself! We're happy to help.