@@ -3,7 +3,7 @@ import './utils/PatchTsyringe'
33
44import { DhtAddress } from '@streamr/dht'
55import { ProxyDirection } from '@streamr/trackerless-network'
6- import { DEFAULT_PARTITION_COUNT , EthereumAddress , HexString , StreamID , TheGraphClient , toEthereumAddress , toUserId } from '@streamr/utils'
6+ import { DEFAULT_PARTITION_COUNT , EthereumAddress , HexString , Logger , StreamID , TheGraphClient , toEthereumAddress , toUserId } from '@streamr/utils'
77import type { Overrides } from 'ethers'
88import EventEmitter from 'eventemitter3'
99import merge from 'lodash/merge'
@@ -73,6 +73,8 @@ export interface ExtraSubscribeOptions {
7373 erc1271Contract ?: HexString
7474}
7575
76+ const logger = new Logger ( module )
77+
7678/**
7779 * The main API used to interact with Streamr.
7880 *
@@ -357,8 +359,11 @@ export class StreamrClient {
357359 */
358360 async getStream ( streamIdOrPath : string ) : Promise < Stream > {
359361 const streamId = await this . streamIdBuilder . toStreamID ( streamIdOrPath )
360- const metadata = await this . streamRegistry . getStreamMetadata ( streamId , false )
361- return new Stream ( streamId , metadata , this )
362+ // Query metadata to check if the stream exists. This if there is no metadata and therefore
363+ // no stream). Also populates the StreamRegistry's metadata cache as a side-effect: i.e.
364+ // the metadata will be available e.g. for calling stream.getPartitionCount()
365+ await this . streamRegistry . getStreamMetadata ( streamId , false )
366+ return new Stream ( streamId , this )
362367 }
363368
364369 /**
@@ -376,7 +381,7 @@ export class StreamrClient {
376381 const streamId = await this . streamIdBuilder . toStreamID ( props . id )
377382 const metadata = merge ( { partitions : DEFAULT_PARTITION_COUNT } , omit ( props , 'id' ) )
378383 await this . streamRegistry . createStream ( streamId , metadata )
379- return new Stream ( streamId , metadata , this )
384+ return new Stream ( streamId , this )
380385 }
381386
382387 /**
@@ -429,17 +434,20 @@ export class StreamrClient {
429434 * @param permissionFilter - permissions that should be in effect for a result
430435 * @param orderBy - the default is ascending order by stream id field
431436 */
432- searchStreams (
437+ async * searchStreams (
433438 term : string | undefined ,
434439 permissionFilter : SearchStreamsPermissionFilter | undefined ,
435440 orderBy : SearchStreamsOrderBy = { field : 'id' , direction : 'asc' }
436441 ) : AsyncIterable < Stream > {
437- return this . streamRegistry . searchStreams (
442+ logger . debug ( 'Search for streams' , { term, permissionFilter } )
443+ const streamIds = this . streamRegistry . searchStreams (
438444 term ,
439445 ( permissionFilter !== undefined ) ? toInternalSearchStreamsPermissionFilter ( permissionFilter ) : undefined ,
440- orderBy ,
441- this
446+ orderBy
442447 )
448+ for await ( const id of streamIds ) {
449+ yield new Stream ( id , this )
450+ }
443451 }
444452
445453 // --------------------------------------------------------------------------------------------
@@ -572,8 +580,11 @@ export class StreamrClient {
572580 */
573581 async getStoredStreams ( storageNodeAddress : HexString ) : Promise < { streams : Stream [ ] , blockNumber : number } > {
574582 const queryResult = await this . streamStorageRegistry . getStoredStreams ( toEthereumAddress ( storageNodeAddress ) )
583+ for ( const stream of queryResult . streams ) {
584+ this . streamRegistry . populateMetadataCache ( stream . id , stream . metadata )
585+ }
575586 return {
576- streams : queryResult . streams . map ( ( item ) => new Stream ( item . id , item . metadata , this ) ) ,
587+ streams : queryResult . streams . map ( ( item ) => new Stream ( item . id , this ) ) ,
577588 blockNumber : queryResult . blockNumber
578589 }
579590 }
0 commit comments