feat(objectloader): added example usage for new func in readme
This commit is contained in:
@@ -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 )
|
||||
}
|
||||
@@ -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)
|
||||
// NOTE: This lib is not working in node, because node-fetch returns node-native readable streams - we need a workaround first.
|
||||
|
||||
@@ -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]
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user