Skip to content

Simplify subscription async API #32

@dmytro-kashcheiev

Description

@dmytro-kashcheiev

Problem:
Send command and observe resulting events via subscription.
Solution:

// Create subscription before command
var subscription = client
    .subscribeToEvents<Event>()
    .post();
subscription.eventMessages.forEach((event) {
	....
	<some event reaction here>
	....
})

/// Now when we have a subscription let's send the command.
client.command(commandMessage)

Expectations:
We receive Event
Actual behavior:
Sometimes we receive Event.

Reasoning:
subscribeToEvents.post is async by design and by Javadoc, but doesn't async by the method signature.

To make above example work. Following solution should be used:

var plannedSubscription = client
    .subscribeToEvents<Event>()
    .post();
// Await for internal Future<Subscription> to resolve so subscription is actually created
plannedSubscription.subsciption.then((ignored) => client.command(commandMessage));

It's an unclear API and it's difficult to use it, especially when you want to have several subscriptions active before command send.

Consider rewriting of .post method API to post(...) async so the developer is forced to decide either it's important for him to resolve this future right now or not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions