Skip to content

feat: Accessing the Event Stream Should Depend on scanner.start() Being Invoked #146

@0xNeshi

Description

@0xNeshi

The problem currently is that it is easy to forget to call scanner.start() before starting to read the event stream.

This could be addressed using a sort of typestate pattern (open for discussion, need your input @LeoPatOZ ):

  • scanner.start() returns some struct ScannerStartedToken that cannot be instantiated outside the crate
  • scanner.subscribe(filter) returns Subscription
  • stream can only be accessed by passing the ScannerStartedToken like: subscription.stream(scanner_started_token)
  • this ensures that scanner.start() must be called before streaming can start

Full example:

// init scanner as before
let mut scanner = EventScannerBuilder::sync().from_block(0).connect(provider);

let filter = EventFilter::new();

// create subscription
let subscription = scanner.subscribe(filter);

// because scanner wasn't started, client can't access stream events yet
// let mut stream = subscription.stream(!!!missing token - won't compile!!!);

// start scanner
// name `token` used for simplicity, exact name open for discussion
let token = scanner.start().await?;  

// now we're sure scanner is started, we can access the stream
let mut stream = subscription.stream(&token);

while let Some(msg) = stream.next().await {
    // ...
}

Metadata

Metadata

Assignees

Labels

priority: mediumShould be done soon but not blockingstatus: needs discussionRequires team input before implementationtype: enhancementImprovement to existing functionality

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions