Class: CommandRequest

CommandRequest(commandMessagenon-null, clientnon-null, actorRequestFactorynon-null)

A request to post a command to the Spine backend. Optionally allows to subscribe to events that are the immediate results of handling the command. A usage example: ``` client.command(logInUser) .onOk(_logOk) .onError(_logError) .onImmediateRejection(_warnAboutRejection) .observe(UserLoggedIn.class, ({subscribe, unsubscribe}) => { subscribe(event => _logAndUnsubscribe(event, unsubscribe)); setTimeout(unsubscribe, EVENT_WAIT_TIMEOUT); }) .observe(UserAlreadyLoggedIn.class, (({subscribe, unsubscribe}) => { subscribe(event => _warnAboutAndUnsubscribe(event, unsubscribe)); setTimeout(unsubscribe, EVENT_WAIT_TIMEOUT); }) .post(); ``` The `subscribe` callback provided to the consumer allows to configure an event receival process while the `unsubscribe` callback allows to cancel the subscription. Please note that in the example above we make sure the subscription is cancelled even if the specified event type is never received.

Constructor

new CommandRequest(commandMessagenon-null, clientnon-null, actorRequestFactorynon-null)

Parameters:
Name Type Description
commandMessage Message the command to post
client Client the client which initiated the request
actorRequestFactory ActorRequestFactory the request factory
Source:

Classes

CommandRequest

Methods

observe(eventTypenon-null, consumernon-null) → {this}

Adds the event type to the list of observed command handling results.
Parameters:
Name Type Description
eventType Class.<Message> a type of the observed events
consumer consumerCallback.<EventSubscriptionCallbacks> a consumer of the `subscribe` and `unsubscribe` callbacks which are responsible for accepting the incoming events and cancelling the subscription respectively
Source:
Returns:
self for method chaining
Type
this

onError(callbacknon-null) → {this}

Runs the callback if the command could not be handled by the Spine server due to a technical error.
Parameters:
Name Type Description
callback consumerCallback.<CommandHandlingError> the callback to run
Source:
Returns:
self for method chaining
Type
this

onImmediateRejection(callbacknon-null) → {this}

Runs the callback if the server responded on a command with an immediate rejection. The immediate rejection means the command did not pass the command filters set up in the bounded context and was disqualified from execution right away. A typical example of this would be the command not passing filters due to user permissions being not broad enough. Please note that this rejection is different to a "normal" rejection when the command is acknowledged with the `OK` status and then reaches the handler method which processes it. Such rejections can be tracked using the `observe(...)` method of this request.
Parameters:
Name Type Description
callback consumerCallback.<spine.core.Event>
Source:
Returns:
self for method chaining
Type
this

onOk(callbacknon-null) → {this}

Runs the callback if the command is successfully handled by the Spine server.
Parameters:
Name Type Description
callback parameterlessCallback the callback to run
Source:
Returns:
self for method chaining
Type
this

post() → {Promise.<void>}

Posts the command to the server and subscribes to all observed types.
Source:
Returns:
a promise that signals if the command posting was done successfully, may be ignored
Type
Promise.<void>