Skip to content

Commit 755dcd2

Browse files
authored
feat(sdk): Consistent state for metadata and permissions (#2889)
Changed these `StreamrClient` methods to use cached `StreamRegistry` data: - `getStream()` - `isStreamPublisher()` - `isStreamSubscriber()` Before these PRs explicitly bypassed the cache. Therefore it was possible that these methods saw different state for metadata and permissions than other components of the SDK. It is better that the whole SDK uses consistent state. Now that is achieved by using cached `StreamRegistry` data by all components. ## Future improvements - Maybe we could add a public API method for invalidating the caches?
1 parent 1d4b0d7 commit 755dcd2

File tree

3 files changed

+14
-24
lines changed

3 files changed

+14
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ Changes before Tatum release are not documented in this file.
4040
- **BREAKING CHANGE:** Replace methods `StreamrClient#updateStream()` and `Stream#update()`: (https://github.com/streamr-dev/network/pull/2826, https://github.com/streamr-dev/network/pull/2855, https://github.com/streamr-dev/network/pull/2859, https://github.com/streamr-dev/network/pull/2862)
4141
- use `StreamrClient#setStreamMetadata()` and `Stream#setMetadata()` instead
4242
- both methods overwrite metadata instead of merging it
43-
- Change storage node address caching (https://github.com/streamr-dev/network/pull/2877, https://github.com/streamr-dev/network/pull/2878)
43+
- Caching changes:
44+
- storage node addresses (https://github.com/streamr-dev/network/pull/2877, https://github.com/streamr-dev/network/pull/2878)
45+
- stream metadata and permissions (https://github.com/streamr-dev/network/pull/2889)
4446
- Upgrade `StreamRegistry` from v4 to v5 (https://github.com/streamr-dev/network/pull/2780)
4547
- Network-level changes:
4648
- avoid routing through proxy connections (https://github.com/streamr-dev/network/pull/2801)

packages/sdk/src/StreamrClient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ export class StreamrClient {
357357
*/
358358
async getStream(streamIdOrPath: string): Promise<Stream> {
359359
const streamId = await this.streamIdBuilder.toStreamID(streamIdOrPath)
360-
const metadata = await this.streamRegistry.getStreamMetadata(streamId, false)
360+
const metadata = await this.streamRegistry.getStreamMetadata(streamId)
361361
return new Stream(streamId, metadata, this)
362362
}
363363

@@ -501,15 +501,15 @@ export class StreamrClient {
501501
*/
502502
async isStreamPublisher(streamIdOrPath: string, userId: HexString): Promise<boolean> {
503503
const streamId = await this.streamIdBuilder.toStreamID(streamIdOrPath)
504-
return this.streamRegistry.isStreamPublisher(streamId, toUserId(userId), false)
504+
return this.streamRegistry.isStreamPublisher(streamId, toUserId(userId))
505505
}
506506

507507
/**
508508
* Checks whether a given ethereum address has {@link StreamPermission.SUBSCRIBE} permission to a stream.
509509
*/
510510
async isStreamSubscriber(streamIdOrPath: string, userId: HexString): Promise<boolean> {
511511
const streamId = await this.streamIdBuilder.toStreamID(streamIdOrPath)
512-
return this.streamRegistry.isStreamSubscriber(streamId, toUserId(userId), false)
512+
return this.streamRegistry.isStreamSubscriber(streamId, toUserId(userId))
513513
}
514514

515515
// --------------------------------------------------------------------------------------------

packages/sdk/src/contracts/StreamRegistry.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ export class StreamRegistry {
172172
cacheKey: ([streamId]) => formCacheKeyPrefix(streamId)
173173
})
174174
this.isStreamPublisher_cached = new CachingMap((streamId: StreamID, userId: UserID) => {
175-
return this.isStreamPublisher(streamId, userId, false)
175+
return this.isStreamPublisherOrSubscriber_nonCached(streamId, userId, StreamPermission.PUBLISH)
176176
}, {
177177
...config.cache,
178178
cacheKey: ([streamId, userId]) =>`${formCacheKeyPrefix(streamId)}${userId}`
179179
})
180180
this.isStreamSubscriber_cached = new CachingMap((streamId: StreamID, userId: UserID) => {
181-
return this.isStreamSubscriber(streamId, userId, false)
181+
return this.isStreamPublisherOrSubscriber_nonCached(streamId, userId, StreamPermission.SUBSCRIBE)
182182
}, {
183183
...config.cache,
184184
cacheKey: ([streamId, userId]) =>`${formCacheKeyPrefix(streamId)}${userId}`
@@ -511,28 +511,16 @@ export class StreamRegistry {
511511
// Caching
512512
// --------------------------------------------------------------------------------------------
513513

514-
getStreamMetadata(streamId: StreamID, useCache = true): Promise<StreamMetadata> {
515-
if (useCache) {
516-
return this.getStreamMetadata_cached.get(streamId)
517-
} else {
518-
return this.getStreamMetadata_nonCached(streamId)
519-
}
514+
getStreamMetadata(streamId: StreamID): Promise<StreamMetadata> {
515+
return this.getStreamMetadata_cached.get(streamId)
520516
}
521517

522-
isStreamPublisher(streamId: StreamID, userId: UserID, useCache = true): Promise<boolean> {
523-
if (useCache) {
524-
return this.isStreamPublisher_cached.get(streamId, userId)
525-
} else {
526-
return this.isStreamPublisherOrSubscriber_nonCached(streamId, userId, StreamPermission.PUBLISH)
527-
}
518+
isStreamPublisher(streamId: StreamID, userId: UserID): Promise<boolean> {
519+
return this.isStreamPublisher_cached.get(streamId, userId)
528520
}
529521

530-
isStreamSubscriber(streamId: StreamID, userId: UserID, useCache = true): Promise<boolean> {
531-
if (useCache) {
532-
return this.isStreamSubscriber_cached.get(streamId, userId)
533-
} else {
534-
return this.isStreamPublisherOrSubscriber_nonCached(streamId, userId, StreamPermission.SUBSCRIBE)
535-
}
522+
isStreamSubscriber(streamId: StreamID, userId: UserID): Promise<boolean> {
523+
return this.isStreamSubscriber_cached.get(streamId, userId)
536524
}
537525

538526
hasPublicSubscribePermission(streamId: StreamID): Promise<boolean> {

0 commit comments

Comments
 (0)