Implements limitless advanced queries to Stellar Horizon nodes
npm install @cosmic-plus/loopcallloopcall /
Readme
• Contributing
• Changelog
!Licence
!Build


!Vulnerabilities
!Downloads
!Bundle
Implements limitless advanced queries to Stellar Horizon nodes.
Loopcall is a tiny library that enables unlimited complex queries to
Horizon nodes. It takes a _CallBuilder_ and accepts a few optional
parameters. It returns an array of records similar to the ones returned byCallBuilder.call().
- NPM: npm install @cosmic-plus/loopcall
- Yarn: yarn add @cosmic-plus/loopcall
In your script: const loopcall = require("@cosmic-plus/loopcall")
bower install cosmic-plus-loopcall
In your HTML page:
``HTML`
In your HTML page:
`HTML`
_Note:_ For production release it is advised to serve your copy of the library.
#### loopcall(callBuilder, [options]) ⇒ Array
Fetch more than 200 records
To get an arbitrary amount of record:
`js`
const callBuilder = server.operations().order("asc")
const the2000FirstOperations = await loopcall(callBuilder, { limit: 2000 })
To get all existing records (take care with that one!):
`js`
const callBuilder = server.transactions().forAccount("GDE...YBX")
const allTransactions = await loopcall(callBuilder)
Conditional Break
To stop fetching records when a condition is met:
`js`
const callBuilder = server.transactions().forAccount("GDE...YBX")
const thisYearTransactions = await loopcall(callBuilder, {
breaker: (record) => record.created_at.substr(0, 4) < 2018
})
breaker is a _Function_ that is called over each fetched record. Once ittrue
returns , the fetching loop breaks and the record that triggered the
break is discarded.
Conditional Filtering
To filter records by condition:
`js`
const callBuilder = server.transactions().forAccount("GDE...YBX")
const transactionsWithoutMemo = await loopcall(callBuilder, {
filter: (record) => !record.memo
})
filter is a _Function_ that is called over each fetched record. Whentrue
provided, the records are added to the query results only when it returns.
Iterating over records on-the-fly
In some situations waiting for the result to be concatenated is not an
option. filter offers the possibility of iterating over records while they
are fetched:
`js
const callBuilder = server.transactions()
async function showTxUntilScreenIsFilled(record) {
displayTxUsingRecord(record)
await endOfPageReached()
}
loopcall(callBuilder, { filter: showTxUntilScreenIsFilled })
`
This example shows a part of the code to implement unlimited scrolling on a
webpage showing the last transactions on a Stellar network.
Combining parameters
All those parameters may be combined together:
`js
const callBuilder = server.operations().order("asc")
function iterateOver1000RecordsMax() {
let counter = 0
return function () {
counter++
if (counter > 1000) return true
}
}
const the20firstAccountCreations = await loopcall(callBuilder, {
limit: 20,
breaker: iterateOver1000RecordsMax(),
filter: (record) => record.type === "create_account"
})
`
When both are provided, breaker is called before filter.
| Param | Type | Description |
| ----------------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| callBuilder | CallBuilder | A CallBuilder object |Object
| [options] | | |Integer
| [options.limit] | | The maximum number of record to return |function
| [options.filter] | | A function that accepts a record argument. It is called with each fetched record. If it returns a true value, the record is added to returned records, else it is discarded. |function` | A function that accepts a record argument. It is called with each fetched record. If it returns a true value, the loop ends and the array of the filtered records is returned. |
| [options.breaker] |
Organization: Cosmic.plus | @GitHub | @NPM