js-stellar-sdk icon indicating copy to clipboard operation
js-stellar-sdk copied to clipboard

[Regression] Types for some call builders are wrong

Open andywer opened this issue 5 years ago • 2 comments

We used to have CallBuilder with one generic type parameter which didn't work, since some call builders work on single records, others on collections of records, but some can return either single records or collections, depending on whether .call() or .stream() is used.

Then there was #269 to fix that (also see #271).

When rewriting the SDK in TypeScript, the single-parameter CallBuilder had a come back and if you try to implement a TransactionCallBuilder.stream() according to its type definition, for instance, you will create non-working code 😕

andywer avatar Aug 08 '19 10:08 andywer

Right now there is only one type argument to CallBuilder again, specifying what kind of data it will return.

The CallBuilder provides two methods to retrieve data (.call() & .stream()) which might or might not return the same kind of data. For those call builders that return different data on .call() vs. on .stream() (like the TransactionCallBuilder) we need to provide two type parameters to CallBuilder: The call return type and the streaming return type.

Example:

Here the TransactionCallBuilder (created via Server.transactions()) will return a collection of recent transactions on .call().

const { records } = await horizon
  .transactions()
  .forAccount(accountPubKey)
  .limit(limit)
  .order("desc")
  .call()

If I now stream new transactions for that account as they happen, however, onmessage will be called with one transaction at a time. Even if I use .stream() to retrieve a set of historic transactions it will call onmessage multiple times, once for every transaction.

horizon
  .transactions()
  .forAccount(accountPubKey)
  .cursor(cursor)
  .stream({
    onmessage(transaction) {
      // ...
    }
  })

So the return type of .call() and the type of the data passed to onmessage by .stream() cannot be specified by one shared type argument.

andywer avatar Aug 08 '19 15:08 andywer

@andywer related https://github.com/stellar/js-stellar-sdk/pull/489

abuiles avatar Feb 06 '20 02:02 abuiles